Yaffs site version 1.1
[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\Extension\Manager;
12 use Drupal\Console\Utils\Site;
13 use Drupal\Console\Core\Utils\TwigRenderer;
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     /**
58      * Generator Entity.
59      *
60      * @param string $module             Module name
61      * @param string $entity_name        Entity machine name
62      * @param string $entity_class       Entity class name
63      * @param string $label              Entity label
64      * @param string $base_path          Base path
65      * @param string $is_translatable    Translation configuration
66      * @param string $bundle_entity_type (Config) entity type acting as bundle
67      * @param bool   $revisionable       Revision configuration
68      */
69     public function generate($module, $entity_name, $entity_class, $label, $base_path, $is_translatable, $bundle_entity_type = null, $revisionable = false)
70     {
71         $parameters = [
72             'module' => $module,
73             'entity_name' => $entity_name,
74             'entity_class' => $entity_class,
75             'label' => $label,
76             'bundle_entity_type' => $bundle_entity_type,
77             'base_path' => $base_path,
78             'is_translatable' => $is_translatable,
79             'revisionable' => $revisionable,
80         ];
81
82         $this->renderFile(
83             'module/permissions-entity-content.yml.twig',
84             $this->extensionManager->getModule($module)->getPath().'/'.$module.'.permissions.yml',
85             $parameters,
86             FILE_APPEND
87         );
88
89         $this->renderFile(
90             'module/links.menu-entity-content.yml.twig',
91             $this->extensionManager->getModule($module)->getPath().'/'.$module.'.links.menu.yml',
92             $parameters,
93             FILE_APPEND
94         );
95
96         $this->renderFile(
97             'module/links.task-entity-content.yml.twig',
98             $this->extensionManager->getModule($module)->getPath().'/'.$module.'.links.task.yml',
99             $parameters,
100             FILE_APPEND
101         );
102
103         $this->renderFile(
104             'module/links.action-entity-content.yml.twig',
105             $this->extensionManager->getModule($module)->getPath().'/'.$module.'.links.action.yml',
106             $parameters,
107             FILE_APPEND
108         );
109
110         $this->renderFile(
111             'module/src/accesscontrolhandler-entity-content.php.twig',
112             $this->extensionManager->getModule($module)->getSourcePath().'/'.$entity_class.'AccessControlHandler.php',
113             $parameters
114         );
115
116         if ($is_translatable) {
117             $this->renderFile(
118                 'module/src/entity-translation-handler.php.twig',
119                 $this->extensionManager->getModule($module)->getSourcePath().'/'.$entity_class.'TranslationHandler.php',
120                 $parameters
121             );
122         }
123
124         $this->renderFile(
125             'module/src/Entity/interface-entity-content.php.twig',
126             $this->extensionManager->getModule($module)->getEntityPath().'/'.$entity_class.'Interface.php',
127             $parameters
128         );
129
130         $this->renderFile(
131             'module/src/Entity/entity-content.php.twig',
132             $this->extensionManager->getModule($module)->getEntityPath().'/'.$entity_class.'.php',
133             $parameters
134         );
135
136         $this->renderFile(
137             'module/src/entity-content-route-provider.php.twig',
138             $this->extensionManager->getModule($module)->getSourcePath().'/'.$entity_class.'HtmlRouteProvider.php',
139             $parameters
140         );
141
142         $this->renderFile(
143             'module/src/Entity/entity-content-views-data.php.twig',
144             $this->extensionManager->getModule($module)->getEntityPath().'/'.$entity_class.'ViewsData.php',
145             $parameters
146         );
147
148         $this->renderFile(
149             'module/src/listbuilder-entity-content.php.twig',
150             $this->extensionManager->getModule($module)->getSourcePath().'/'.$entity_class.'ListBuilder.php',
151             $parameters
152         );
153
154         $this->renderFile(
155             'module/src/Entity/Form/entity-settings.php.twig',
156             $this->extensionManager->getModule($module)->getFormPath().'/'.$entity_class.'SettingsForm.php',
157             $parameters
158         );
159
160         $this->renderFile(
161             'module/src/Entity/Form/entity-content.php.twig',
162             $this->extensionManager->getModule($module)->getFormPath().'/'.$entity_class.'Form.php',
163             $parameters
164         );
165
166         $this->renderFile(
167             'module/src/Entity/Form/entity-content-delete.php.twig',
168             $this->extensionManager->getModule($module)->getFormPath().'/'.$entity_class.'DeleteForm.php',
169             $parameters
170         );
171
172         $this->renderFile(
173             'module/entity-content-page.php.twig',
174             $this->extensionManager->getModule($module)->getPath().'/'.$entity_name.'.page.inc',
175             $parameters
176         );
177
178         $this->renderFile(
179             'module/templates/entity-html.twig',
180             $this->extensionManager->getModule($module)->getTemplatePath().'/'.$entity_name.'.html.twig',
181             $parameters
182         );
183
184         if ($revisionable) {
185             $this->renderFile(
186                 'module/src/Entity/Form/entity-content-revision-delete.php.twig',
187                 $this->extensionManager->getModule($module)->getFormPath() .'/'.$entity_class.'RevisionDeleteForm.php',
188                 $parameters
189             );
190             $this->renderFile(
191                 'module/src/Entity/Form/entity-content-revision-revert-translation.php.twig',
192                 $this->extensionManager->getModule($module)->getFormPath() .'/'.$entity_class.'RevisionRevertTranslationForm.php',
193                 $parameters
194             );
195             $this->renderFile(
196                 'module/src/Entity/Form/entity-content-revision-revert.php.twig',
197                 $this->extensionManager->getModule($module)->getFormPath().'/'.$entity_class.'RevisionRevertForm.php',
198                 $parameters
199             );
200             $this->renderFile(
201                 'module/src/entity-storage.php.twig',
202                 $this->extensionManager->getModule($module)->getSourcePath() .'/'.$entity_class.'Storage.php',
203                 $parameters
204             );
205             $this->renderFile(
206                 'module/src/interface-entity-storage.php.twig',
207                 $this->extensionManager->getModule($module)->getSourcePath() .'/'.$entity_class.'StorageInterface.php',
208                 $parameters
209             );
210             $this->renderFile(
211                 'module/src/Controller/entity-controller.php.twig',
212                 $this->extensionManager->getModule($module)->getControllerPath() .'/'.$entity_class.'Controller.php',
213                 $parameters
214             );
215         }
216
217         if ($bundle_entity_type) {
218             $this->renderFile(
219                 'module/templates/entity-with-bundle-content-add-list-html.twig',
220                 $this->extensionManager->getModule($module)->getTemplatePath().'/'.str_replace('_', '-', $entity_name).'-content-add-list.html.twig',
221                 $parameters
222             );
223
224             // Check for hook_theme() in module file and warn ...
225             $module_filename = $this->extensionManager->getModule($module)->getPath().'/'.$module.'.module';
226             // Check if the module file exists.
227             if (!file_exists($module_filename)) {
228                 $this->renderFile(
229                     'module/module.twig',
230                     $this->extensionManager->getModule($module)->getPath().'/'.$module . '.module',
231                     [
232                         'machine_name' => $module,
233                         'description' => '',
234                     ]
235                 );
236             }
237             $module_file_contents = file_get_contents($module_filename);
238             if (strpos($module_file_contents, 'function ' . $module . '_theme') !== false) {
239                 $this->io->warning(
240                     [
241                     "It looks like you have a hook_theme already declared",
242                     "Please manually merge the two hook_theme() implementations in",
243                     $module_filename
244                     ]
245                 );
246             }
247
248             $this->renderFile(
249                 'module/src/Entity/entity-content-with-bundle.theme.php.twig',
250                 $this->extensionManager->getModule($module)->getPath().'/'.$module.'.module',
251                 $parameters,
252                 FILE_APPEND
253             );
254
255             if (strpos($module_file_contents, 'function ' . $module . '_theme_suggestions_' . $entity_name) !== false) {
256                 $this->io->warning(
257                     [
258                     "It looks like you have a hook_theme_suggestions_HOOK already declared",
259                     "Please manually merge the two hook_theme_suggestions_HOOK() implementations in",
260                     $module_filename
261                     ]
262                 );
263             }
264
265             $this->renderFile(
266                 'module/src/Entity/entity-content-with-bundle.theme_hook_suggestions.php.twig',
267                 $this->extensionManager->getModule($module)->getPath().'/'.$module.'.module',
268                 $parameters,
269                 FILE_APPEND
270             );
271         }
272
273         $content = $this->twigrenderer->render(
274             'module/src/Entity/entity-content.theme.php.twig',
275             $parameters
276         );
277
278
279         //@TODO:
280         /**
281         if ($this->isLearning()) {
282             $this->io->commentBlock(
283                 [
284                     'Add this to your hook_theme:',
285                     $content
286                 ]
287             );
288         }
289         */
290     }
291 }