Пример #1
0
 public function inject(Container $container, $service, array $serviceConfig)
 {
     $callConfig = array();
     if (array_key_exists('call', $serviceConfig)) {
         $callConfig = $serviceConfig['call'];
     }
     foreach ($callConfig as $methodName => $parameters) {
         if (false !== strpos($methodName, '[')) {
             if (preg_match('`^([^\\[]*)\\[[0-9]*\\]$`i', $methodName, $matches)) {
                 $methodToCall = $matches[1];
             } else {
                 throw new UnbuildableServiceException(sprintf("Invalid method name '%s'", $methodName));
             }
         } else {
             $methodToCall = $methodName;
         }
         $convertedParameters = $container->resolveMany($parameters);
         if (method_exists($service, $methodToCall)) {
             call_user_func_array(array($service, $methodToCall), $convertedParameters);
         } else {
             throw new UnbuildableServiceException(sprintf("Couldn't call method '%s' in '%s', method doesn't exist", $methodToCall, get_class($service)));
         }
     }
     return true;
 }
 public function createInstance(Container $container, $serviceName, array $serviceConfig)
 {
     list($instanceName, $methodName) = explode('->', $serviceConfig['builder']);
     $invocationSite = $container->get($instanceName);
     $activationArgs = isset($serviceConfig['arguments']) ? $container->resolveMany($serviceConfig['arguments']) : array();
     if (!method_exists($invocationSite, $methodName)) {
         throw new UnbuildableServiceException(sprintf("Class '%s' has no '%s' method.", $instanceName, $methodName));
     }
     return call_user_func_array(array($invocationSite, $methodName), $activationArgs);
 }
Пример #3
0
 public function inject(Container $container, $service, array $serviceConfig)
 {
     $propConfig = array();
     if (array_key_exists('props', $serviceConfig)) {
         $propConfig = $serviceConfig['props'];
     }
     foreach ($propConfig as $propName => $propValue) {
         $convertedValue = $container->resolve($propValue);
         $service->{$propName} = $convertedValue;
     }
     return true;
 }
Пример #4
0
 public function encapsulate(Container $container, $object, array $serviceConfig)
 {
     if (array_key_exists('interceptor', $serviceConfig)) {
         foreach ($serviceConfig['interceptor'] as $interceptorName) {
             $interceptor = $container->resolve($interceptorName);
             if (!is_object($interceptor)) {
                 throw new \RuntimeException('The interceptor ' . $interceptorName . ' does not reference a known service');
             }
             $interceptor->setDecorated($object);
             $object = $interceptor;
         }
     }
     return $object;
 }
Пример #5
0
    public function testAddingParameterWontEraseCollateralData()
    {
        $yml = <<<YML
parameters :
    dummy :
        key : dummy-value
YML;
        $container = new Container(new \DICIT\Config\YMLInline($yml));
        $container->setParameter('dummy', array('db' => array("host" => "127.0.0.1", "port" => 5432)));
        $this->assertSame('dummy-value', $container->getParameter('dummy.key'));
    }