@@ -118,44 +118,15 @@ public function withConfig(array $config)
118
118
* {@inheritDoc}
119
119
*/
120
120
public function get ($ name )
121
- {
122
- return $ this ->build ($ name );
123
- }
124
-
125
- /**
126
- * {@inheritDoc}
127
- *
128
- * This is a highly performance sensitive method, do not modify if you have not benchmarked it carefully
129
- */
130
- public function build ($ name , array $ options = [])
131
121
{
132
122
$ name = $ this ->resolveAlias ($ name );
133
123
134
- // We start by checking if the service is cached (this is the fastest method). If options is not empty, we
135
- // never "share" because this could create unexpected behaviour
136
- if (empty ($ options ) && isset ($ this ->services [$ name ])) {
124
+ // We start by checking if the service is cached (this is the fastest method).
125
+ if (isset ($ this ->services [$ name ])) {
137
126
return $ this ->services [$ name ];
138
127
}
139
128
140
- try {
141
- if (!isset ($ this ->delegators [$ name ])) {
142
- // Let's create the service by fetching the factory
143
- $ factory = $ this ->getFactory ($ name );
144
- $ object = $ factory ($ this ->creationContext , $ name , $ options );
145
- } else {
146
- $ object = $ this ->createDelegatorFromName ($ name , $ options );
147
- }
148
- } catch (Exception $ exception ) {
149
- throw new ServiceNotCreatedException (sprintf (
150
- 'Service with name "%s" could not be created. Reason: %s ' ,
151
- $ name ,
152
- $ exception ->getMessage ()
153
- ));
154
- }
155
-
156
- foreach ($ this ->initializers as $ initializer ) {
157
- $ initializer ($ this ->creationContext , $ object );
158
- }
129
+ $ object = $ this ->doCreate ($ name );
159
130
160
131
if (($ this ->sharedByDefault && !isset ($ this ->shared [$ name ]))
161
132
|| (isset ($ this ->shared [$ name ]) && $ this ->shared [$ name ])) {
@@ -165,6 +136,17 @@ public function build($name, array $options = [])
165
136
return $ object ;
166
137
}
167
138
139
+ /**
140
+ * {@inheritDoc}
141
+ *
142
+ * This is a highly performance sensitive method, do not modify if you have not benchmarked it carefully
143
+ */
144
+ public function build ($ name , array $ options = [])
145
+ {
146
+ // We never cache when using "build"
147
+ return $ this ->doCreate ($ this ->resolveAlias ($ name ), $ options );
148
+ }
149
+
168
150
/**
169
151
* {@inheritDoc}
170
152
*/
@@ -331,4 +313,34 @@ private function resolveAlias($alias)
331
313
332
314
return $ name ;
333
315
}
316
+
317
+ /**
318
+ * @param string $resolvedName
319
+ * @param array $options
320
+ * @return mixed
321
+ */
322
+ private function doCreate ($ resolvedName , $ options = [])
323
+ {
324
+ try {
325
+ if (!isset ($ this ->delegators [$ resolvedName ])) {
326
+ // Let's create the service by fetching the factory
327
+ $ factory = $ this ->getFactory ($ resolvedName );
328
+ $ object = $ factory ($ this ->creationContext , $ resolvedName , $ options );
329
+ } else {
330
+ $ object = $ this ->createDelegatorFromName ($ resolvedName , $ options );
331
+ }
332
+ } catch (Exception $ exception ) {
333
+ throw new ServiceNotCreatedException (sprintf (
334
+ 'Service with name "%s" could not be created. Reason: %s ' ,
335
+ $ resolvedName ,
336
+ $ exception ->getMessage ()
337
+ ));
338
+ }
339
+
340
+ foreach ($ this ->initializers as $ initializer ) {
341
+ $ initializer ($ this ->creationContext , $ object );
342
+ }
343
+
344
+ return $ object ;
345
+ }
334
346
}
0 commit comments