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();
33 $this->loggingFormatter = new LoggingFormatter();
37 * Returns the PassConfig.
39 * @return PassConfig The PassConfig instance
41 public function getPassConfig()
43 return $this->passConfig;
47 * Returns the ServiceReferenceGraph.
49 * @return ServiceReferenceGraph The ServiceReferenceGraph instance
51 public function getServiceReferenceGraph()
53 return $this->serviceReferenceGraph;
57 * Returns the logging formatter which can be used by compilation passes.
59 * @return LoggingFormatter
61 public function getLoggingFormatter()
63 return $this->loggingFormatter;
67 * Adds a pass to the PassConfig.
69 * @param CompilerPassInterface $pass A compiler pass
70 * @param string $type The type of the pass
71 * @param int $priority Used to sort the passes
73 public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/*, $priority = 0*/)
75 if (func_num_args() >= 3) {
76 $priority = func_get_arg(2);
78 if (__CLASS__ !== get_class($this)) {
79 $r = new \ReflectionMethod($this, __FUNCTION__);
80 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
81 @trigger_error(sprintf('Method %s() will have a third `$priority = 0` argument in version 4.0. Not defining it is deprecated since 3.2.', __METHOD__), E_USER_DEPRECATED);
88 $this->passConfig->addPass($pass, $type, $priority);
94 * @param string $string The log message
96 public function addLogMessage($string)
98 $this->log[] = $string;
104 * @return array Log array
106 public function getLog()
112 * Run the Compiler and process all Passes.
114 * @param ContainerBuilder $container
116 public function compile(ContainerBuilder $container)
119 foreach ($this->passConfig->getPasses() as $pass) {
120 $pass->process($container);
122 } catch (\Exception $e) {
127 $msg = $prev->getMessage();
129 if ($msg !== $resolvedMsg = $container->resolveEnvPlaceholders($msg, null, $usedEnvs)) {
130 $r = new \ReflectionProperty($prev, 'message');
131 $r->setAccessible(true);
132 $r->setValue($prev, $resolvedMsg);
134 } while ($prev = $prev->getPrevious());
137 $e = new EnvParameterException($usedEnvs, $e);