These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\GeneratorBundle\Generator; |
||
4 | |||
5 | use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
||
6 | |||
7 | /** |
||
8 | * Generates all classes/files for a new page |
||
9 | */ |
||
10 | class PageGenerator extends KunstmaanGenerator |
||
11 | { |
||
12 | /** |
||
13 | * @var BundleInterface |
||
14 | */ |
||
15 | private $bundle; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $entity; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $prefix; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $fields; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $template; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $sections; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $parentPages; |
||
46 | |||
47 | /** |
||
48 | * Generate the page. |
||
49 | * |
||
50 | * @param BundleInterface $bundle The bundle |
||
51 | * @param string $entity The entity name |
||
52 | * @param string $prefix The database prefix |
||
53 | * @param array $fields The fields |
||
54 | * @param string $template The page template |
||
55 | * @param array $sections The page sections |
||
56 | * @param array $parentPages The parent pages |
||
57 | * |
||
58 | * @throws \RuntimeException |
||
59 | */ |
||
60 | public function generate( |
||
61 | BundleInterface $bundle, |
||
62 | $entity, |
||
63 | $prefix, |
||
64 | array $fields, |
||
65 | $template, |
||
66 | array $sections, |
||
67 | array $parentPages |
||
68 | ) { |
||
69 | $this->bundle = $bundle; |
||
70 | $this->entity = $entity; |
||
71 | $this->prefix = $prefix; |
||
72 | $this->fields = $fields; |
||
73 | $this->template = $template; |
||
74 | $this->sections = $sections; |
||
75 | $this->parentPages = $parentPages; |
||
76 | |||
77 | $this->generatePageEntity(); |
||
78 | $this->generatePageFormType(); |
||
79 | $this->generatePageTemplateConfiguration(); |
||
80 | $this->updateParentPages(); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Generate the page entity. |
||
85 | * |
||
86 | * @throws \RuntimeException |
||
87 | */ |
||
88 | private function generatePageEntity() |
||
89 | { |
||
90 | list($entityCode, $entityPath) = $this->generateEntity( |
||
91 | $this->bundle, |
||
92 | $this->entity, |
||
93 | $this->fields, |
||
94 | 'Pages', |
||
95 | $this->prefix, |
||
96 | 'Kunstmaan\NodeBundle\Entity\AbstractPage' |
||
97 | ); |
||
98 | |||
99 | // Add implements HasPageTemplateInterface |
||
100 | $search = 'extends \Kunstmaan\NodeBundle\Entity\AbstractPage'; |
||
101 | $entityCode = str_replace( |
||
102 | $search, |
||
103 | $search . ' implements \Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface', |
||
104 | $entityCode |
||
105 | ); |
||
106 | |||
107 | // Add some extra functions in the generated entity :s |
||
108 | $params = array( |
||
109 | 'bundle' => $this->bundle->getName(), |
||
110 | 'page' => $this->entity, |
||
111 | 'template' => substr($this->template, 0, strpos($this->template, '.')), |
||
112 | 'sections' => array_map( |
||
113 | function ($val) { |
||
114 | return substr($val, 0, strpos($val, '.')); |
||
115 | }, |
||
116 | $this->sections |
||
117 | ), |
||
118 | 'adminType' => '\\' . $this->bundle->getNamespace() . '\\Form\\Pages\\' . $this->entity . 'AdminType', |
||
119 | 'namespace' => $this->registry->getAliasNamespace($this->bundle->getName()) . '\\Pages\\' . $this->entity, |
||
120 | 'isV4' => $this->isSymfony4(), |
||
121 | ); |
||
122 | $extraCode = $this->render('/Entity/Pages/ExtraFunctions.php', $params); |
||
123 | |||
124 | $pos = strrpos($entityCode, "\n}"); |
||
125 | $trimmed = substr($entityCode, 0, $pos); |
||
126 | $entityCode = $trimmed."\n\n".$extraCode."\n}\n"; |
||
127 | |||
128 | // Write class to filesystem |
||
129 | $this->filesystem->mkdir(dirname($entityPath)); |
||
130 | file_put_contents($entityPath, $entityCode); |
||
131 | |||
132 | $this->assistant->writeLine('Generating entity : <info>OK</info>'); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Generate the admin form type entity. |
||
137 | */ |
||
138 | private function generatePageFormType() |
||
139 | { |
||
140 | $this->generateEntityAdminType( |
||
141 | $this->bundle, |
||
142 | $this->entity, |
||
143 | 'Pages', |
||
144 | $this->fields, |
||
145 | '\Kunstmaan\NodeBundle\Form\PageAdminType' |
||
146 | ); |
||
147 | |||
148 | $this->assistant->writeLine('Generating form type : <info>OK</info>'); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Generate the page template -and pagepart configuration. |
||
153 | */ |
||
154 | private function generatePageTemplateConfiguration() |
||
155 | { |
||
156 | $this->installDefaultPageTemplates($this->bundle); |
||
157 | $this->installDefaultPagePartConfiguration($this->bundle); |
||
158 | |||
159 | $this->assistant->writeLine('Generating template configuration : <info>OK</info>'); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Update the getPossibleChildTypes function of the parent Page classes |
||
164 | */ |
||
165 | View Code Duplication | private function updateParentPages() |
|
0 ignored issues
–
show
|
|||
166 | { |
||
167 | $phpCode = " [\n"; |
||
168 | $phpCode .= " 'name' => '" . $this->entity . "',\n"; |
||
169 | $phpCode .= " 'class'=> '" . |
||
170 | $this->bundle->getNamespace() . |
||
171 | '\\Entity\\Pages\\' . $this->entity . "'\n"; |
||
172 | $phpCode .= ' ],'."\n "; |
||
173 | |||
174 | // When there is a BehatTestPage, we should also allow the new page as sub page |
||
175 | $behatTestPage = $this->bundle->getPath() . '/Entity/Pages/BehatTestPage.php'; |
||
176 | if (file_exists($behatTestPage)) { |
||
177 | $this->parentPages[] = $behatTestPage; |
||
178 | } |
||
179 | |||
180 | foreach ($this->parentPages as $file) { |
||
181 | $data = file_get_contents($file); |
||
182 | $data = preg_replace( |
||
183 | '/(function\s*getPossibleChildTypes\s*\(\)\s*\{\s*)(return\s*\[|return\s*array\()/', |
||
184 | "$1$2\n$phpCode", |
||
185 | $data |
||
186 | ); |
||
187 | file_put_contents($file, $data); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 |
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.