1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace JonnyW\PhantomJs\Procedure; |
11
|
|
|
|
12
|
|
|
use JonnyW\PhantomJs\Cache\CacheInterface; |
13
|
|
|
use JonnyW\PhantomJs\Template\TemplateRendererInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PHP PhantomJs |
17
|
|
|
* |
18
|
|
|
* @author Jon Wenmoth <[email protected]> |
19
|
|
|
*/ |
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
|
6 |
|
public function __construct(ProcedureLoaderInterface $procedureLoader, ProcedureValidatorInterface $procedureValidator, |
72
|
|
|
CacheInterface $cacheHandler, TemplateRendererInterface $renderer) |
73
|
|
|
{ |
74
|
6 |
|
$this->procedureLoader = $procedureLoader; |
75
|
6 |
|
$this->procedureValidator = $procedureValidator; |
76
|
6 |
|
$this->cacheHandler = $cacheHandler; |
77
|
6 |
|
$this->renderer = $renderer; |
78
|
6 |
|
$this->cacheEnabled = true; |
79
|
6 |
|
} |
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
|
42 |
|
public function compile(ProcedureInterface $procedure, InputInterface $input) |
90
|
|
|
{ |
91
|
42 |
|
$cacheKey = sprintf('phantomjs_%s_%s', $input->getType(), md5($procedure->getTemplate())); |
|
|
|
|
92
|
|
|
|
93
|
42 |
|
if ($this->cacheEnabled && $this->cacheHandler->exists($cacheKey)) { |
94
|
31 |
|
$template = $this->cacheHandler->fetch($cacheKey); |
|
|
|
|
95
|
31 |
|
} |
96
|
|
|
|
97
|
42 |
|
if (empty($template)) { |
98
|
|
|
|
99
|
12 |
|
$template = $this->renderer |
100
|
12 |
|
->render($procedure->getTemplate(), array('engine' => $this, 'procedure_type' => $input->getType())); |
|
|
|
|
101
|
|
|
|
102
|
12 |
|
$test = clone $procedure; |
103
|
12 |
|
$test->setTemplate($template); |
104
|
|
|
|
105
|
12 |
|
$compiled = $test->compile($input); |
106
|
|
|
|
107
|
12 |
|
$this->procedureValidator->validate($compiled); |
108
|
|
|
|
109
|
10 |
|
if ($this->cacheEnabled) { |
110
|
9 |
|
$this->cacheHandler->save($cacheKey, $template); |
111
|
9 |
|
} |
112
|
10 |
|
} |
113
|
|
|
|
114
|
40 |
|
$procedure->setTemplate($template); |
115
|
40 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Load partial template. |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
* @param string $name |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
5 |
|
public function load($name) |
125
|
|
|
{ |
126
|
5 |
|
return $this->procedureLoader->loadTemplate($name, 'partial'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Enable cache. |
131
|
|
|
* |
132
|
|
|
* @access public |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
1 |
|
public function enableCache() |
136
|
|
|
{ |
137
|
1 |
|
$this->cacheEnabled = true; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Disable cache. |
142
|
|
|
* |
143
|
|
|
* @access public |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
1 |
|
public function disableCache() |
147
|
|
|
{ |
148
|
1 |
|
$this->cacheEnabled = false; |
149
|
1 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Clear cache. |
153
|
|
|
* |
154
|
|
|
* @access public |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
1 |
|
public function clearCache() |
158
|
|
|
{ |
159
|
1 |
|
$this->cacheHandler->delete('phantomjs_*'); |
160
|
1 |
|
} |
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: