Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Generator / EntityContentGenerator.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Generator\EntityContentGenerator.
6  */
7
8 namespace Drupal\Console\Generator;
9
10 use Drupal\Console\Core\Generator\Generator;
11 use Drupal\Console\Core\Utils\TwigRenderer;
12 use Drupal\Console\Extension\Manager;
13 use Drupal\Console\Utils\Site;
14
15 class EntityContentGenerator extends Generator
16 {
17     /**
18      * @var Manager
19      */
20     protected $extensionManager;
21
22     /**
23      * @var Site
24      */
25     protected $site;
26
27     /**
28      * @var TwigRenderer
29      */
30     protected $twigrenderer;
31
32     protected $io;
33
34     /**
35      * EntityContentGenerator constructor.
36      *
37      * @param Manager      $extensionManager
38      * @param Site         $site
39      * @param TwigRenderer $twigrenderer
40      */
41     public function __construct(
42         Manager $extensionManager,
43         Site $site,
44         TwigRenderer $twigrenderer
45     ) {
46         $this->extensionManager = $extensionManager;
47         $this->site = $site;
48         $this->twigrenderer = $twigrenderer;
49     }
50
51     public function setIo($io)
52     {
53         $this->io = $io;
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     public function generate(array $parameters)
60     {
61         $module = $parameters['module'];
62         $entity_name = $parameters['entity_name'];
63         $entity_class = $parameters['entity_class'];
64         $bundle_entity_type = $parameters['bundle_entity_type'];
65         $is_translatable = $parameters['is_translatable'];
66         $revisionable = $parameters['revisionable'];
67
68         $moduleInstance = $this->extensionManager->getModule($module);
69         $moduleDir = $moduleInstance->getPath();
70         $modulePath = $moduleDir . '/' . $module;
71         $moduleSourcePath = $moduleInstance->getSourcePath() . '/' . $entity_class;
72         $moduleFormPath = $moduleInstance->getFormPath() . '/' . $entity_class;
73         $moduleEntityPath = $moduleInstance->getEntityPath() . '/' . $entity_class;
74         $moduleTemplatePath = $moduleInstance->getTemplatePath() . '/';
75         $moduleFileName = $modulePath . '.module';
76
77         $this->renderFile(
78             'module/permissions-entity-content.yml.twig',
79             $modulePath . '.permissions.yml',
80             $parameters,
81             FILE_APPEND
82         );
83
84         $this->renderFile(
85             'module/links.menu-entity-content.yml.twig',
86             $modulePath . '.links.menu.yml',
87             $parameters,
88             FILE_APPEND
89         );
90
91         $this->renderFile(
92             'module/links.task-entity-content.yml.twig',
93             $modulePath . '.links.task.yml',
94             $parameters,
95             FILE_APPEND
96         );
97
98         $this->renderFile(
99             'module/links.action-entity-content.yml.twig',
100             $modulePath . '.links.action.yml',
101             $parameters,
102             FILE_APPEND
103         );
104
105         $this->renderFile(
106             'module/src/accesscontrolhandler-entity-content.php.twig',
107             $moduleSourcePath . 'AccessControlHandler.php',
108             $parameters
109         );
110
111         if ($is_translatable) {
112             $this->renderFile(
113                 'module/src/entity-translation-handler.php.twig',
114                 $moduleSourcePath . 'TranslationHandler.php',
115                 $parameters
116             );
117         }
118
119         $this->renderFile(
120             'module/src/Entity/interface-entity-content.php.twig',
121             $moduleEntityPath . 'Interface.php',
122             $parameters
123         );
124
125         $this->renderFile(
126             'module/src/Entity/entity-content.php.twig',
127             $moduleEntityPath . '.php',
128             $parameters
129         );
130
131         $this->renderFile(
132             'module/src/entity-content-route-provider.php.twig',
133             $moduleSourcePath . 'HtmlRouteProvider.php',
134             $parameters
135         );
136
137         $this->renderFile(
138             'module/src/Entity/entity-content-views-data.php.twig',
139             $moduleEntityPath . 'ViewsData.php',
140             $parameters
141         );
142
143         $this->renderFile(
144             'module/src/listbuilder-entity-content.php.twig',
145             $moduleSourcePath . 'ListBuilder.php',
146             $parameters
147         );
148
149         $this->renderFile(
150             'module/src/Entity/Form/entity-settings.php.twig',
151             $moduleFormPath . 'SettingsForm.php',
152             $parameters
153         );
154
155         $this->renderFile(
156             'module/src/Entity/Form/entity-content.php.twig',
157             $moduleFormPath . 'Form.php',
158             $parameters
159         );
160
161         $this->renderFile(
162             'module/src/Entity/Form/entity-content-delete.php.twig',
163             $moduleFormPath . 'DeleteForm.php',
164             $parameters
165         );
166
167         $this->renderFile(
168             'module/entity-content-page.php.twig',
169             $moduleDir . '/' . $entity_name . '.page.inc',
170             $parameters
171         );
172
173         $this->renderFile(
174             'module/templates/entity-html.twig',
175             $moduleTemplatePath . $entity_name . '.html.twig',
176             $parameters
177         );
178
179         if ($revisionable) {
180             $this->renderFile(
181                 'module/src/Entity/Form/entity-content-revision-delete.php.twig',
182                 $moduleFormPath . 'RevisionDeleteForm.php',
183                 $parameters
184             );
185             $this->renderFile(
186                 'module/src/Entity/Form/entity-content-revision-revert-translation.php.twig',
187                 $moduleFormPath . 'RevisionRevertTranslationForm.php',
188                 $parameters
189             );
190             $this->renderFile(
191                 'module/src/Entity/Form/entity-content-revision-revert.php.twig',
192                 $moduleFormPath . 'RevisionRevertForm.php',
193                 $parameters
194             );
195             $this->renderFile(
196                 'module/src/entity-storage.php.twig',
197                 $moduleSourcePath . 'Storage.php',
198                 $parameters
199             );
200             $this->renderFile(
201                 'module/src/interface-entity-storage.php.twig',
202                 $moduleSourcePath . 'StorageInterface.php',
203                 $parameters
204             );
205             $this->renderFile(
206                 'module/src/Controller/entity-controller.php.twig',
207                 $moduleInstance->getControllerPath()  . '/' . $entity_class . 'Controller.php',
208                 $parameters
209             );
210         }
211
212         if ($bundle_entity_type) {
213             $this->renderFile(
214                 'module/templates/entity-with-bundle-content-add-list-html.twig',
215                 $moduleTemplatePath . '/' . str_replace('_', '-', $entity_name) . '-content-add-list.html.twig',
216                 $parameters
217             );
218
219             // Check for hook_theme() in module file and warn ...
220             // Check if the module file exists.
221             if (!file_exists($moduleFileName)) {
222                 $this->renderFile(
223                     'module/module.twig',
224                     $moduleFileName,
225                     [
226                         'machine_name' => $module,
227                         'description' => '',
228                     ]
229                 );
230             }
231             $module_file_contents = file_get_contents($moduleFileName);
232             if (strpos($module_file_contents, 'function ' . $module . '_theme') !== false) {
233                 $this->io->warning(
234                     [
235                     'It looks like you have a hook_theme already declared',
236                     'Please manually merge the two hook_theme() implementations in',
237                         $moduleFileName
238                     ]
239                 );
240             }
241
242             $this->renderFile(
243                 'module/src/Entity/entity-content-with-bundle.theme.php.twig',
244                 $moduleFileName,
245                 $parameters,
246                 FILE_APPEND
247             );
248
249             if (strpos($module_file_contents, 'function ' . $module . '_theme_suggestions_' . $entity_name) !== false) {
250                 $this->io->warning(
251                     [
252                         'It looks like you have a hook_theme_suggestions_HOOK already declared',
253                         'Please manually merge the two hook_theme_suggestions_HOOK() implementations in',
254                         $moduleFileName
255                     ]
256                 );
257             }
258
259             $this->renderFile(
260                 'module/src/Entity/entity-content-with-bundle.theme_hook_suggestions.php.twig',
261                 $moduleFileName,
262                 $parameters,
263                 FILE_APPEND
264             );
265         }
266
267         $content = $this->twigrenderer->render(
268             'module/src/Entity/entity-content.theme.php.twig',
269             $parameters
270         );
271
272
273         //@TODO:
274         /**
275         if ($this->isLearning()) {
276             $this->io->commentBlock(
277                 [
278                     'Add this to your hook_theme:',
279                     $content
280                 ]
281             );
282         }
283         */
284     }
285 }