|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HelloWordPl\SimpleEntityGeneratorBundle\Lib; |
|
4
|
|
|
|
|
5
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\RenderableInterface; |
|
6
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Config\FileLocator; |
|
8
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\ClassConstructorManager; |
|
9
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\InterfaceManager; |
|
10
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\TestClassManager; |
|
11
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\TestMethodManager; |
|
12
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodGetterManager; |
|
13
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodGetterInterfaceManager; |
|
14
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodGetterBooleanManager; |
|
15
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodGetterBooleanInterfaceManager; |
|
16
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\ClassManager; |
|
17
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodSetterManager; |
|
18
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\MethodSetterInterfaceManager; |
|
19
|
|
|
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Items\PropertyManager; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Sławomir Kania <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class TemplateManager |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var KernelInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $kernel; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* CONSTRUCT |
|
34
|
|
|
* |
|
35
|
|
|
* @param KernelInterface $kernel |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(KernelInterface $kernel) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->kernel = $kernel; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param RenderableInterface $item |
|
44
|
|
|
*/ |
|
45
|
|
|
public function loadAndSetTemplateOnItem(RenderableInterface $item) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->resolveAndSetTemplatePathIfCan($item); |
|
48
|
|
|
$template = null; |
|
|
|
|
|
|
49
|
|
|
if (false === empty($item->getTemplate())) { |
|
50
|
|
|
$template = $item->getTemplate(); |
|
51
|
|
|
} elseif (false === empty($item->getTemplatePath())) { |
|
52
|
|
|
$path = $this->getFileLocator()->locate("@".$item->getTemplatePath()); // may throw exception |
|
53
|
|
|
$template = $this->getFilesManager()->loadContentFromFile($path); |
|
|
|
|
|
|
54
|
|
|
} else { |
|
55
|
|
|
// default template |
|
56
|
|
|
$reflect = new \ReflectionClass($item); |
|
57
|
|
|
$paramName = $reflect->getShortName()."TemplatePath"; |
|
58
|
|
|
|
|
59
|
|
|
if (false === $this->kernel->getContainer()->hasParameter($paramName)) { |
|
60
|
|
|
throw new \RuntimeException(sprintf("No parameter %s set", $paramName)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$path = $this->getFileLocator()->locate("@".$this->kernel->getContainer()->getParameter($paramName)); // may throw exception |
|
64
|
|
|
$template = $this->getFilesManager()->loadContentFromFile($path); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$item->setTemplate($template); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return FilesManager |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getFilesManager() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->kernel->getContainer()->get("seg.files_manager"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return FileLocator |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getFileLocator() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->kernel->getContainer()->get("file_locator"); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param RenderableInterface $item |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function resolveAndSetTemplatePathIfCan(RenderableInterface $item) |
|
90
|
|
|
{ |
|
91
|
|
|
$templatePath = ""; |
|
92
|
|
|
switch (true) { |
|
93
|
|
|
case $item instanceof ClassManager: |
|
94
|
|
|
$templatePath = $item->getClassManagerTemplatePath(); |
|
95
|
|
|
break; |
|
96
|
|
|
case $item instanceof PropertyManager: |
|
97
|
|
|
$templatePath = $item->getPropertyManagerTemplatePath(); |
|
98
|
|
|
break; |
|
99
|
|
|
case $item instanceof ClassConstructorManager: |
|
100
|
|
|
$templatePath = $item->getClassManager()->getClassConstructorManagerTemplatePath(); |
|
101
|
|
|
break; |
|
102
|
|
|
case $item instanceof InterfaceManager: |
|
103
|
|
|
$templatePath = $item->getClassManager()->getInterfaceManagerTemplatePath(); |
|
104
|
|
|
break; |
|
105
|
|
|
case $item instanceof TestClassManager: |
|
106
|
|
|
$templatePath = $item->getClassManager()->getTestClassManagerTemplatePath(); |
|
107
|
|
|
break; |
|
108
|
|
|
case $item instanceof TestMethodManager: |
|
109
|
|
|
$templatePath = $item->getMethod()->getProperty()->getTestClassMethodManagerTemplatePath(); |
|
110
|
|
|
break; |
|
111
|
|
|
case $item instanceof MethodGetterManager: |
|
112
|
|
|
$templatePath = $item->getProperty()->getMethodGetterManagerTemplatePath(); |
|
113
|
|
|
break; |
|
114
|
|
|
case $item instanceof MethodGetterInterfaceManager: |
|
115
|
|
|
$templatePath = $item->getProperty()->getMethodGetterInterfaceManagerTemplatePath(); |
|
116
|
|
|
break; |
|
117
|
|
|
case $item instanceof MethodGetterBooleanManager: |
|
118
|
|
|
$templatePath = $item->getProperty()->getMethodGetterBooleanManagerTemplatePath(); |
|
119
|
|
|
break; |
|
120
|
|
|
case $item instanceof MethodGetterBooleanInterfaceManager: |
|
121
|
|
|
$templatePath = $item->getProperty()->getMethodGetterBooleanInterfaceManageTemplatePath(); |
|
122
|
|
|
break; |
|
123
|
|
|
case $item instanceof MethodSetterInterfaceManager: |
|
124
|
|
|
$templatePath = $item->getProperty()->getMethodSetterInterfaceManagerTemplatePath(); |
|
125
|
|
|
break; |
|
126
|
|
|
case $item instanceof MethodSetterManager: |
|
127
|
|
|
$templatePath = $item->getProperty()->getMethodSetterManagerTemplatePath(); |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (false === empty($templatePath)) { |
|
132
|
|
|
$item->setTemplatePath($templatePath); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.