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