1 | <?php |
||
20 | class ProcedureCompiler implements ProcedureCompilerInterface |
||
21 | { |
||
22 | /** |
||
23 | * Procedure loader |
||
24 | * |
||
25 | * @var \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface |
||
26 | * @access protected |
||
27 | */ |
||
28 | protected $procedureLoader; |
||
29 | |||
30 | /** |
||
31 | * Procedure validator |
||
32 | * |
||
33 | * @var \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface |
||
34 | * @access protected |
||
35 | */ |
||
36 | protected $procedureValidator; |
||
37 | |||
38 | /** |
||
39 | * Cache handler |
||
40 | * |
||
41 | * @var \JonnyW\PhantomJs\Cache\CacheInterface |
||
42 | * @access protected |
||
43 | */ |
||
44 | protected $cacheHandler; |
||
45 | |||
46 | /** |
||
47 | * Renderer |
||
48 | * |
||
49 | * @var \JonnyW\PhantomJs\Template\TemplateRendererInterface |
||
50 | * @access protected |
||
51 | */ |
||
52 | protected $renderer; |
||
53 | |||
54 | /** |
||
55 | * Cache enabled |
||
56 | * |
||
57 | * @var boolean |
||
58 | * @access protected |
||
59 | */ |
||
60 | protected $cacheEnabled; |
||
61 | |||
62 | /** |
||
63 | * Internal constructor |
||
64 | * |
||
65 | * @access public |
||
66 | * @param \JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface $procedureLoader |
||
67 | * @param \JonnyW\PhantomJs\Procedure\ProcedureValidatorInterface $procedureValidator |
||
68 | * @param \JonnyW\PhantomJs\Cache\CacheInterface $cacheHandler |
||
69 | * @param \JonnyW\PhantomJs\Template\TemplateRendererInterface $renderer |
||
70 | */ |
||
71 | 1 | public function __construct(ProcedureLoaderInterface $procedureLoader, ProcedureValidatorInterface $procedureValidator, |
|
72 | CacheInterface $cacheHandler, TemplateRendererInterface $renderer) |
||
73 | { |
||
74 | 1 | $this->procedureLoader = $procedureLoader; |
|
75 | 1 | $this->procedureValidator = $procedureValidator; |
|
76 | 1 | $this->cacheHandler = $cacheHandler; |
|
77 | 1 | $this->renderer = $renderer; |
|
78 | 1 | $this->cacheEnabled = true; |
|
79 | 1 | } |
|
80 | |||
81 | /** |
||
82 | * Compile partials into procedure. |
||
83 | * |
||
84 | * @access public |
||
85 | * @param \JonnyW\PhantomJs\Procedure\ProcedureInterface $procedure |
||
86 | * @param \JonnyW\PhantomJs\Procedure\InputInterface $input |
||
87 | * @return void |
||
88 | */ |
||
89 | 27 | public function compile(ProcedureInterface $procedure, InputInterface $input) |
|
90 | { |
||
91 | 27 | $cacheKey = sprintf('phantomjs_%s_%s', $input->getType(), md5($procedure->getTemplate())); |
|
|
|||
92 | |||
93 | 27 | if ($this->cacheEnabled && $this->cacheHandler->exists($cacheKey)) { |
|
94 | 21 | $template = $this->cacheHandler->fetch($cacheKey); |
|
95 | 21 | } |
|
96 | |||
97 | 27 | if (empty($template)) { |
|
98 | |||
99 | 6 | $template = $this->renderer |
|
100 | 6 | ->render($procedure->getTemplate(), array('engine' => $this, 'procedure_type' => $input->getType())); |
|
101 | |||
102 | 6 | $test = clone $procedure; |
|
103 | 6 | $test->setTemplate($template); |
|
104 | |||
105 | 6 | $compiled = $test->compile($input); |
|
106 | |||
107 | 6 | $this->procedureValidator->validate($compiled); |
|
108 | |||
109 | 5 | if ($this->cacheEnabled) { |
|
110 | 5 | $this->cacheHandler->save($cacheKey, $template); |
|
111 | 5 | } |
|
112 | 5 | } |
|
113 | |||
114 | 26 | $procedure->setTemplate($template); |
|
115 | 26 | } |
|
116 | |||
117 | /** |
||
118 | * Load partial template. |
||
119 | * |
||
120 | * @access public |
||
121 | * @param string $name |
||
122 | * @return string |
||
123 | */ |
||
124 | 3 | public function load($name) |
|
128 | |||
129 | /** |
||
130 | * Enable cache. |
||
131 | * |
||
132 | * @access public |
||
133 | * @return void |
||
134 | */ |
||
135 | public function enableCache() |
||
139 | |||
140 | /** |
||
141 | * Disable cache. |
||
142 | * |
||
143 | * @access public |
||
144 | * @return void |
||
145 | */ |
||
146 | public function disableCache() |
||
150 | |||
151 | /** |
||
152 | * Clear cache. |
||
153 | * |
||
154 | * @access public |
||
155 | * @return void |
||
156 | */ |
||
157 | public function clearCache() |
||
161 | } |
||
162 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: