Exemplo n.º 1
0
 /**
  * **Invokes a class method with dependency injection.**
  *
  * Creates and optionally invokes a class->method() using dependency injection.
  *
  * @param string|object $class   - name of the class to make
  * @param string        $method  - class method to call
  * @param bool          $execute - TRUE to execute the object->method, FALSE instantiates and returns
  *
  * @return mixed - whatever the object returns
  */
 public function invokeClassMethod($class, $method = NULL, $execute = TRUE)
 {
     // is the class described by `class@method`?
     if (is_string($class) and Lib::str_has(':', $class)) {
         list($class, $method) = explode(':', $class);
     }
     // extract the route class dependencies
     $class_dependencies = $this->extract_dependencies($class, $method);
     list($reflection, $arguments) = array_values($class_dependencies);
     // construct a new class object.
     // this will trigger an early return
     if ($method === '__construct') {
         /** @var ReflectionClass $reflection */
         $reflection = new \ReflectionClass($class);
         // this is a simple call to instantiate a class.
         return $reflection->newInstanceArgs($arguments);
     }
     // optionally, transfer control over to the class:method.
     if ($execute) {
         /** @var \ReflectionClass $rf */
         $rf = new \ReflectionClass($reflection->class);
         $constructor = $class;
         // determine if a constructor exists and has required parameters
         if ($rf->getConstructor()) {
             // extract its dependencies
             $dependencies = $this->extract_dependencies($class, '__construct');
             // instantiate the object through its constructor
             $constructor = $rf->newInstanceArgs($dependencies['arg_list']);
         }
         // invoke the method
         /** @var \ReflectionMethod $reflection */
         return $reflection->invokeArgs($constructor, $arguments);
     }
     # no constructor so instantiate the class without it.
     return new $reflection->class();
 }