Advertisement
fruffl

ObjectAccessLogger

Mar 9th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.26 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System
  6.      * @package    ILLI
  7.      * @subpackage System
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.     NAMESPACE ILLI\System;
  13.  
  14.     /**
  15.      * ILLI System Trace
  16.      *
  17.      * Array Wrapper to log object access (call, set, get).
  18.      * It's absurd to log all objects in your program!
  19.      *
  20.      * Implements iArrayAccess to allow ArrayAccess-method-calls
  21.      *
  22.      * <code>
  23.      * <?PHP
  24.      *  $m = new Trace(new TypeArray(["m.foo", "m.bar", "m.fox"]));
  25.      *  $m[] = 'foobar';
  26.      *  $m->insert(2, '____baz');
  27.      *     
  28.      *  var_dump($m);
  29.      * ?>
  30.      * </code>
  31.      * <code>
  32.      *  object(ILLI\System\Trace)#2 (2) {
  33.      *    ["__traces":"ILLI\System\Trace":private]=>
  34.      *    array(2) {
  35.      *      [0]=>
  36.      *      array(4) {
  37.      *        ["type"]=>
  38.      *        string(11) "method_call"
  39.      *        ["name"]=>
  40.      *        string(9) "offsetSet"
  41.      *        ["args"]=>
  42.      *        array(2) {
  43.      *          [0]=>
  44.      *          NULL
  45.      *          [1]=>
  46.      *          string(6) "foobar"
  47.      *        }
  48.      *        ["call"]=>
  49.      *        string(87) "/var/www/clients/client5/web22/web/dev/index.php:89"
  50.      *      }
  51.      *      [1]=>
  52.      *      array(4) {
  53.      *        ["type"]=>
  54.      *        string(11) "method_call"
  55.      *        ["name"]=>
  56.      *        string(6) "insert"
  57.      *        ["args"]=>
  58.      *        array(2) {
  59.      *          [0]=>
  60.      *          int(2)
  61.      *          [1]=>
  62.      *          string(7) "____baz"
  63.      *        }
  64.      *        ["call"]=>
  65.      *        string(87) "/var/www/clients/client5/web22/web/dev/index.php:90"
  66.      *      }
  67.      *    }
  68.      *    ["__MEMBER":"ILLI\System\Trace":private]=>
  69.      *        object(ILLI\System\TypeArray)#3 (8) {
  70.      *      ...
  71.      * </code>
  72.      *
  73.      * @category   ILLI_System
  74.      * @package    ILLI
  75.      * @subpackage System
  76.      * @namespace  ILLI\System
  77.      * @link       http://illi.be
  78.      * @license    http://l.illi.be
  79.      * @copyright  ILLI Conference
  80.      * @since      2.0.0-1
  81.      * @version    2.0.0-1
  82.      * @abstract
  83.      */
  84.     CLASS Trace IMPLEMENTS iArrayAccess
  85.     {
  86.         const ACCESS_TYPE_PROPERTY    = 1;
  87.         const ACCESS_TYPE_METHOD      = 2;
  88.         const ACCESS_TYPE_METHOD_INTERNAL = 3;
  89.        
  90.         private $__traces       = array();
  91.         private $__MEMBER       = NULL;
  92.        
  93.         public function __construct($subject)
  94.         {
  95.             $this->__MEMBER = $subject;
  96.         }
  97.        
  98.         private function backtrace($depth)
  99.         {
  100.             $data = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $depth)[$depth - 1];
  101.             return $data['file'] . ':' . $data['line'];
  102.         }
  103.        
  104.         public function getTrace()
  105.         {
  106.             return $this->__traces;
  107.         }
  108.        
  109.         private function internal($method, $args)
  110.         {
  111.             $call = $this->backtrace(self::ACCESS_TYPE_METHOD_INTERNAL);
  112.                
  113.             $this->__traces[] =
  114.             [
  115.                 'type' => 'method_call',
  116.                 'name' => $method,
  117.                 'args' => $args,
  118.                 'call' => $call
  119.             ];
  120.            
  121.             return Member::call($this->__MEMBER, $method, $args);
  122.         }
  123.        
  124.         public function __call($method, $args)
  125.         {
  126.             $call = $this->backtrace(self::ACCESS_TYPE_METHOD);
  127.                
  128.             $this->__traces[] =
  129.             [
  130.                 'type' => 'method_call',
  131.                 'name' => $method,
  132.                 'args' => $args,
  133.                 'call' => $call
  134.             ];
  135.            
  136.             return Member::call($this->__MEMBER, $method, $args);
  137.         }
  138.        
  139.         public function __set($property, $value)
  140.         {
  141.             $call = $this->backtrace(self::ACCESS_TYPE_PROPERTY);
  142.             $this->__traces[] =
  143.             [
  144.                 'type' => 'set_property',
  145.                 'name' => $property,
  146.                 'value' => $value,
  147.                 'call' => $call
  148.             ];
  149.            
  150.             return Member::propertySet($this->__MEMBER, $property, $value);
  151.         }
  152.        
  153.         public function __get($property)
  154.         {
  155.             $result = Member::propertyGet($this->__MEMBER, $property, $value);
  156.             $call = $this->backtrace(self::ACCESS_TYPE_PROPERTY);
  157.             $this->__traces[] =
  158.             [
  159.                 'type' => 'get_property',
  160.                 'name' => $property,
  161.                 'value' => $result,
  162.                 'call' => $call
  163.             ];
  164.            
  165.             return $result;
  166.         }
  167.        
  168.         // log ArrayAccess calls
  169.        
  170.         public function offsetSet($offset, $value)
  171.         {
  172.             return $this->internal('offsetSet', [$offset, $value]);
  173.         }
  174.        
  175.         public function offsetExists($offset)
  176.         {
  177.             return $this->internal('offsetExists', [$offset]);
  178.         }
  179.        
  180.         public function offsetUnset($offset)
  181.         {
  182.             return $this->internal('offsetUnset', [$offset]);
  183.         }
  184.        
  185.         public function offsetGet($offset)
  186.         {
  187.             return $this->internal('offsetGet', [$offset]);
  188.         }
  189.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement