4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
18 * This class is used to remove circular dependencies between individual passes.
20 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
25 private $log = array();
26 private $loggingFormatter;
27 private $serviceReferenceGraph;
29 public function __construct()
31 $this->passConfig = new PassConfig();
32 $this->serviceReferenceGraph = new ServiceReferenceGraph();
36 * Returns the PassConfig.
38 * @return PassConfig The PassConfig instance
40 public function getPassConfig()
42 return $this->passConfig;
46 * Returns the ServiceReferenceGraph.
48 * @return ServiceReferenceGraph The ServiceReferenceGraph instance
50 public function getServiceReferenceGraph()
52 return $this->serviceReferenceGraph;
56 * Returns the logging formatter which can be used by compilation passes.
58 * @return LoggingFormatter
60 * @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
62 public function getLoggingFormatter()
64 if (null === $this->loggingFormatter) {
65 @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', __METHOD__), E_USER_DEPRECATED);
67 $this->loggingFormatter = new LoggingFormatter();
70 return $this->loggingFormatter;
74 * Adds a pass to the PassConfig.
76 * @param CompilerPassInterface $pass A compiler pass
77 * @param string $type The type of the pass
78 * @param int $priority Used to sort the passes
80 public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/)
82 if (\func_num_args() >= 3) {
83 $priority = func_get_arg(2);
85 if (__CLASS__ !== \get_class($this)) {
86 $r = new \ReflectionMethod($this, __FUNCTION__);
87 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
88 @trigger_error(sprintf('Method %s() will have a third `int $priority = 0` argument in version 4.0. Not defining it is deprecated since Symfony 3.2.', __METHOD__), E_USER_DEPRECATED);
95 $this->passConfig->addPass($pass, $type, $priority);
101 * @param string $string The log message
103 * @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
105 public function addLogMessage($string)
107 @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', __METHOD__), E_USER_DEPRECATED);
109 $this->log[] = $string;
115 public function log(CompilerPassInterface $pass, $message)
117 if (false !== strpos($message, "\n")) {
118 $message = str_replace("\n", "\n".\get_class($pass).': ', trim($message));
121 $this->log[] = \get_class($pass).': '.$message;
127 * @return array Log array
129 public function getLog()
135 * Run the Compiler and process all Passes.
137 public function compile(ContainerBuilder $container)
140 foreach ($this->passConfig->getPasses() as $pass) {
141 $pass->process($container);
143 } catch (\Exception $e) {
148 $msg = $prev->getMessage();
150 if ($msg !== $resolvedMsg = $container->resolveEnvPlaceholders($msg, null, $usedEnvs)) {
151 $r = new \ReflectionProperty($prev, 'message');
152 $r->setAccessible(true);
153 $r->setValue($prev, $resolvedMsg);
155 } while ($prev = $prev->getPrevious());
158 $e = new EnvParameterException($usedEnvs, $e);
163 $this->getServiceReferenceGraph()->clear();