PDOプロキシは、すべてのPDOメソッドの実行を傍受および変更できるシンプルなイベント駆動型のPDOラッパーです。
このような機能は、次のように使用できます。
PDOProxyPDOおよびPDOProxyPDOStatementクラスは、ネイティブPDOおよびPDOStatementクラスを拡張するため、通常のPDOオブジェクトを期待する方法と互換性があります。
このライブラリをインストールした後、次のようにプロキシ構成を提供する必要があります。
<?php
use PDOProxy EventManager ;
use PDOProxy ProxyConfiguration ;
use PDOProxy PDOCommand ;
$ ev = new EventManager ();
// you can alter the output of any method, like "__construct", "query", "prepare"...
// in order to do so, you must pass method name as an event name
// please note: multiple event listeners may be attached to a single event type
$ ev -> addEventListener ( " query " , function ( PDOCommand $ command , string $ eventName ) {
// you can alter the result of any PDO method
$ event -> setResult ( new PDOMockStatement ( " query " , " SELECT 1 " );
$ command -> stopPropagation ();
});
// you can intercept any method, like "__construct", "query", "prepare"...
// to do so, you must add "#pre" suffix to the event name
$ ev -> addEventListener ( " query#pre " , function ( PDOCommand $ command , string $ eventName ) {
// if you stop event propagation, the real PDO method won't be executed and the result will be taken from this callback
if ( $ command -> getArgs ()[ 0 ] == " SELECT 1 " ) {
$ command -> stopPropagation ();
}
});
ProxyConfiguration:: init ( $ ev );これで、通常のPDOオブジェクトと同じようにpdoproxyを初期化できます。
<?php
use PDOProxy PDO ;
$ pdo = new PDO ( " dsn " , " user " , " password " , [ " option1 " => 1 , " option2 " => 2 ]);
$ result = $ pdo -> query ( " SELECT 2 " );
// [...]プロキシは2つの異なる方法でインストールできます。
user@host:/directory$ composer require pdo-proxy/pdo-proxy
ソースコードは、次のURLのzipファイルにあります:https://github.com/artur-graniszewski/pdo-proxy/archive/master.zip
上記のコードで示されているように、各イベントハンドラーは、特定のEventオブジェクトとイベント名(文字列として)の2つのパラメーターへのアクセスを獲得します。
PDOプロキシによって提起された2つのイベントタイプの概要を次に示します。
PDOProxyPDOクラスメソッドのすべての実行で提起されます
<?php
namespace PDOProxy ;
interface PDOCommandInterface
{
public function getArgs () : array ;
public function setArgs ( array $ args );
public function getMethodName () : string ;
public function getResult ();
public function setResult ( $ result );
} getArgs()およびsetArgs()メソッドにより、 PDOProxyPDOメソッドに渡された入力パラメーターを取得および変更できますが、 getMethodName()により、どのPDOメソッドが実行されたかを確認できます。
getResult()およびsetResult()イベントメソッドにより、特定のPDOメソッドによって返される結果にアクセスして変更できます。
PDOProxyPDOStatementクラスメソッドのすべての実行で提起されます
<?php
namespace PDOProxy ;
interface PDOStatementCommandInterface extends PDOCommandInterface
{
public function getParentArgs () : array ;
public function setParentArgs ( array $ args );
public function getParentMethodName () : string ;
} getParentArgs()およびsetParentArgs()メソッドにより、指定されたPDOProxyPDOStatementオブジェクトを作成したPDOProxyPDOメソッドに渡された入力パラメーターを取得および変更できますが、 getParentMethodName()により、最初に実行されたPDOメソッドを確認できます。
すべてのPDOイベントは、次の一般的なイベントインターフェイスを拡張します。
<?php
namespace PDOProxy ;
interface EventInterface
{
public function stopPropagation ();
public function isPropagationStopped () : bool ;
} stopPropagation()メソッドは、さらにイベントの伝播を停止します(他のイベントリスナーが特定のイベントタイプで実行されていません)。 「#pre」文(「クエリ#pre」など)で接尾辞が付着したイベント名の場合、元のpdo/pdostatementメソッドはsetResult()で実行されません。
PDOProxyEventManagerのインスタンスをStatic init() PDOProxyProxyConfigurationクラスのメソッドに提供することにより、プロキシを構成する必要があります(上記の例を参照)。
プロキシ構成の前にPDOProxyPDOクラスをインスタンス化すると、 LogicExceptionが得られることに留意してください