2011-01-05 11:13:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-01-05 11:13:27 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2016-12-13 17:37:51 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
|
|
|
|
2011-01-05 11:13:27 +00:00
|
|
|
/**
|
|
|
|
* Inline service definitions where this is possible.
|
|
|
|
*
|
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
*/
|
2017-01-17 20:04:53 +00:00
|
|
|
class InlineServiceDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
|
2011-01-05 11:13:27 +00:00
|
|
|
{
|
2011-03-11 13:50:46 +00:00
|
|
|
private $repeatedPass;
|
2017-05-22 15:33:24 +01:00
|
|
|
private $inlinedServiceIds = array();
|
2011-01-08 08:39:53 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-08 08:39:53 +00:00
|
|
|
public function setRepeatedPass(RepeatedPass $repeatedPass)
|
|
|
|
{
|
|
|
|
$this->repeatedPass = $repeatedPass;
|
|
|
|
}
|
2011-01-07 14:44:29 +00:00
|
|
|
|
2017-05-22 15:33:24 +01:00
|
|
|
/**
|
|
|
|
* Returns an array of all services inlined by this pass.
|
|
|
|
*
|
|
|
|
* @return array Service id strings
|
|
|
|
*/
|
|
|
|
public function getInlinedServiceIds()
|
|
|
|
{
|
|
|
|
return $this->inlinedServiceIds;
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
2017-01-17 20:04:53 +00:00
|
|
|
* {@inheritdoc}
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2017-01-17 20:04:53 +00:00
|
|
|
protected function processValue($value, $isRoot = false)
|
2011-01-05 11:13:27 +00:00
|
|
|
{
|
2017-01-17 20:04:53 +00:00
|
|
|
if ($value instanceof ArgumentInterface) {
|
2017-04-21 10:31:17 +01:00
|
|
|
// Reference found in ArgumentInterface::getValues() are not inlineable
|
2017-01-17 20:04:53 +00:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) {
|
|
|
|
$definition = $this->container->getDefinition($id);
|
2011-01-05 11:13:27 +00:00
|
|
|
|
2017-01-24 18:16:15 +00:00
|
|
|
if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
|
|
|
|
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
|
2017-05-22 15:33:24 +01:00
|
|
|
$this->inlinedServiceIds[] = $id;
|
2011-04-12 23:51:22 +01:00
|
|
|
|
2017-01-17 20:04:53 +00:00
|
|
|
if ($definition->isShared()) {
|
|
|
|
return $definition;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
2017-01-17 20:04:53 +00:00
|
|
|
$value = clone $definition;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:04:53 +00:00
|
|
|
return parent::processValue($value, $isRoot);
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Checks if the definition is inlineable.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return bool If the definition is inlineable
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2017-01-17 20:04:53 +00:00
|
|
|
private function isInlineableDefinition($id, Definition $definition, ServiceReferenceGraph $graph)
|
2011-01-05 11:13:27 +00:00
|
|
|
{
|
2015-09-04 20:54:37 +01:00
|
|
|
if (!$definition->isShared()) {
|
2011-01-05 11:13:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:39:53 +01:00
|
|
|
if ($definition->isPublic() || $definition->isLazy()) {
|
2011-01-05 11:13:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:04:53 +00:00
|
|
|
if (!$graph->hasNode($id)) {
|
2011-01-09 09:53:46 +00:00
|
|
|
return true;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 11:49:18 +00:00
|
|
|
if ($this->currentId == $id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-09 09:53:46 +00:00
|
|
|
$ids = array();
|
2017-01-17 20:04:53 +00:00
|
|
|
foreach ($graph->getNode($id)->getInEdges() as $edge) {
|
2011-01-09 09:53:46 +00:00
|
|
|
$ids[] = $edge->getSourceNode()->getId();
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 12:44:42 +00:00
|
|
|
if (count(array_unique($ids)) > 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-04 20:27:00 +00:00
|
|
|
if (count($ids) > 1 && is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-04 20:54:37 +01:00
|
|
|
return true;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
2011-02-04 18:10:13 +00:00
|
|
|
}
|