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 " );
// [...]프록시는 두 가지 방법으로 설치할 수 있습니다.
user@host:/directory$ composer require pdo-proxy/pdo-proxy
소스 코드는 다음 URL 아래에 zip 파일에서 찾을 수 있습니다 : https://github.com/artur-graniszewski/pdo-proxy/archive/master.zip
위의 코드에서 설명 된 바와 같이, 각 이벤트 핸들러는 두 개의 매개 변수, 특정 Event 개체 및 이벤트 이름 (문자열)에 대한 액세스를 얻습니다.
다음은 PDO 프록시가 제기 한 두 가지 이벤트 유형에 대한 개요입니다.
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"문장 ( "Query#pre"와 같은)이 접미사 된 이벤트 이름의 경우 원래 PDO/PDOSTATEMENT 메소드가 데이터베이스에서 실행되지 않으므로 setResult() 메소드를 실행하여 결과를 조정해야합니다.
프록시는 PDOProxyEventManager 의 인스턴스를 PDOProxyProxyConfiguration 클래스의 static init() 메소드에 제공하여 구성해야합니다 (위의 예 참조).
프록시 구성 이전에 PDOProxyPDO 클래스를 인스턴스화하는 것은 LogicException 의 결과를 명심하십시오.