示例#1
0
 public function __sleep()
 {
     return array_merge(parent::__sleep(), array('factoryService', 'factoryMethod', 'factoryArguments'));
 }
示例#2
0
文件: Service.php 项目: splot/di
 public function applyParent(Service $parent)
 {
     if (!$this->getClass()) {
         $this->setClass($parent->getClass());
     }
     $arguments = $this->getArguments();
     if (empty($arguments)) {
         $this->setArguments($parent->getArguments());
     }
     // prepend parent method calls
     foreach ($parent->getMethodCalls() as $methodCall) {
         array_unshift($this->methodCalls, $methodCall);
     }
 }
示例#3
0
 public function __construct($name, $object)
 {
     parent::__construct($name);
     $this->instance = $object;
     $this->class = Debugger::getType($object);
 }
示例#4
0
 public function __construct($name, $closure)
 {
     parent::__construct($name);
     $this->closure = $closure;
 }
示例#5
0
文件: Container.php 项目: splot/di
 /**
  * Expands short options and definitions to full options array.
  * 
  * @param  array   $options Array of service definition options.
  * @param  Service $service The service to configure.
  * @return array
  */
 protected function expandAndVerifyOptions(array $options, Service $service)
 {
     // if options is an array with at least 2 numeric keys then treat it as a very compact factory definition
     if (isset($options[0]) && isset($options[1]) && (count($options) === 2 || count($options) === 3)) {
         $options = array('factory' => $options);
     }
     // if defined factory key then expand it
     if (isset($options['factory'])) {
         if (!isset($options['factory'][0]) || !isset($options['factory'][1])) {
             throw new InvalidServiceException('You have to specify factory service name and method when registering a service built from a factory for "' . $service->getName() . '".');
         }
         $options['factory_service'] = $options['factory'][0];
         $options['factory_method'] = $options['factory'][1];
         $options['factory_arguments'] = isset($options['factory'][2]) ? is_array($options['factory'][2]) ? $options['factory'][2] : array($options['factory'][2]) : array();
     }
     $options = array_merge($this->defaultOptions, $options);
     if ($options['factory_service'] && empty($options['factory_method'])) {
         throw new InvalidServiceException('Cannot define service built from factory without specifying factory method for "' . $service->getName() . '".');
     }
     if (is_string($options['aliases'])) {
         $options['aliases'] = array($options['aliases']);
     }
     return $options;
 }
示例#6
0
 /**
  * Call a method on a service.
  * 
  * @param  Service $service Service to call a method on.
  * @param  string  $methodName Name of the method to call on the service.
  * @param  array   $arguments [optional] Arguments to call the method with. Default: `array()`.
  * @param  object  $instance [optional] Service object instance. Should be passed for all non-singleton services.
  * @return mixed
  *
  * @throws RuntimeException When trying to call a method on a service that hasn't been instantiated yet.
  */
 public function callMethod(Service $service, $methodName, array $arguments = array(), $instance = null)
 {
     if ($instance === null && !$service->isInstantiated()) {
         throw new RuntimeException('Cannot call a method of a service that has not been instantiated yet, when trying to call "::' . $methodName . '" on "' . $service->name . '".');
     }
     $instance = $instance ? $instance : $service->getInstance();
     $arguments = $this->argumentsResolver->resolve($arguments);
     switch (count($arguments)) {
         case 0:
             $result = $instance->{$methodName}();
             break;
         case 1:
             $result = $instance->{$methodName}($arguments[0]);
             break;
         case 2:
             $result = $instance->{$methodName}($arguments[0], $arguments[1]);
             break;
         case 3:
             $result = $instance->{$methodName}($arguments[0], $arguments[1], $arguments[2]);
             break;
         case 4:
             $result = $instance->{$methodName}($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
             break;
         case 5:
             $result = $instance->{$methodName}($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
             break;
         default:
             $result = call_user_func_array(array($instance, $methodName), $arguments);
     }
     return $result;
 }