8000 [DI] Generate one file per service factory by nicolas-grekas · Pull Request #23678 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Generate one file per service factory #23678

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[DI] Generate one file per service factory
  • Loading branch information
nicolas-grekas committed Aug 1, 2017
commit 359b6ef367471cd7d19676e21ba4e07907c31545
8 changes: 6 additions & 2 deletions src/Symfony/Bridge/Doctrine/ManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
if (isset($this->aliases[$name])) {
$name = $this->aliases[$name];
}
$method = $this->methodMap[$name] ?? 'get'.strtr($name, $this->underscoreMap).'Service'; // BC with DI v3.4
$wrapped 10000 Instance = $this->{$method}(false);
if (isset($this->fileMap[$name])) {
$wrappedInstance = $this->requireInScope($this->fileMap[$name], false);
} else { // BC with DI v3.4
$method = $this->methodMap[$name] ?? 'get'.strtr($name, $this->underscoreMap).'Service';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas you said on Slack that you can submit it on 3.4 but that version AFAIS has php >=5.5.9 requirement, so ?? operator is not compatible. So it should be changed when submitting to 3.4 or maybe @$this->methodMap[$name] ?: should be used instead.

$wrappedInstance = $this->{$method}(false);
}

$manager->setProxyInitializer(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ public function getProxyFactoryCode(Definition $definition, $id, $factoryCode =
$instantiation = 'return';

if ($definition->isShared()) {
$instantiation .= sprintf(' $this->%s[\'%s\'] =', $definition->isPublic() || !method_exists(ContainerBuilder::class, 'addClassResource') ? 'services' : 'privates', $id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idiom refactor:smile:

if (!method_exists(ContainerBuilder::class, 'addClassResource')) {
$instantiation .= sprintf(' $this->%s[\'%s\'] =', $definition->isPublic() ? 'services' : 'privates', $id);
} else {
// BC with DI v3.4
$instantiation .= sprintf(' $this->services[\'%s\'] =', $id);
}
}

if (null === $factoryCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Clear and Warmup the cache.
* Clear the cache.
*
* @author Francis Besset <francis.besset@gmail.com>
* @author Fabien Potencier <fabien@symfony.com>
Expand Down
68 changes: 34 additions & 34 deletions src/Symfony/Component/DependencyInjection/Container.php
F438
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Container implements ResettableContainerInterface
protected $parameterBag;

protected $services = array();
protected $fileMap = array();
protected $methodMap = array();
protected $aliases = array();
protected $loading = array();
Expand Down Expand Up @@ -150,7 +151,7 @@ public function set($id, $service)
throw new InvalidArgumentException('You cannot set service "service_container".');
}

if (isset($this->methodMap[$id])) {
if (isset($this->fileMap[$id]) || isset($this->methodMap[$id])) {
throw new InvalidArgumentException(sprintf('You cannot set the pre-defined service "%s".', $id));
}

Expand Down Expand Up @@ -186,19 +187,12 @@ public function has($id)
return true;
}

if (isset($this->methodMap[$id])) {
return true;
}

return false;
return isset($this->fileMap[$id]) || isset($this->methodMap[$id]);
}

/**
* Gets a service.
*
* If a service is defined both through a set() method and
* with a get{$id}Service() method, the former has always precedence.
*
* @param string $id The service identifier
* @param int $invalidBehavior The behavior when the service does not exist
*
Expand Down Expand Up @@ -228,32 +222,14 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
}

if (isset($this->methodMap[$id])) {
$method = $this->methodMap[$id];
} else {
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
if (!$id) {
throw new ServiceNotFoundException($id);
}

$alternatives = array();
foreach ($this->getServiceIds() as $knownId) {
$lev = levenshtein($id, $knownId);
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
$alternatives[] = $knownId;
}
}

throw new ServiceNotFoundException($id, null, null, $alternatives);
}

return;
}

$this->loading[$id] = true;

try {
$service = $this->$method();
if (isset($this->fileMap[$id])) {
return $this->requireInScope($this->fileMap[$id]);
} elseif (isset($this->methodMap[$id])) {
return $this->{$this->methodMap[$id]}();
}
} catch (\Exception $e) {
unset($this->services[$id]);

Expand All @@ -262,7 +238,21 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
unset($this->loading[$id]);
}

return $service;
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
if (!$id) {
throw new ServiceNotFoundException($id);
}

$alternatives = array();
foreach ($this->getServiceIds() as $knownId) {
$lev = levenshtein($id, $knownId);
if ($lev <= strlen($id) / 3 || false !== strpos($knownId, $id)) {
$alternatives[] = $knownId;
}
}

throw new ServiceNotFoundException($id, null, null, $alternatives);
}
}

/**
Expand Down Expand Up @@ -300,7 +290,7 @@ public function reset()
*/
public function getServiceIds()
{
return array_unique(array_merge(array('service_container'), array_keys($this->methodMap), array_keys($this->services)));
return array_unique(array_merge(array('service_container'), array_keys($this->fileMap), array_keys($this->methodMap), array_keys($this->services)));
}

/**
Expand All @@ -327,6 +317,16 @@ public static function underscore($id)
return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), str_replace('_', '.', $id)));
}

/**
* Creates a service by requiring its factory file.
*
* @return object The service created by the file
*/
protected function requireInScope($file)
{
return require $file;
}

/**
* Fetches a variable from the environment.
*
Expand Down
Loading
0