Example #1
0
 /**
  * Catch all calls, fire events and remap to real instance
  *
  * @access public
  * @param string $method
  * @param array $args
  * @return mixed
  */
 public function __call($method, array $args = array())
 {
     $event = $this->observer->createEvent()->setMethod($method)->setParams($args)->setInstance($this)->setType('before');
     $before = $this->observer->trigger()->getResult();
     if ($event->isPropagationStopped()) {
         return $before;
     }
     /** @var \Closure $closure */
     if ($closure = $this->observer->bound($method)) {
         $callback = $closure->bindTo($this, $this);
         $result = Executor::callClosure($callback, $event->getParams());
     } else {
         $result = Executor::call($this(true), $method, $event->getParams());
     }
     $event->setType('after')->setResult($result);
     return $this->observer->trigger()->getResult();
 }
 public function testBindByObject()
 {
     $stdMock = $this->getMock('\\StdClass', ['test']);
     $stdMock->expects($this->once())->method('test');
     $callback = function () use($stdMock) {
         $stdMock->test();
     };
     $obj = new \StdClass();
     $observer = new Observer([[$obj, 'test', $callback]]);
     $observer->trigger($obj, 'test');
 }