/**
  * @param string $service
  * @param array $params
  * @return mixed|object
  * @throws \InvalidArgumentException
  */
 public function create($service, array $params = array())
 {
     $prefix = substr($service, 0, 9);
     if ($prefix === 'heystack.' || $prefix === 'heystack?') {
         $service = substr($service, 9);
         if ($this->heystackContainer->has($service)) {
             return $this->heystackContainer->get($service);
         } elseif ($this->heystackContainer->hasParameter($service)) {
             return $this->heystackContainer->getParameter($service);
         } elseif ($prefix === 'heystack?') {
             return null;
         }
         throw new \InvalidArgumentException(sprintf("Requested Heystack service or parameter '%s' doesn't exist", $service));
     } else {
         return parent::create($service, $params);
     }
 }
 public function create($class, array $params = array())
 {
     if (strpos($class, '(') === false) {
         return parent::create($class, $params);
     } else {
         list($class, $params) = Object::parse_class_spec($class);
         $params = $this->injector->convertServiceProperty($params);
         return parent::create($class, $params);
     }
 }
예제 #3
0
 /**
  * Instantiate a managed object
  *
  * Given a specification of the form
  *
  * array(
  *		'class' => 'ClassName',
  *		'properties' => array('property' => 'scalar', 'other' => '%$BeanRef')
  *		'id' => 'ServiceId',
  *		'type' => 'singleton|prototype'
  * )
  *
  * will create a new object, store it in the service registry, and
  * set any relevant properties
  *
  * Optionally, you can pass a class name directly for creation
  * 
  * To access this from the outside, you should call ->get('Name') to ensure
  * the appropriate checks are made on the specific type. 
  * 
  *
  * @param array $spec
  *				The specification of the class to instantiate
  * @param string $id
  *				The name of the object being created. If not supplied, then the id will be inferred from the
  *				object being created
  * @param string $type
  *				Whether to create as a singleton or prototype object. Allows code to be explicit as to how it
  *				wants the object to be returned
  */
 protected function instantiate($spec, $id = null, $type = null)
 {
     if (is_string($spec)) {
         $spec = array('class' => $spec);
     }
     $class = $spec['class'];
     // create the object, using any constructor bindings
     $constructorParams = array();
     if (isset($spec['constructor']) && is_array($spec['constructor'])) {
         $constructorParams = $spec['constructor'];
     }
     $object = $this->objectCreator->create($this, $class, $constructorParams);
     // figure out if we have a specific id set or not. In some cases, we might be instantiating objects
     // that we don't manage directly; we don't want to store these in the service cache below
     if (!$id) {
         $id = isset($spec['id']) ? $spec['id'] : null;
     }
     // now set the service in place if needbe. This is NOT done for prototype beans, as they're
     // created anew each time
     if (!$type) {
         $type = isset($spec['type']) ? $spec['type'] : null;
     }
     if ($id && (!$type || $type != 'prototype')) {
         // this ABSOLUTELY must be set before the object is injected.
         // This prevents circular reference errors down the line
         $this->serviceCache[$id] = $object;
     }
     // now inject safely
     $this->inject($object, $id);
     return $object;
 }
예제 #4
0
 public function create(Injector $injector, $class, $params = array())
 {
     if (strpos($class, '(') === false) {
         return parent::create($injector, $class, $params);
     } else {
         list($class, $params) = self::parse_class_spec($class);
         return parent::create($injector, $class, $params);
     }
 }