-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Add ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE #24033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,26 +23,15 @@ | |
* Container is a dependency injection container. | ||
* | ||
* It gives access to object instances (services). | ||
* | ||
* Services and parameters are simple key/pair stores. | ||
* | ||
* Parameter and service keys are case insensitive. | ||
* | ||
* A service can also be defined by creating a method named | ||
* getXXXService(), where XXX is the camelized version of the id: | ||
* | ||
* <ul> | ||
* <li>request -> getRequestService()</li> | ||
* <li>mysql_session_storage -> getMysqlSessionStorageService()</li> | ||
* <li>symfony.mysql_session_storage -> getSymfony_MysqlSessionStorageService()</li> | ||
* </ul> | ||
* | ||
* The container can have three possible behaviors when a service does not exist: | ||
* The container can have four possible behaviors when a service | ||
* does not exist (or is not initialized for the last case): | ||
* | ||
* * EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default) | ||
* * NULL_ON_INVALID_REFERENCE: Returns null | ||
* * IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference | ||
* (for instance, ignore a setter if the service does not exist) | ||
* * IGNORE_ON_UNINITIALIZED_REFERENCE: Ignores/returns null for uninitialized services or invalid references | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
|
@@ -304,9 +293,9 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE | |
|
||
try { | ||
if (isset($this->fileMap[$id])) { | ||
return $this->load($this->fileMap[$id]); | ||
return self::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior ? null : $this->load($this->fileMap[$id]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't the value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it can, but it's better as is to me (wouldn't help readability, and the extra var is just overhead, so that's two reasons) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Np then ;) |
||
} elseif (isset($this->methodMap[$id])) { | ||
return $this->{$this->methodMap[$id]}(); | ||
return self::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior ? null : $this->{$this->methodMap[$id]}(); | ||
} elseif (--$i && $id !== $normalizedId = $this->normalizeId($id)) { | ||
$id = $normalizedId; | ||
continue; | ||
|
@@ -315,7 +304,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE | |
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper) | ||
@trigger_error('Generating a dumped container without populating the method map is deprecated since 3.2 and will be unsupported in 4.0. Update your dumper to generate the method map.', E_USER_DEPRECATED); | ||
|
||
return $this->{$method}(); | ||
return self::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior ? null : $this->{$method}(); | ||
} | ||
|
||
break; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,8 +304,12 @@ private function dumpValue($value) | |
*/ | ||
private function getServiceCall($id, Reference $reference = null) | ||
{ | ||
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { | ||
return sprintf('@?%s', $id); | ||
if (null !== $reference) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we do ?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that would force duplicating the |
||
switch ($reference->getInvalidBehavior()) { | ||
case ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE: break; | ||
case ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE: return sprintf('@!%s', $id); | ||
default: return sprintf('@?%s', $id); | ||
} | ||
} | ||
|
||
return sprintf('@%s', $id); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we tag this class as
@final
to allow us to avoid having to do such magic for future changes to the container ? I'm not sure extending the ServiceReferenceGraph actually makes sense.Tagging it as
@internal
would be even more strict, but tools wanting to analyze the DIC to display debugging info may be a valid use case for using this class so I would not go that far (for instance, https://github.com/schmittjoh/JMSDebuggingBundle relies on it, even though I'm not sure it plays well with recent versions as I haven't used this bundle since years)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree,
@final
it is now