1 | <?php |
||
29 | final class ServicesBuilder implements BuilderInterface |
||
30 | { |
||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $parameters; |
||
35 | |||
36 | /** |
||
37 | * @var ReferenceResolverInterface |
||
38 | */ |
||
39 | private $referenceResolver; |
||
40 | |||
41 | /** |
||
42 | * @var ServiceInterface[] |
||
43 | */ |
||
44 | private $services; |
||
45 | |||
46 | /** |
||
47 | * @var ParameterResolverInterface|null |
||
48 | */ |
||
49 | private $parameterResolver; |
||
50 | |||
51 | /** |
||
52 | * @param ServiceInterface[] $services |
||
53 | * @param array $parameters |
||
54 | * @param ParameterResolverInterface|null $parameterResolver |
||
55 | * @param ReferenceResolverInterface|null $referenceResolver |
||
56 | */ |
||
57 | 16 | public function __construct( |
|
68 | |||
69 | 16 | public function build(Application $application) |
|
70 | { |
||
71 | try { |
||
72 | 16 | $parameterResolver = $this->getParameterResolver($application); |
|
73 | 16 | $instantiator = new ServiceInstantiator($parameterResolver, $this->referenceResolver, $application); |
|
74 | 16 | foreach ($this->services as $service) { |
|
75 | 14 | $this->buildService($service, $instantiator, $application); |
|
76 | 5 | } |
|
77 | |||
78 | 10 | return $this->parameters; |
|
79 | 6 | } catch (BindingResolutionException $exception) { |
|
80 | 2 | throw new Exception(sprintf('Could not load "%s" class', ConfigRepository::class), 0, $exception); |
|
81 | 4 | } catch (ResolverException $exception) { |
|
82 | throw new Exception('Could not resolve the parameters', 0, $exception); |
||
83 | 4 | } catch (\Exception $exception) { |
|
84 | 4 | throw new Exception('Could not build the parameters', 0, $exception); |
|
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param Application $application |
||
90 | * |
||
91 | * @return ParameterResolverInterface |
||
92 | */ |
||
93 | 16 | private function getParameterResolver(Application $application) |
|
102 | |||
103 | 14 | private function buildService( |
|
104 | ServiceInterface $service, |
||
105 | ServiceInstantiator $instantiator, |
||
106 | Application $application |
||
107 | ) { |
||
108 | 14 | $application->singleton( |
|
109 | 14 | $service->getName(), |
|
110 | 7 | function () use ($instantiator, $service) { |
|
111 | return $instantiator->create($service); |
||
112 | 7 | } |
|
113 | 7 | ); |
|
114 | 10 | $application->bind($service->getClass(), $service->getName()); |
|
115 | 8 | $this->bindAutowiringTypes($service, $application); |
|
116 | 8 | $this->tagService($service, $application); |
|
117 | 8 | } |
|
118 | |||
119 | 8 | private function bindAutowiringTypes(ServiceInterface $service, Application $application) |
|
125 | |||
126 | 8 | private function tagService(ServiceInterface $service, Application $application) |
|
132 | } |
||
133 |