1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PPI Framework. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2011-2016 Paul Dragoonis <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* |
8
|
|
|
* @link http://www.ppi.io |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PPI\Framework\ServiceManager\Config; |
12
|
|
|
|
13
|
|
|
use PPI\Framework\View\DelegatingEngine; |
14
|
|
|
use PPI\Framework\View\GlobalVariables; |
15
|
|
|
use PPI\Framework\View\Helper\RouterHelper; |
16
|
|
|
use PPI\Framework\View\Helper\SessionHelper; |
17
|
|
|
use PPI\Framework\View\Mustache\Loader\FileSystemLoader as MustacheFileSystemLoader; |
18
|
|
|
use PPI\Framework\View\Mustache\MustacheEngine; |
19
|
|
|
// Helpers |
20
|
|
|
use PPI\Framework\View\Smarty\Extension\AssetsExtension as SmartyAssetsExtension; |
21
|
|
|
use PPI\Framework\View\Smarty\Extension\RouterExtension as SmartyRouterExtension; |
22
|
|
|
use PPI\Framework\View\TemplateLocator; |
23
|
|
|
use PPI\Framework\View\TemplateNameParser; |
24
|
|
|
// Mustache |
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader; |
26
|
|
|
use Symfony\Component\Templating\Helper\AssetsHelper; |
27
|
|
|
// Twig |
28
|
|
|
|
29
|
|
|
// Smarty |
30
|
|
|
use Symfony\Component\Templating\Helper\SlotsHelper; |
31
|
|
|
use Symfony\Component\Templating\PhpEngine; |
32
|
|
|
// Service Manager |
33
|
|
|
use Zend\ServiceManager\ServiceManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ServiceManager configuration for the Templating component. |
37
|
|
|
* |
38
|
|
|
* @author Vítor Brandão <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
class TemplatingConfig extends AbstractConfig |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* Templating engines currently supported: |
44
|
|
|
* - PHP |
45
|
|
|
* - Twig |
46
|
|
|
* - Smarty |
47
|
|
|
* - Mustache. |
48
|
|
|
* |
49
|
|
|
* @param ServiceManager $serviceManager |
50
|
|
|
* |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
*/ |
53
|
|
|
public function configureServiceManager(ServiceManager $serviceManager) |
54
|
|
|
{ |
55
|
|
|
$config = $serviceManager->get('Config'); |
56
|
|
|
$appRootDir = $config['parameters']['app.root_dir']; |
|
|
|
|
57
|
|
|
$appCacheDir = $config['parameters']['app.cache_dir']; |
58
|
|
|
$appCharset = $config['parameters']['app.charset']; |
59
|
|
|
|
60
|
|
|
// The "framework.templating" option is deprecated. Please replace it with "framework.view" |
61
|
|
|
$config = $this->processConfiguration($config); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// these are the templating engines currently supported |
64
|
|
|
// @todo - this needs to come from the app config. |
65
|
|
|
$knownEngineIds = array('php', 'smarty', 'twig', 'mustache', 'plates', 'latte'); |
66
|
|
|
|
67
|
|
|
// these are the engines selected by the user |
68
|
|
|
$engineIds = isset($config['engines']) ? $config['engines'] : array('php'); |
69
|
|
|
|
70
|
|
|
// filter templating engines |
71
|
|
|
$engineIds = array_intersect($engineIds, $knownEngineIds); |
72
|
|
|
if (empty($engineIds)) { |
73
|
|
|
throw new \RuntimeException(sprintf('At least one templating engine should be defined in your app config (in $config[\'view.engines\']). These are the available ones: "%s". Example: "$config[\'templating.engines\'] = array(\'%s\');"', implode('", ', $knownEngineIds), implode("', ", $knownEngineIds))); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* |
77
|
|
|
* Templating Locator. |
78
|
|
|
*/ |
79
|
|
|
$serviceManager->setFactory('templating.locator', function ($serviceManager) use ($appCacheDir) { |
80
|
|
|
return new TemplateLocator( |
81
|
|
|
$serviceManager->get('file_locator'), |
82
|
|
|
$appCacheDir |
83
|
|
|
); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
/* |
87
|
|
|
* Templating Name Parser. |
88
|
|
|
*/ |
89
|
|
|
$serviceManager->setFactory('templating.name_parser', function ($serviceManager) { |
90
|
|
|
return new TemplateNameParser($serviceManager->get('modulemanager')); |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
/* |
94
|
|
|
* Filesystem Loader. |
95
|
|
|
*/ |
96
|
|
|
$serviceManager->setFactory('templating.loader.filesystem', function ($serviceManager) { |
97
|
|
|
return new FileSystemLoader($serviceManager->get('templating.locator')); |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
/* |
101
|
|
|
* Templating assets helper. |
102
|
|
|
*/ |
103
|
|
|
$serviceManager->setFactory('templating.helper.assets', function ($serviceManager) { |
104
|
|
|
return new AssetsHelper($serviceManager->get('request')->getBasePath()); |
|
|
|
|
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
* Templating globals. |
109
|
|
|
*/ |
110
|
|
|
$serviceManager->setFactory('templating.globals', function ($serviceManager) { |
111
|
|
|
return new GlobalVariables($serviceManager->get('servicemanager')); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
/* |
115
|
|
|
* PHP Engine. |
116
|
|
|
* |
117
|
|
|
* TODO: Migrate to Symfony\Bundle\FrameworkBundle\Templating\PhpEngine |
118
|
|
|
*/ |
119
|
|
|
$serviceManager->setFactory('templating.engine.php', function ($serviceManager) use ($appCharset) { |
120
|
|
|
$engine = new PhpEngine( |
121
|
|
|
$serviceManager->get('templating.name_parser'), |
122
|
|
|
$serviceManager->get('templating.loader'), |
123
|
|
|
array( |
124
|
|
|
new SlotsHelper(), |
125
|
|
|
$serviceManager->get('templating.helper.assets'), |
126
|
|
|
new RouterHelper($serviceManager->get('router')), |
127
|
|
|
new SessionHelper($serviceManager->get('session')), |
128
|
|
|
) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$engine->addGlobal('app', $serviceManager->get('templating.globals')); |
132
|
|
|
$engine->setCharset($appCharset); |
133
|
|
|
|
134
|
|
|
return $engine; |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
/* |
138
|
|
|
* Twig Engine |
139
|
|
|
*/ |
140
|
|
|
$serviceManager->setFactory('templating.engine.twig', function ($serviceManager) { |
141
|
|
|
|
142
|
|
|
if (!class_exists('Twig_Environment')) { |
143
|
|
|
throw new \Exception('PPI\Framework\TwigModule not found. Composer require: ppi/twig-module'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$twigEnvironment = new \Twig_Environment( |
147
|
|
|
new \PPI\Framework\View\Twig\Loader\FileSystemLoader( |
148
|
|
|
$serviceManager->get('templating.locator'), |
149
|
|
|
$serviceManager->get('templating.name_parser')) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
// Add some twig extension |
153
|
|
|
$twigEnvironment->addExtension(new \PPI\Framework\View\Twig\Extension\AssetsExtension($serviceManager->get('templating.helper.assets'))); |
154
|
|
|
$twigEnvironment->addExtension(new \PPI\Framework\View\Twig\Extension\RouterExtension($serviceManager->get('router'))); |
155
|
|
|
|
156
|
|
|
return new \PPI\Framework\View\Twig\TwigEngine($twigEnvironment, $serviceManager->get('templating.name_parser'), |
157
|
|
|
$serviceManager->get('templating.locator'), $serviceManager->get('templating.globals')); |
158
|
|
|
}); |
159
|
|
|
|
160
|
|
|
/* |
161
|
|
|
* Smarty Engine. |
162
|
|
|
*/ |
163
|
|
|
$serviceManager->setFactory('templating.engine.smarty', function ($serviceManager) use ($appCacheDir) { |
164
|
|
|
|
165
|
|
|
if (!class_exists('NoiseLabs\Bundle\SmartyBundle\SmartyEngine')) { |
166
|
|
|
throw new \Exception('PPI\Framework\SmartyModule not found. Composer require: ppi/smarty-module'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$cacheDir = $appCacheDir . DIRECTORY_SEPARATOR . 'smarty'; |
170
|
|
|
|
171
|
|
|
$smartyEngine = new \PPI\Framework\View\Smarty\SmartyEngine( |
172
|
|
|
new \Smarty(), |
173
|
|
|
$serviceManager->get('templating.locator'), |
174
|
|
|
$serviceManager->get('templating.name_parser'), |
175
|
|
|
$serviceManager->get('templating.loader'), |
176
|
|
|
array( |
177
|
|
|
'cache_dir' => $cacheDir . DIRECTORY_SEPARATOR . 'cache', |
178
|
|
|
'compile_dir' => $cacheDir . DIRECTORY_SEPARATOR . 'templates_c', |
179
|
|
|
), |
180
|
|
|
$serviceManager->get('templating.globals'), |
181
|
|
|
$serviceManager->get('logger') |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
// Add some SmartyBundle extensions |
185
|
|
|
$smartyEngine->addExtension(new SmartyAssetsExtension($serviceManager->get('templating.helper.assets'))); |
186
|
|
|
$smartyEngine->addExtension(new SmartyRouterExtension($serviceManager->get('router'))); |
187
|
|
|
|
188
|
|
|
return $smartyEngine; |
189
|
|
|
}); |
190
|
|
|
|
191
|
|
|
// Mustache Engine |
192
|
|
|
$serviceManager->setFactory('templating.engine.mustache', function ($serviceManager, $appCacheDir) { |
193
|
|
|
|
194
|
|
|
if (!class_exists('Mustache_Engine')) { |
195
|
|
|
throw new \Exception('PPI\Framework\MustacheModule not found. Composer require: ppi/mustache-module'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$rawMustacheEngine = new \Mustache_Engine(array( |
199
|
|
|
'loader' => new MustacheFileSystemLoader($serviceManager->get('templating.locator'), |
200
|
|
|
$serviceManager->get('templating.name_parser')), |
201
|
|
|
'cache' => $appCacheDir . DIRECTORY_SEPARATOR . 'mustache', |
202
|
|
|
)); |
203
|
|
|
|
204
|
|
|
return new MustacheEngine($rawMustacheEngine, $serviceManager->get('templating.name_parser')); |
205
|
|
|
}); |
206
|
|
|
|
207
|
|
|
/* |
208
|
|
|
* Delegating Engine. |
209
|
|
|
*/ |
210
|
|
|
$serviceManager->setFactory('templating.engine.delegating', function ($serviceManager) use ($engineIds) { |
211
|
|
|
$delegatingEngine = new DelegatingEngine(); |
212
|
|
|
// @todo - lazy load this |
213
|
|
|
foreach ($engineIds as $id) { |
214
|
|
|
$delegatingEngine->addEngine($serviceManager->get('templating.engine.' . $id)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $delegatingEngine; |
218
|
|
|
}); |
219
|
|
|
|
220
|
|
|
$serviceManager->setAlias('templating', 'templating.engine.delegating'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritDoc} |
225
|
|
|
*/ |
226
|
|
|
public function getConfigurationDefaults() |
227
|
|
|
{ |
228
|
|
|
return array('framework' => array( |
229
|
|
|
'view' => array( |
230
|
|
|
'engines' => array('php'), |
231
|
|
|
), |
232
|
|
|
)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritDoc} |
237
|
|
|
*/ |
238
|
|
|
protected function processConfiguration(array $config, ServiceManager $serviceManager = null) |
239
|
|
|
{ |
240
|
|
|
$config = $config['framework']; |
241
|
|
|
if (!isset($config['templating'])) { |
242
|
|
|
$config['templating'] = array(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
if (isset($config['view'])) { |
246
|
|
|
$config['templating'] = $this->mergeConfiguration($config['view'], $config['templating']); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $config['templating']; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.