public function create(Parameter $parameterConf, \Pimple $container)
 {
     $parameterName = $parameterConf->getParameterName();
     $parameterValue = $parameterConf->getParameterValue();
     // we wrap our parameter in a magic proxy class with a __invoke method which is
     // called automatically on access by pimple. this way we have a chance to access
     // parameters as references which could be set later
     // the value is evaluated on every access
     $that = $this;
     $frozen = $this->frozen;
     $old = null;
     if (isset($container[$parameterName])) {
         $old = $container[$parameterName];
     }
     $value = $this->proxyFactory->createProxy(function ($c) use($that, $parameterConf, $parameterValue, $old, &$frozen) {
         $parameterName = $parameterConf->getParameterName();
         if (!empty($frozen[$parameterName])) {
             return $frozen[$parameterName];
         }
         if (is_array($parameterValue)) {
             $c['self'] = $parameterValue;
             $parameterValue = $that->normalize($parameterValue, $c);
             unset($c['self']);
         } else {
             $parameterValue = $that->normalize($parameterValue, $c);
         }
         if (null !== $old && $parameterConf->mergeExisting()) {
             // extract the value if it is a callable
             if (is_object($old) && is_callable($old) && method_exists($old, '__invoke')) {
                 $old = $old($c);
             }
             $parameterValue = call_user_func($parameterConf->getMergeStrategy(), $old, $parameterValue);
         }
         // freeze our value on first access (as singleton) this is default
         if ($parameterConf->isFrozen()) {
             $frozen[$parameterName] = $parameterValue;
         }
         return $parameterValue;
     });
     $container[$parameterName] = $value;
     return $container;
 }
 public function it_creates_a_array_parameter_and_merges_existing_data(Parameter $parameterConf, \Pimple $container)
 {
     $parameterConf->getParameterName()->willReturn('db.options');
     $parameterConf->getParameterValue()->willReturn(array('host' => 'example.org'));
     $parameterConf->getMergeStrategy(Argument::type('array'))->willReturn('array_replace_recursive');
     $parameterConf->mergeExisting()->willReturn(true);
     $container->offsetExists('db.options')->willReturn(true);
     $container->offsetGet('db.options')->willReturn(array('host' => 'localhost', 'user' => 'root', 'password' => 'test'));
     $container->offsetSet('db.options', array('host' => 'example.org', 'user' => 'root', 'password' => 'test'))->shouldBeCalled();
     $container = $this->create($parameterConf, $container);
 }
Example #3
0
 public function create(Parameter $parameterConf, \Pimple $container)
 {
     $parameterName = $parameterConf->getParameterName();
     $parameterValue = $parameterConf->getParameterValue();
     $value = $this->normalize($parameterValue, $container);
     if ($parameterConf->mergeExisting() && isset($container[$parameterName])) {
         $old = $container[$parameterName];
         $value = call_user_func($parameterConf->getMergeStrategy($old), $old, $value);
     }
     $container[$parameterName] = $value;
     return $container;
 }