1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2017 |
4
|
|
|
* |
5
|
|
|
* @category Library |
6
|
|
|
* @package Dwoo\Plugins\Functions |
7
|
|
|
* @author Jordi Boggiano <[email protected]> |
8
|
|
|
* @author David Sanchez <[email protected]> |
9
|
|
|
* @copyright 2008-2013 Jordi Boggiano |
10
|
|
|
* @copyright 2013-2017 David Sanchez |
11
|
|
|
* @license http://dwoo.org/LICENSE Modified BSD License |
12
|
|
|
* @version 1.3.2 |
13
|
|
|
* @date 2017-01-06 |
14
|
|
|
* @link http://dwoo.org/ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Dwoo\Plugins\Functions; |
18
|
|
|
|
19
|
|
|
use Dwoo\Compiler; |
20
|
|
|
use Dwoo\Compilation\Exception as CompilationException; |
21
|
|
|
use Dwoo\ICompilable; |
22
|
|
|
use Dwoo\Plugin; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Checks whether an extended file has been modified, and if so recompiles the current template. This is for internal |
26
|
|
|
* use only, do not use. This software is provided 'as-is', without any express or implied warranty. In no event will |
27
|
|
|
* the authors be held liable for any damages arising from the use of this software. |
28
|
|
|
*/ |
29
|
|
|
class PluginExtendsCheck extends Plugin implements ICompilable |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param Compiler $compiler |
33
|
|
|
* @param $file |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
* @throws CompilationException |
37
|
|
|
*/ |
38
|
|
|
public static function compile(Compiler $compiler, $file) |
39
|
|
|
{ |
40
|
|
|
preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m); |
41
|
|
|
$resource = $m[1]; |
42
|
|
|
$identifier = $m[2]; |
43
|
|
|
|
44
|
|
|
$tpl = $compiler->getDwoo()->templateFactory($resource, $identifier); |
45
|
|
|
|
46
|
|
View Code Duplication |
if ($tpl === null) { |
|
|
|
|
47
|
|
|
throw new CompilationException($compiler, |
48
|
|
|
'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); |
49
|
|
|
} elseif ($tpl === false) { |
50
|
|
|
throw new CompilationException($compiler, |
51
|
|
|
'Load Templates : Resource "' . $resource . '" does not support includes.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; |
55
|
|
|
|
56
|
|
|
$modCheck = $tpl->getIsModifiedCode(); |
57
|
|
|
|
58
|
|
View Code Duplication |
if ($modCheck) { |
|
|
|
|
59
|
|
|
$out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; |
60
|
|
|
} else { |
61
|
|
|
$out .= 'try { |
62
|
|
|
$tpl = $this->templateFactory("' . $resource . '", "' . $identifier . '"); |
63
|
|
|
} catch (Dwoo\Exception $e) { |
64
|
|
|
$this->triggerError(\'Load Templates : Resource <em>' . $resource . '</em> was not added to Dwoo, can not extend <em>' . $identifier . '</em>\', E_USER_WARNING); |
65
|
|
|
} |
66
|
|
|
if ($tpl === null) |
67
|
|
|
$this->triggerError(\'Load Templates : Resource "' . $resource . ':' . $identifier . '" was not found.\', E_USER_WARNING); |
68
|
|
|
elseif ($tpl === false) |
69
|
|
|
$this->triggerError(\'Load Templates : Resource "' . $resource . '" does not support extends.\', E_USER_WARNING); |
70
|
|
|
if ($tpl->getUid() != "' . $tpl->getUid() . '") { ob_end_clean(); return false; }'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $out; |
74
|
|
|
} |
75
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.