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\View; |
12
|
|
|
|
13
|
|
|
use PPI\Framework\Module\ModuleManager; |
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser as BaseTemplateNameParser; |
15
|
|
|
use Symfony\Component\Templating\TemplateReferenceInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* TemplateNameParser converts template names from the short notation |
19
|
|
|
* "bundle:section:template.format.engine" to TemplateReferenceInterface |
20
|
|
|
* instances. |
21
|
|
|
* |
22
|
|
|
* @author Fabien Potencier <[email protected]> |
23
|
|
|
* @author Paul Dragoonis <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class TemplateNameParser extends BaseTemplateNameParser |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ModuleManager |
29
|
|
|
*/ |
30
|
|
|
protected $moduleManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param ModuleManager $moduleManager A ModuleManager instance |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ModuleManager $moduleManager) |
38
|
|
|
{ |
39
|
|
|
$this->moduleManager = $moduleManager; |
40
|
|
|
$this->cache = array(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function parse($name) |
47
|
|
|
{ |
48
|
|
|
if ($name instanceof TemplateReferenceInterface) { |
49
|
|
|
return $name; |
50
|
|
|
} elseif (isset($this->cache[$name])) { |
51
|
|
|
return $this->cache[$name]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// normalize name |
55
|
|
|
$name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'))); |
56
|
|
|
|
57
|
|
|
if (false !== strpos($name, '..')) { |
58
|
|
|
throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$parts = explode(':', $name); |
62
|
|
|
if (3 !== count($parts)) { |
63
|
|
|
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$elements = explode('.', $parts[2]); |
67
|
|
|
if (3 > count($elements)) { |
68
|
|
|
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name)); |
69
|
|
|
} |
70
|
|
|
$engine = array_pop($elements); |
71
|
|
|
$format = array_pop($elements); |
72
|
|
|
|
73
|
|
|
$template = new TemplateReference($parts[0], $parts[1], implode('.', $elements), $format, $engine); |
74
|
|
|
|
75
|
|
|
if ($template->get('module')) { |
76
|
|
|
try { |
77
|
|
|
$this->moduleManager->getModule($template->get('module')); |
|
|
|
|
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->cache[$name] = $template; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: