static function factory($rawDefinition)
 {
     $rawDefinition = Collection::cast($rawDefinition);
     // first check an id has been provided
     if ($rawDefinition->lacks('id')) {
         throw new Exception('Missing mandatory \'id\' parameter in service definition', Exception::INCOMPLETE_SERVICE_SPECS);
     }
     // try to guess service type if not provided
     if ($rawDefinition->lacks('type')) {
         $matchingTypes = [];
         foreach (['instance' => PrefabServiceSpecs::class, 'class' => ClassServiceSpecs::class] as $key => $type) {
             if ($rawDefinition->has($key)) {
                 $matchingTypes[] = $type;
             }
         }
         if (!$matchingTypes) {
             // throw new Exception('The service specs factory has not been able to guess what type of service has been passed. Please check your syntax, or explicitly define the "type" key in your service specifications', Exception::INCOMPLETE_SERVICE_SPECS);
             // default to UndefinedService
             $matchingTypes[] = UndefinedServiceSpecs::class;
         }
         if (count($matchingTypes) > 1) {
             throw new Exception('Service specifications are ambiguous: they contain both "instance" and "class" key. Please remove the unneeded oneor explicitly define the "type" key in your service specifications ', Exception::AMBIGUOUS_SERVICE_SPECS);
         }
         // only one match
         $rawDefinition['type'] = array_pop($matchingTypes);
     }
     $serviceDefinition = call_user_func([$rawDefinition['type'], 'factory'], $rawDefinition);
     // static
     if ($rawDefinition->has('static')) {
         $serviceDefinition->setStatic($rawDefinition['static']);
     }
     // aliases
     if ($rawDefinition->has('alias') || $rawDefinition->has('aliases')) {
         $aliases = new Collection();
         if ($rawDefinition->has('alias')) {
             $aliases[] = $rawDefinition['alias'];
         }
         if ($rawDefinition->has('aliases')) {
             $aliases->merge($rawDefinition['aliases']);
         }
         $serviceDefinition->setAliases($aliases);
     }
     return $serviceDefinition;
 }
Example #2
0
 public function testMergeWithCombiningValueMerger()
 {
     $collection = new Collection(['a' => 'x']);
     $mergedCollection = new Collection(['a' => 'y']);
     $merger = $this->getMockBuilder(ValueMerger::class)->disableOriginalConstructor()->getMock();
     $merger->expects($this->once())->method('merge')->with('x', 'y')->willReturn('merged value');
     $collection->addMerger('a', $merger);
     $collection->merge($mergedCollection);
     $this->assertEquals(['a' => 'merged value'], $collection->toArray());
 }