PDO Proxy هو غلاف 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 );الآن ، يمكنك تهيئة pdoproxy تمامًا مثل كائن PDO العادي:
<?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
يمكن العثور على رموز المصدر في ملف zip تحت عنوان URL التالي: https://github.com/artur-graniszewski/pdo-proxy/archive/master.zip
كما هو موضح في الكود أعلاه ، تمكن كل معالج الأحداث من الوصول إلى معلمتين ، كائن Event محدد واسم الحدث (كسلسلة).
فيما يلي نظرة عامة على نوعين من الأحداث التي أثارها PDO Proxy:
يتم رفعه على كل تنفيذ من طريقة فئة 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() بإحضار وتغيير معلمات الإدخال التي تم تمريرها إلى طريقة PDOProxyPDO التي أنشأت طريقة PDOProxyPDOStatement المعينة ، في حين أن getParentMethodName() يتيح التحقق من طريقة PDO في المقام الأول.
تمتد جميع أحداث PDO على واجهة الحدث الزائدة التالية:
<?php
namespace PDOProxy ;
interface EventInterface
{
public function stopPropagation ();
public function isPropagationStopped () : bool ;
} تتوقف طريقة stopPropagation() عن مزيد من انتشار الأحداث (مما يؤدي إلى عدم تنفيذ مستمعي الأحداث الآخرين لنوع حدث معين). في حالة أسماء الأحداث ، اللاحقة مع جملة "#Pre" (مثل "الاستعلام#قبل") ، لن يتم تنفيذ طريقة PDO/PDOSTATEMENT الأصلية على قاعدة البيانات ، في مثل هذه الحالة ، يجب الاستهزاء بتنفيذ طريقة setResult() .
يجب تكوين الوكيل من خلال توفير مثيل لفئة PDOProxyEventManager لبطولة init() الثابتة لفئة PDOProxyProxyConfiguration (انظر المثال أعلاه).
يرجى الأخذ في الاعتبار أن إنشاء فئة PDOProxyPDO قبل تكوين الوكيل ينتج عنه LogicException