Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Factorize compiler passes around new AbstractRecursivePass #21327

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

Merged
merged 1 commit into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractRecursivePass implements CompilerPassInterface
{
protected $container;
protected $currentId;
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be "better" to create getters for those?

Copy link
Member Author

@nicolas-grekas nicolas-grekas Jan 18, 2017

Choose a reason for hiding this comment

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

Some passes set the value also. They are "local vars passed as properties". Cleaned after use. Just saves boilerplates this way. I'd prefer keep them asis.


/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$this->container = $container;

try {
$this->processValue($container->getDefinitions(), true);
} finally {
$this->container = null;
}
}

/**
* Processes a value found in a definition tree.
*
* @param mixed $value
* @param bool $isRoot
*
* @return mixed The processed value
Copy link
Member

Choose a reason for hiding this comment

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

Can be removed as mixed does not really help :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I fear that people won't notice that the return value is here at all.

*/
protected function processValue($value, $isRoot = false)
{
if (is_array($value)) {
foreach ($value as $k => $v) {
if ($isRoot) {
$this->currentId = $k;
}
if ($v !== $processedValue = $this->processValue($v, $isRoot)) {
$value[$k] = $processedValue;
}
}
} elseif ($value instanceof ArgumentInterface) {
$value->setValues($this->processValue($value->getValues()));
} elseif ($value instanceof Definition) {
$value->setArguments($this->processValue($value->getArguments()));
$value->setProperties($this->processValue($value->getProperties()));
$value->setMethodCalls($this->processValue($value->getMethodCalls()));

if ($v = $value->getFactory()) {
$value->setFactory($this->processValue($v));
}
if ($v = $value->getConfigurator()) {
$value->setConfigurator($this->processValue($v));
}
}

return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class AnalyzeServiceReferencesPass implements RepeatablePassInterface
class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface
{
private $graph;
private $container;
private $currentId;
private $currentDefinition;
private $repeatedPass;
private $onlyConstructorArguments;
private $lazy;

/**
* @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
Expand Down Expand Up @@ -60,68 +59,60 @@ public function process(ContainerBuilder $container)
$this->container = $container;
$this->graph = $container->getCompiler()->getServiceReferenceGraph();
$this->graph->clear();

foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->isSynthetic() || $definition->isAbstract()) {
continue;
}

$this->currentId = $id;
$this->currentDefinition = $definition;

$this->processArguments($definition->getArguments());
if (is_array($definition->getFactory())) {
$this->processArguments($definition->getFactory());
}

if (!$this->onlyConstructorArguments) {
$this->processArguments($definition->getMethodCalls());
$this->processArguments($definition->getProperties());
if ($definition->getConfigurator()) {
$this->processArguments(array($definition->getConfigurator()));
}
}
}
$this->lazy = false;

foreach ($container->getAliases() as $id => $alias) {
Copy link
Member

Choose a reason for hiding this comment

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

this is moved before the processing of definitions instead of after. Is it expected ? And does it make any difference ?

Copy link
Member Author

Choose a reason for hiding this comment

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

given that this pass is ready-only, that doesn't make any difference yes

$this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
}

parent::process($container);
}

/**
* Processes service definitions for arguments to find relationships for the service graph.
*
* @param array $arguments An array of Reference or Definition objects relating to service definitions
* @param bool $lazy Whether the references nested in the arguments should be considered lazy or not
*/
private function processArguments(array $arguments, $lazy = false)
protected function processValue($value, $isRoot = false)
{
foreach ($arguments as $argument) {
if (is_array($argument)) {
$this->processArguments($argument, $lazy);
} elseif ($argument instanceof ArgumentInterface) {
$this->processArguments($argument->getValues(), true);
} elseif ($argument instanceof Reference) {
$targetDefinition = $this->getDefinition((string) $argument);

$this->graph->connect(
$this->currentId,
$this->currentDefinition,
$this->getDefinitionId((string) $argument),
$targetDefinition,
$argument,
$lazy || ($targetDefinition && $targetDefinition->isLazy())
);
} elseif ($argument instanceof Definition) {
$this->processArguments($argument->getArguments());
$this->processArguments($argument->getMethodCalls());
$this->processArguments($argument->getProperties());

if (is_array($argument->getFactory())) {
$this->processArguments($argument->getFactory());
}
$lazy = $this->lazy;

if ($value instanceof ArgumentInterface) {
$this->lazy = true;
parent::processValue($value);
$this->lazy = $lazy;

return $value;
}
if ($value instanceof Reference) {
$targetDefinition = $this->getDefinition((string) $value);

$this->graph->connect(
$this->currentId,
$this->currentDefinition,
$this->getDefinitionId((string) $value),
$targetDefinition,
$value,
$this->lazy || ($targetDefinition && $targetDefinition->isLazy())
);

return $value;
}
if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
}
if ($isRoot) {
if ($value->isSynthetic() || $value->isAbstract()) {
return $value;
}
$this->currentDefinition = $value;
}
$this->lazy = false;

if ($this->onlyConstructorArguments) {
$this->processValue($value->getFactory());
$this->processValue($value->getArguments());
} else {
parent::processValue($value, $isRoot);
}
$this->lazy = $lazy;

return $value;
}

/**
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.