-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Use the method map as authoritative list of factories for dumped containers #20113
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 |
---|---|---|
|
@@ -206,15 +206,29 @@ public function has($id) | |
) { | ||
return true; | ||
} | ||
|
||
if (isset($this->privates[$id])) { | ||
@trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); | ||
} | ||
|
||
if (isset($this->methodMap[$id])) { | ||
return true; | ||
} | ||
|
||
if (--$i && $id !== $lcId = strtolower($id)) { | ||
$id = $lcId; | ||
} else { | ||
if (isset($this->privates[$id])) { | ||
@trigger_error(sprintf('Checking for the existence of the "%s" private service is deprecated since Symfony 3.2 and won\'t be supported anymore in Symfony 4.0.', $id), E_USER_DEPRECATED); | ||
} | ||
continue; | ||
} | ||
|
||
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder, | ||
// and only when the dumper has not generated the method map (otherwise the method map is considered to be fully populated by the dumper) | ||
if (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service')) { | ||
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. Wouldn't it be cleaner to introduced a new interface for dumped containers instead of having this condition which hardcodes some names and conventions from the PHP dumper? 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. Or can' we just simplify this like that? 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. @nicolas-grekas in get, we could. But in has, this would not allow to warn properly in all cases @fabpot the convention is the deprecated one. Adding a new interface won't help, as non-updated dumpers would not implement this interface either, and updated dumpers will never enter this code due to |
||
@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 method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service'); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
|
@@ -262,7 +276,10 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE | |
} elseif (--$i && $id !== $lcId = strtolower($id)) { | ||
$id = $lcId; | ||
continue; | ||
} elseif (method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) { | ||
} elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) { | ||
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder, | ||
// 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); | ||
// $method is set to the right value, proceed | ||
} else { | ||
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) { | ||
|
@@ -341,14 +358,21 @@ public function reset() | |
public function getServiceIds() | ||
{ | ||
$ids = array(); | ||
foreach (get_class_methods($this) as $method) { | ||
if (preg_match('/^get(.+)Service$/', $method, $match)) { | ||
$ids[] = self::underscore($match[1]); | ||
|
||
if (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class) { | ||
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder, | ||
// 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); | ||
|
||
foreach (get_class_methods($this) as $method) { | ||
if (preg_match('/^get(.+)Service$/', $method, $match)) { | ||
$ids[] = self::underscore($match[1]); | ||
} | ||
} | ||
} | ||
$ids[] = 'service_container'; | ||
|
||
return array_unique(array_merge($ids, array_keys($this->services))); | ||
return array_unique(array_merge($ids, array_keys($this->methodMap), array_keys($this->services))); | ||
} | ||
|
||
/** | ||
|
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.
Reorganizing this code was done to match the way the code is organized in
get
, so that we first try to find the service as is, and then do the lowercase conversion if needed. This way, we won't make astrtolower
call when checking->has()
for an existing service anymore when providing the lowercase string already.