cd49e7b82e17990b3544e90ed79ac00afe45ea52
[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         $this->loggingFormatter = new LoggingFormatter();
34     }
35
36     /**
37      * Returns the PassConfig.
38      *
39      * @return PassConfig The PassConfig instance
40      */
41     public function getPassConfig()
42     {
43         return $this->passConfig;
44     }
45
46     /**
47      * Returns the ServiceReferenceGraph.
48      *
49      * @return ServiceReferenceGraph The ServiceReferenceGraph instance
50      */
51     public function getServiceReferenceGraph()
52     {
53         return $this->serviceReferenceGraph;
54     }
55
56     /**
57      * Returns the logging formatter which can be used by compilation passes.
58      *
59      * @return LoggingFormatter
60      */
61     public function getLoggingFormatter()
62     {
63         return $this->loggingFormatter;
64     }
65
66     /**
67      * Adds a pass to the PassConfig.
68      *
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
72      */
73     public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/*, $priority = 0*/)
74     {
75         if (func_num_args() >= 3) {
76             $priority = func_get_arg(2);
77         } else {
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);
82                 }
83             }
84
85             $priority = 0;
86         }
87
88         $this->passConfig->addPass($pass, $type, $priority);
89     }
90
91     /**
92      * Adds a log message.
93      *
94      * @param string $string The log message
95      */
96     public function addLogMessage($string)
97     {
98         $this->log[] = $string;
99     }
100
101     /**
102      * Returns the log.
103      *
104      * @return array Log array
105      */
106     public function getLog()
107     {
108         return $this->log;
109     }
110
111     /**
112      * Run the Compiler and process all Passes.
113      *
114      * @param ContainerBuilder $container
115      */
116     public function compile(ContainerBuilder $container)
117     {
118         try {
119             foreach ($this->passConfig->getPasses() as $pass) {
120                 $pass->process($container);
121             }
122         } catch (\Exception $e) {
123             $usedEnvs = array();
124             $prev = $e;
125
126             do {
127                 $msg = $prev->getMessage();
128
129                 if ($msg !== $resolvedMsg = $container->resolveEnvPlaceholders($msg, null, $usedEnvs)) {
130                     $r = new \ReflectionProperty($prev, 'message');
131                     $r->setAccessible(true);
132                     $r->setValue($prev, $resolvedMsg);
133                 }
134             } while ($prev = $prev->getPrevious());
135
136             if ($usedEnvs) {
137                 $e = new EnvParameterException($usedEnvs, $e);
138             }
139
140             throw $e;
141         }
142     }
143 }