Esempio n. 1
0
File: Inspector.php Progetto: cti/di
 /**
  * @param $class
  * @param $method
  * @return int
  */
 public function getMethodRequiredCount($class, $method)
 {
     if ($this->getCache()->contains(__METHOD__, func_get_args())) {
         return $this->getCache()->get(__METHOD__, func_get_args());
     }
     $requiredCount = 0;
     $reflection = Reflection::getReflectionMethod($class, $method);
     foreach ($reflection->getParameters() as $parameter) {
         if (!$parameter->isDefaultValueAvailable()) {
             $requiredCount++;
         }
     }
     $this->getCache()->set(__METHOD__, func_get_args(), $requiredCount);
     return $requiredCount;
 }
Esempio n. 2
0
 /**
  * @param $instance
  */
 function process($instance)
 {
     $class = get_class($instance);
     $this->processHook($this->before, $class, $instance);
     if (method_exists($instance, 'init')) {
         if (in_array('init', $this->getManager()->getInspector()->getPublicMethods($class))) {
             $this->getManager()->call($instance, 'init');
         } else {
             $reflection = Reflection::getReflectionMethod($class, 'init');
             $reflection->setAccessible(true);
             $this->getManager()->call($instance, 'init');
             $reflection->setAccessible(false);
         }
     }
     $this->processHook($this->after, $class, $instance);
 }
Esempio n. 3
0
File: Callback.php Progetto: cti/di
 /**
  * @param null $instance
  * @param array $parameters
  * @param Manager $manager
  * @return mixed
  * @throws Exception
  */
 function launch($instance = null, $parameters, Manager $manager)
 {
     $arguments = array();
     foreach ($this->arguments as $index => $argument) {
         if (is_string($argument) && isset($parameters[$argument])) {
             $arguments[] = $parameters[$argument];
         } elseif ($argument instanceof Reference) {
             // find parameter by class
             $foundInParams = false;
             foreach ($parameters as $param) {
                 if (is_a($param, $argument->getClass())) {
                     $arguments[] = $param;
                     $foundInParams = true;
                     break;
                 }
             }
             if (!$foundInParams) {
                 $arguments[] = $argument->getInstance($manager);
             }
         } else {
             if (count(array_filter(array_keys($parameters), 'is_int')) > 0) {
                 $arguments[] = array_shift($parameters);
             } else {
                 if ($index < $this->requiredCount) {
                     throw new Exception(sprintf("Key %s for method %s::%s not found!", $argument, $this->class, $this->method));
                 }
             }
         }
     }
     if ($this->method == '__construct') {
         if (!count($arguments)) {
             return new $this->class();
         }
         return Reflection::getReflectionClass($this->class)->newInstanceArgs($arguments);
     }
     if (in_array($this->method, $manager->getInspector()->getPublicMethods($this->class))) {
         return call_user_func_array(array($instance, $this->method), $arguments);
     }
     return Reflection::getReflectionMethod($this->class, $this->method)->invokeArgs($instance, $arguments);
 }