Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 704d6b1

Browse files
bakura10weierophinney
authored andcommitted
Never cache with build
1 parent b4e1c6a commit 704d6b1

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

src/ServiceLocatorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface ServiceLocatorInterface extends ContainerInterface
2222
*
2323
* @param string $name
2424
* @param array $options
25-
* @return object
25+
* @return mixed
2626
* @throws Exception\ServiceNotFoundException If no factory/abstract factory could be found to create the instance
2727
* @throws Exception\ServiceNotCreatedException If factory/delegator fails to create the instance
2828
*/

src/ServiceManager.php

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,44 +118,15 @@ public function withConfig(array $config)
118118
* {@inheritDoc}
119119
*/
120120
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 = [])
131121
{
132122
$name = $this->resolveAlias($name);
133123

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])) {
137126
return $this->services[$name];
138127
}
139128

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);
159130

160131
if (($this->sharedByDefault && !isset($this->shared[$name]))
161132
|| (isset($this->shared[$name]) && $this->shared[$name])) {
@@ -165,6 +136,17 @@ public function build($name, array $options = [])
165136
return $object;
166137
}
167138

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+
168150
/**
169151
* {@inheritDoc}
170152
*/
@@ -331,4 +313,34 @@ private function resolveAlias($alias)
331313

332314
return $name;
333315
}
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+
}
334346
}

0 commit comments

Comments
 (0)