/**
  * Constructor
  *
  * @param   lang.XPClass $scriptlet
  * @param   string[] args
  * @param   [:string] env
  */
 public function __construct(XPClass $scriptlet, $args, $env = [], $filters = [])
 {
     if ($scriptlet->hasConstructor()) {
         $this->scriptlet = $scriptlet->getConstructor()->newInstance((array) $args);
     } else {
         $this->scriptlet = $scriptlet->newInstance();
     }
     foreach ($filters as $filter) {
         $this->scriptlet->filter($filter);
     }
     $this->scriptlet->init();
     $this->env = $env;
 }
Example #2
0
 /**
  * Creates a new instance of a given class. If the constructor uses
  * injection, the arguments are compiled from the relevant annotations.
  * Otherwise, optional constructor arguments may be passed.
  *
  * @param   lang.XPClass $class
  * @param   [:var] $named Named arguments
  * @return  var
  * @throws  inject.ProvisionException
  */
 public function newInstance(XPClass $class, $named = [])
 {
     if ($class->hasConstructor()) {
         $constructor = $class->getConstructor();
         try {
             return $constructor->newInstance($this->args($constructor, $named));
         } catch (TargetInvocationException $e) {
             throw new ProvisionException('Error creating an instance of ' . $class->getName(), $e->getCause());
         } catch (Throwable $e) {
             throw new ProvisionException('Error creating an instance of ' . $class->getName(), $e);
         }
     } else {
         return $class->newInstance();
     }
 }