Skip to content

Commit e34147e

Browse files
committed
Handle Wire404Exceptions thrown in controllers
If a controller throws a 404WireException, make sure that the $page object for the template engine is updated to the actual 404 page object. Otherwise, the page object passed to a template engine still points to the page being active when throwing the 404 exception.
1 parent 58bb709 commit e34147e

1 file changed

Lines changed: 53 additions & 29 deletions

File tree

TemplateEngineFactory.module.php

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
*
1616
* @author Stefan Wanzenried <stefan.wanzenried@gmail.com>
1717
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
18-
* @version 1.1.1
18+
* @version 1.1.2
1919
*/
2020
class TemplateEngineFactory extends WireData implements Module, ConfigurableModule
2121
{
2222

2323
/**
2424
* @var array
2525
*/
26-
protected static $default_config = array(
26+
protected static $defaultConfig = array(
2727
'engine' => '',
2828
'api_var' => 'view',
2929
'api_var_factory' => 'factory',
@@ -34,12 +34,12 @@ class TemplateEngineFactory extends WireData implements Module, ConfigurableModu
3434
/**
3535
* @var array
3636
*/
37-
protected $installed_engines;
37+
protected $installedEngines;
3838

3939

4040
public function __construct()
4141
{
42-
foreach (self::$default_config as $k => $v) {
42+
foreach (self::$defaultConfig as $k => $v) {
4343
$this->set($k, $v);
4444
}
4545
}
@@ -79,6 +79,7 @@ public function ready()
7979
}
8080
$this->wire($this->get('api_var'), $engine);
8181
$this->addHookAfter('Page::render', $this, 'hookRender', array('priority' => '100.01'));
82+
$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'hookPageNotFound');
8283
// If the engine supports caching, attach hooks to clear the cache when saving/deleting pages
8384
if (in_array('TemplateEngineCache', class_implements($engine))) {
8485
$this->wire('pages')->addHookAfter('save', $this, 'hookClearCache');
@@ -91,6 +92,7 @@ public function ready()
9192
* @param string $chunk_file The chunk file to load, relative to site/templates/ without file extension
9293
* @param array $params Additional parameters that are passed to the chunk
9394
* @param string $template_file The template (view) that should be used to render the chunk
95+
* @throws WireException
9496
* @return TemplateEngineChunk
9597
*/
9698
public function chunk($chunk_file, array $params = array(), $template_file = '')
@@ -120,6 +122,27 @@ public function hookRender(HookEvent $event)
120122
}
121123

122124

125+
/**
126+
* Hook executed before ProcessPageView::pageNotFound().
127+
* If controllers manually throw a Wire404Exception() we need to make sure that
128+
* the current active template engine uses the correct $page object.
129+
*
130+
* @param HookEvent $event
131+
*/
132+
public function hookPageNotFound(HookEvent $event)
133+
{
134+
$pageNotFoundId = $this->wire('config')->http404PageID;
135+
if (!$pageNotFoundId) {
136+
return;
137+
}
138+
$page = $this->wire('pages')->get($pageNotFoundId);
139+
if (!$page->id) {
140+
return;
141+
}
142+
$this->instance($page->template->name, true);
143+
}
144+
145+
123146
/**
124147
* Method executed after saving or deleting a page, always clear complete cache
125148
*
@@ -182,17 +205,17 @@ public function unregisterEngine(TemplateEngine $engine)
182205
*/
183206
public function getInstalledEngines()
184207
{
185-
if (is_array($this->installed_engines)) {
186-
return $this->installed_engines;
208+
if (is_array($this->installedEngines)) {
209+
return $this->installedEngines;
187210
}
188-
$this->installed_engines = array();
211+
$this->installedEngines = array();
189212
foreach ($this->get('registered_engines') as $class => $title) {
190213
if ($this->wire('modules')->isInstalled($class)) {
191-
$this->installed_engines[$class] = $title;
214+
$this->installedEngines[$class] = $title;
192215
}
193216
}
194217

195-
return $this->installed_engines;
218+
return $this->installedEngines;
196219
}
197220

198221

@@ -203,11 +226,11 @@ public function getInstalledEngines()
203226
*
204227
* @param string $class Class name of engine
205228
* @param string $filename Filename of template file (with or without suffix)
206-
* @param bool $as_api_var Set to true to interact with the given template file over the $view API variable
207-
* @return TemplateEngine|null
229+
* @param bool $setApiVariable Set to true to interact with the given template file over the $view API variable
208230
* @throws WireException
231+
* @return TemplateEngine|null
209232
*/
210-
public function getInstanceById($class, $filename = '', $as_api_var = false)
233+
public function getInstanceById($class, $filename = '', $setApiVariable = false)
211234
{
212235
$installed = $this->getInstalledEngines();
213236
if (!in_array($class, array_keys($installed))) {
@@ -217,15 +240,15 @@ public function getInstanceById($class, $filename = '', $as_api_var = false)
217240
$engine = new $class($filename);
218241
if (!$filename) {
219242
// No filename given, either use global template file or template file of current controller
220-
$global_template = $engine->getConfig('global_template');
221-
$template = ($global_template) ? $global_template : $this->wire('page')->template->name;
243+
$globalTemplate = $engine->getConfig('global_template');
244+
$template = ($globalTemplate) ? $globalTemplate : $this->wire('page')->template->name;
222245
$engine->setFilename($template);
223246
}
224247
if (!is_file($engine->getTemplatesPath() . $engine->getFilename())) {
225248
return null;
226249
}
227250
$engine->initEngine();
228-
if ($as_api_var) {
251+
if ($setApiVariable) {
229252
$this->wire($this->get('api_var'), $engine);
230253
}
231254

@@ -237,34 +260,37 @@ public function getInstanceById($class, $filename = '', $as_api_var = false)
237260
* Get an instance of the current active TemplateEngine module with a given filename
238261
*
239262
* @param string $filename Filename of template file (with or without suffix)
240-
* @param bool $as_api_var Set to true to interact with the given template file over the API variable
263+
* @param bool $setApiVariable Set to true to interact with the given template file over the API variable
264+
* @throws WireException
241265
* @return TemplateEngine|null
242266
*/
243-
public function getInstanceByFilename($filename, $as_api_var = false)
267+
public function getInstanceByFilename($filename, $setApiVariable = false)
244268
{
245-
return $this->getInstanceById($this->get('engine'), $filename, $as_api_var);
269+
return $this->getInstanceById($this->get('engine'), $filename, $setApiVariable);
246270
}
247271

248272

249273
/**
250274
* @param string $filename Filename of template file (with or without suffix)
251-
* @param bool $as_api_var Set to true to interact with the given template file over the API variable
275+
* @param bool $setApiVariable Set to true to interact with the given template file over the API variable
276+
* @throws WireException
252277
* @return TemplateEngine|null
253278
*/
254-
public function instance($filename, $as_api_var = false)
279+
public function instance($filename, $setApiVariable = false)
255280
{
256-
return $this->getInstanceByFilename($filename, $as_api_var);
281+
return $this->getInstanceByFilename($filename, $setApiVariable);
257282
}
258283

259284

260285
/**
261286
* @param string $filename Filename of template file (with or without suffix)
262-
* @param bool $as_api_var Set to true to interact with the given template file over the API variable
287+
* @param bool $setApiVariable Set to true to interact with the given template file over the API variable
288+
* @throws WireException
263289
* @return null|TemplateEngine
264290
*/
265-
public function load($filename, $as_api_var = false)
291+
public function load($filename, $setApiVariable = false)
266292
{
267-
return $this->getInstanceByFilename($filename, $as_api_var);
293+
return $this->getInstanceByFilename($filename, $setApiVariable);
268294
}
269295

270296

@@ -282,11 +308,9 @@ public static function getModuleInfo()
282308
{
283309
return array(
284310
'title' => 'Template Engine Factory',
285-
'version' => 111,
311+
'version' => 112,
286312
'author' => 'Stefan Wanzenried',
287-
'summary' => 'This module aims to separate logic from markup.' .
288-
'Turns ProcessWire templates into controllers which can interact over a new API ' .
289-
'variable to template engines like the native ProcessWire TemplateFile class or Smarty/Twig.',
313+
'summary' => 'Provides ProcessWire integration for various template engines such as Twig or Smarty. ',
290314
'href' => 'https://processwire.com/talk/topic/6833-module-templateenginefactory/',
291315
'singular' => true,
292316
'autoload' => true,
@@ -305,7 +329,7 @@ public static function getModuleConfigInputfields(array $data)
305329
{
306330
$modules = wire('modules');
307331
$wrapper = new InputfieldWrapper();
308-
$data = array_merge(self::$default_config, $data);
332+
$data = array_merge(self::$defaultConfig, $data);
309333

310334
/** @var InputfieldSelect $f */
311335
$engines = $modules->get('TemplateEngineFactory')->getInstalledEngines();

0 commit comments

Comments
 (0)