d5c01a640fce55a415d04e727482b29f7fa804bb
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / Compiler.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Compiler;
13
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
16
17 /**
18  * This class is used to remove circular dependencies between individual passes.
19  *
20  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21  */
22 class Compiler
23 {
24     private $passConfig;
25     private $log = array();
26     private $loggingFormatter;
27     private $serviceReferenceGraph;
28
29     public function __construct()
30     {
31         $this->passConfig = new PassConfig();
32         $this->serviceReferenceGraph = new ServiceReferenceGraph();
33     }
34
35     /**
36      * Returns the PassConfig.
37      *
38      * @return PassConfig The PassConfig instance
39      */
40     public function getPassConfig()
41     {
42         return $this->passConfig;
43     }
44
45     /**
46      * Returns the ServiceReferenceGraph.
47      *
48      * @return ServiceReferenceGraph The ServiceReferenceGraph instance
49      */
50     public function getServiceReferenceGraph()
51     {
52         return $this->serviceReferenceGraph;
53     }
54
55     /**
56      * Returns the logging formatter which can be used by compilation passes.
57      *
58      * @return LoggingFormatter
59      *
60      * @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
61      */
62     public function getLoggingFormatter()
63     {
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);
66
67             $this->loggingFormatter = new LoggingFormatter();
68         }
69
70         return $this->loggingFormatter;
71     }
72
73     /**
74      * Adds a pass to the PassConfig.
75      *
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
79      */
80     public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/)
81     {
82         if (\func_num_args() >= 3) {
83             $priority = func_get_arg(2);
84         } else {
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);
89                 }
90             }
91
92             $priority = 0;
93         }
94
95         $this->passConfig->addPass($pass, $type, $priority);
96     }
97
98     /**
99      * Adds a log message.
100      *
101      * @param string $string The log message
102      *
103      * @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
104      */
105     public function addLogMessage($string)
106     {
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);
108
109         $this->log[] = $string;
110     }
111
112     /**
113      * @final
114      */
115     public function log(CompilerPassInterface $pass, $message)
116     {
117         if (false !== strpos($message, "\n")) {
118             $message = str_replace("\n", "\n".\get_class($pass).': ', trim($message));
119         }
120
121         $this->log[] = \get_class($pass).': '.$message;
122     }
123
124     /**
125      * Returns the log.
126      *
127      * @return array Log array
128      */
129     public function getLog()
130     {
131         return $this->log;
132     }
133
134     /**
135      * Run the Compiler and process all Passes.
136      */
137     public function compile(ContainerBuilder $container)
138     {
139         try {
140             foreach ($this->passConfig->getPasses() as $pass) {
141                 $pass->process($container);
142             }
143         } catch (\Exception $e) {
144             $usedEnvs = array();
145             $prev = $e;
146
147             do {
148                 $msg = $prev->getMessage();
149
150                 if ($msg !== $resolvedMsg = $container->resolveEnvPlaceholders($msg, null, $usedEnvs)) {
151                     $r = new \ReflectionProperty($prev, 'message');
152                     $r->setAccessible(true);
153                     $r->setValue($prev, $resolvedMsg);
154                 }
155             } while ($prev = $prev->getPrevious());
156
157             if ($usedEnvs) {
158                 $e = new EnvParameterException($usedEnvs, $e);
159             }
160
161             throw $e;
162         } finally {
163             $this->getServiceReferenceGraph()->clear();
164         }
165     }
166 }