Beispiel #1
0
 /**
  * Standard implementation of delegate unserialization behavior
  * @param string $data
  * @return mixed
  * @throws Exception
  */
 function unserialize($data)
 {
     list($code, $context) = (array) unserialize($data);
     $builder = function () use($code, $context) {
         if (strpos(ini_get('disable_functions'), 'eval') === false) {
             $fn_code = '$_function = ' . $code . ';';
             eval($fn_code);
             return $_function;
         } else {
             $id = qtil\Identifier::identify($code);
             $fn_code = 'function delegate_' . $id . '() {
                         return ' . $code . ';
                     }';
             Veval::execute('<?php ' . $fn_code);
             return function () use($id) {
                 $closure = call_user_func_array('delegate_' . $id, func_get_args());
                 return $closure();
             };
         }
     };
     $closure = $builder();
     if (isset($closure) && is_callable($closure)) {
         Registry::add($this, $closure, $context);
     } else {
         throw new Exception('Invalid callback, unpacking delegate failed.');
     }
 }
Beispiel #2
0
 function testClassScheme()
 {
     $scheme = new Identifier\ClassScheme('qtil\\Tests\\Mock\\User', 'getIdentifier');
     Identifier::addScheme($scheme);
     $user = new Mock\User();
     $id = Identifier::identify($user);
     $this->assertEquals($id, 1);
 }
Beispiel #3
0
 /**
  * Retrieve a unique identifier for object instance
  * @param mixed $object
  * @return string
  */
 private static function identify($object)
 {
     if ($uid = array_search($object, self::$uids)) {
         return $uid;
     }
     $uid = qtil\Identifier::identify($object);
     self::$uids[$uid] = $object;
     return $uid;
 }
Beispiel #4
0
 function __construct($id = null)
 {
     if (is_null($id)) {
         $this->id = qtil\Identifier::identify($this);
     } else {
         $this->id = $id;
         $this->load();
     }
     $this->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_DELETE);
 }
Beispiel #5
0
 function __construct($name, qio\Resource $resource)
 {
     $this->name = $name;
     $this->resource = $resource;
     $this->id = qtil\Identifier::identify($this);
 }
Beispiel #6
0
 /**
  * Perform clean up / tear down tasks
  */
 protected function tearDown()
 {
     Identifier::clearSchemes();
 }
Beispiel #7
0
 /**
  * Dynamically adds context to closure
  * @param mixed $object
  * @param array $context
  */
 static function rebind($object, array $context)
 {
     $code = self::code($object);
     $count = count($context) - 1;
     $fn_code = '$_function = ' . $code . ';';
     if (($pos = strpos($fn_code, 'use(')) !== false) {
         $c_string = '';
         foreach ($context as $k => $c) {
             $c_string .= '$' . $k . ',';
         }
         $fn_code = substr($fn_code, 0, $pos) . $c_string . substr($fn_code, $pos);
     } else {
         $pos = strpos($fn_code, '{');
         if ($pos !== false) {
             $pos -= 1;
             $c_string = '';
             $i = 0;
             foreach ($context as $k => $c) {
                 $c_string .= '$' . $k;
                 if ($i < $count) {
                     $c_string .= ',';
                 }
                 $i++;
             }
             $fn_code = substr($fn_code, 0, $pos) . 'use(' . $c_string . ')' . substr($fn_code, $pos);
         }
     }
     if (strpos(ini_get('disable_functions'), 'eval') === false) {
         extract($context);
         eval($fn_code);
     } else {
         $id = qtil\Identifier::identify($code);
         $a_string = '';
         $i = 0;
         foreach ($context as $k => $c) {
             $a_string .= '$' . $k;
             if ($i < $count) {
                 $a_string .= ',';
             }
             $i++;
         }
         $fn_code = 'function delegate_' . $id . '(' . $a_string . ') {
                     return ' . $fn_code . ';
                 }';
         \Veval::execute('<?php ' . $fn_code);
         $_function = call_user_func_array('delegate_' . $id, array_values($context));
     }
     self::remove($object);
     self::add($object, $_function);
 }