Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Generator / ModuleGenerator.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Generator\ModuleGenerator.
6  */
7
8 namespace Drupal\Console\Generator;
9
10 use Drupal\Console\Core\Generator\Generator;
11
12 /**
13  * Class ModuleGenerator
14  *
15  * @package Drupal\Console\Generator
16  */
17 class ModuleGenerator extends Generator
18 {
19     /**
20      * @param $module
21      * @param $machineName
22      * @param $dir
23      * @param $description
24      * @param $core
25      * @param $package
26      * @param $moduleFile
27      * @param $featuresBundle
28      * @param $composer
29      * @param $dependencies
30      * @param $test
31      * @param $twigtemplate
32      */
33     public function generate(
34         $module,
35         $machineName,
36         $dir,
37         $description,
38         $core,
39         $package,
40         $moduleFile,
41         $featuresBundle,
42         $composer,
43         $dependencies,
44         $test,
45         $twigtemplate
46     ) {
47         $dir .= '/'.$machineName;
48         if (file_exists($dir)) {
49             if (!is_dir($dir)) {
50                 throw new \RuntimeException(
51                     sprintf(
52                         'Unable to generate the module as the target directory "%s" exists but is a file.',
53                         realpath($dir)
54                     )
55                 );
56             }
57             $files = scandir($dir);
58             if ($files != ['.', '..']) {
59                 throw new \RuntimeException(
60                     sprintf(
61                         'Unable to generate the module as the target directory "%s" is not empty.',
62                         realpath($dir)
63                     )
64                 );
65             }
66             if (!is_writable($dir)) {
67                 throw new \RuntimeException(
68                     sprintf(
69                         'Unable to generate the module as the target directory "%s" is not writable.',
70                         realpath($dir)
71                     )
72                 );
73             }
74         }
75
76         $parameters = [
77           'module' => $module,
78           'machine_name' => $machineName,
79           'type' => 'module',
80           'core' => $core,
81           'description' => $description,
82           'package' => $package,
83           'dependencies' => $dependencies,
84           'test' => $test,
85           'twigtemplate' => $twigtemplate,
86         ];
87
88         $this->renderFile(
89             'module/info.yml.twig',
90             $dir.'/'.$machineName.'.info.yml',
91             $parameters
92         );
93
94         if (!empty($featuresBundle)) {
95             $this->renderFile(
96                 'module/features.yml.twig',
97                 $dir.'/'.$machineName.'.features.yml',
98                 [
99                 'bundle' => $featuresBundle,
100                 ]
101             );
102         }
103
104         if ($moduleFile) {
105             // Generate '.module' file.
106             $this->createModuleFile($dir, $parameters);
107         }
108
109         if ($composer) {
110             $this->renderFile(
111                 'module/composer.json.twig',
112                 $dir.'/'.'composer.json',
113                 $parameters
114             );
115         }
116
117         if ($test) {
118             $this->renderFile(
119                 'module/src/Tests/load-test.php.twig',
120                 $dir . '/tests/src/Functional/' . 'LoadTest.php',
121                 $parameters
122             );
123         }
124         if ($twigtemplate) {
125             // If module file is not created earlier, create now.
126             if (!$moduleFile) {
127                 // Generate '.module' file.
128                 $this->createModuleFile($dir, $parameters);
129             }
130             $this->renderFile(
131                 'module/module-twig-template-append.twig',
132                 $dir .'/' . $machineName . '.module',
133                 $parameters,
134                 FILE_APPEND
135             );
136             $dir .= '/templates/';
137             if (file_exists($dir)) {
138                 if (!is_dir($dir)) {
139                     throw new \RuntimeException(
140                         sprintf(
141                             'Unable to generate the templates directory as the target directory "%s" exists but is a file.',
142                             realpath($dir)
143                         )
144                     );
145                 }
146                 $files = scandir($dir);
147                 if ($files != ['.', '..']) {
148                     throw new \RuntimeException(
149                         sprintf(
150                             'Unable to generate the templates directory as the target directory "%s" is not empty.',
151                             realpath($dir)
152                         )
153                     );
154                 }
155                 if (!is_writable($dir)) {
156                     throw new \RuntimeException(
157                         sprintf(
158                             'Unable to generate the templates directory as the target directory "%s" is not writable.',
159                             realpath($dir)
160                         )
161                     );
162                 }
163             }
164             $this->renderFile(
165                 'module/twig-template-file.twig',
166                 $dir . str_replace("_", "-", $machineName) . '.html.twig',
167                 $parameters
168             );
169         }
170     }
171
172     /**
173      * Generate the '.module' file.
174      *
175      * @param string $dir
176      *   The directory name.
177      * @param array  $parameters
178      *   The parameter array.
179      */
180     protected function createModuleFile($dir, $parameters)
181     {
182         $this->renderFile(
183             'module/module.twig',
184             $dir . '/' . $parameters['machine_name'] . '.module',
185             $parameters
186         );
187     }
188 }