Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

GeneratorBundle/Generator/FormPageGenerator.php (1 issue)

Upgrade to new PHP Analysis Engine

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 Kunstmaan\GeneratorBundle\Helper\GeneratorUtils;
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
8
/**
9
 * Generates all classes/files for a new formpage
10
 */
11
class FormPageGenerator extends KunstmaanGenerator
12
{
13
    /**
14
     * @var BundleInterface
15
     */
16
    private $bundle;
17
18
    /**
19
     * @var string
20
     */
21
    private $entity;
22
23
    /**
24
     * @var string
25
     */
26
    private $prefix;
27
28
    /**
29
     * @var array
30
     */
31
    private $fields;
32
33
    /**
34
     * @var string
35
     */
36
    private $template;
37
38
    /**
39
     * @var array
40
     */
41
    private $sections;
42
43
    /**
44
     * @var array
45
     */
46
    private $parentPages;
47
48
    /**
49
     * @var bool
50
     */
51
    private $generateFormPageParts;
52
53
    /**
54
     * @var string
55
     */
56
    protected $skeletonDir;
57
58
    /**
59
     * Generate the formpage.
60
     *
61
     * @param BundleInterface $bundle                The bundle
62
     * @param string          $entity                The entity name
63
     * @param string          $prefix                The database prefix
64
     * @param array           $fields                The fields
65
     * @param string          $template              The page template
66
     * @param array           $sections              The page sections
67
     * @param array           $parentPages           The parent pages
68
     * @param bool            $generateFormPageParts Boolean to check if form pageparts need to be generated
69
     *
70
     * @throws \RuntimeException
71
     */
72
    public function generate(
73
        BundleInterface $bundle,
74
        $entity,
75
        $prefix,
76
        array $fields,
77
        $template,
78
        array $sections,
79
        array $parentPages,
80
        $generateFormPageParts
81
    ) {
82
        $this->bundle = $bundle;
83
        $this->entity = $entity;
84
        $this->prefix = $prefix;
85
        $this->fields = $fields;
86
        $this->template = $template;
87
        $this->sections = $sections;
88
        $this->parentPages = $parentPages;
89
        $this->skeletonDir = __DIR__.'/../Resources/SensioGeneratorBundle/skeleton/formpage';
90
        $this->generateFormPageParts = $generateFormPageParts;
91
92
        $this->generatePageEntity();
93
        $this->generatePageFormType();
94
        $this->generatePageTemplateConfiguration();
95
        $this->updateParentPages();
96
    }
97
98
    /**
99
     * Generate the page entity.
100
     *
101
     * @throws \RuntimeException
102
     */
103
    private function generatePageEntity()
104
    {
105
        list($entityCode, $entityPath) = $this->generateEntity(
106
            $this->bundle,
107
            $this->entity,
108
            $this->fields,
109
            'Pages',
110
            $this->prefix,
111
            'Kunstmaan\FormBundle\Entity\AbstractFormPage'
112
        );
113
114
        // Add implements HasPageTemplateInterface
115
        $search = 'extends \Kunstmaan\FormBundle\Entity\AbstractFormPage';
116
        $entityCode = str_replace(
117
            $search,
118
            $search . ' implements \Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface',
119
            $entityCode
120
        );
121
122
        // Add extra configuration to the generated entity (for templates, etc)
123
        $params = array(
124
            'bundle' => $this->bundle->getName(),
125
            'page' => $this->entity,
126
            'template' => $this->template,
127
            'sections' => $this->sections,
128
            'adminType' => '\\' . $this->bundle->getNamespace() . '\\Form\\Pages\\' . $this->entity . 'AdminType',
129
            'namespace' => $this->registry->getAliasNamespace($this->bundle->getName()) . '\\Pages\\' . $this->entity,
130
            'isV4' => $this->isSymfony4(),
131
        );
132
133
        $extraCode = $this->render('/Entity/Pages/ExtraFunctions.php', $params);
134
        $defaultTemplate = 'Pages:Common/view.html.twig';
135
        $formPageTemplate = 'Pages\\'.$this->entity.':view.html.twig';
136
        $extraCode = str_replace(
137
            $defaultTemplate,
138
            $formPageTemplate,
139
            $extraCode
140
        );
141
142
        $pos = strrpos($entityCode, '}');
143
        $trimmed = substr($entityCode, 0, $pos);
144
        $entityCode = $trimmed . $extraCode . "\n}";
145
146
        // Write class to filesystem
147
        $this->filesystem->mkdir(dirname($entityPath));
148
        file_put_contents($entityPath, $entityCode);
149
150
        $this->assistant->writeLine('Generating entity : <info>OK</info>');
151
    }
152
153
    /**
154
     * Generate the admin form type entity.
155
     */
156
    private function generatePageFormType()
157
    {
158
        $this->generateEntityAdminType(
159
            $this->bundle,
160
            $this->entity,
161
            'Pages',
162
            $this->fields,
163
            '\Kunstmaan\FormBundle\Form\AbstractFormPageAdminType'
164
        );
165
166
        $this->assistant->writeLine('Generating form type : <info>OK</info>');
167
    }
168
169
    /**
170
     * Generate the page template and pagepart configuration.
171
     */
172
    private function generatePageTemplateConfiguration()
173
    {
174
        $this->copyTemplates();
175
        $this->installDefaultPagePartConfiguration($this->bundle);
176
        $this->copyTemplateConfig();
177
        $this->assistant->writeLine('Generating template configuration : <info>OK</info>');
178
    }
179
180
    /**
181
     * Copy and modify default formPage templates
182
     */
183
    private function copyTemplates()
184
    {
185
        $dirPath = $this->bundle->getPath();
186
        $this->filesystem->copy($this->skeletonDir . '/Resources/views/Pages/FormPage/view.html.twig', $dirPath . '/Resources/views/Pages/'.$this->entity.'/view.html.twig', true);
187
        $this->filesystem->copy($this->skeletonDir . '/Resources/views/Pages/FormPage/pagetemplate.html.twig', $dirPath . '/Resources/views/Pages/'.$this->entity.'/pagetemplate.html.twig', true);
188
        GeneratorUtils::replace('~~~BUNDLE~~~', $this->bundle->getName(), $dirPath . '/Resources/views/Pages/'.$this->entity.'/pagetemplate.html.twig');
189
190
        GeneratorUtils::prepend("{% extends '" . $this->bundle->getName() .":Page:layout.html.twig' %}\n", $dirPath . '/Resources/views/Pages/'.$this->entity.'/view.html.twig');
191
    }
192
193
    /**
194
     * Copy and modify default config files for the pagetemplate and pageparts.
195
     */
196
    private function copyTemplateConfig()
197
    {
198
        $dirPath = $this->bundle->getPath();
199
        $pagepartFile = $dirPath.'/Resources/config/pageparts/'.$this->template.'.yml';
200
        $namespace = $this->generateFormPageParts ? $this->bundle->getNamespace() : 'Kunstmaan\FormBundle';
201
        $this->filesystem->copy($this->skeletonDir.'/Resources/config/pageparts/formpage.yml', $pagepartFile, false);
202
        GeneratorUtils::replace('~~~ENTITY~~~', $this->entity, $pagepartFile);
203
        GeneratorUtils::replace('~~~FORM_BUNDLE~~~', $namespace, $pagepartFile);
204
205
        $pagetemplateFile = $dirPath.'/Resources/config/pagetemplates/'.$this->template.'.yml';
206
        $this->filesystem->copy($this->skeletonDir.'/Resources/config/pagetemplates/formpage.yml', $pagetemplateFile, false);
207
        GeneratorUtils::replace('~~~BUNDLE~~~', $this->bundle->getName(), $pagetemplateFile);
208
        GeneratorUtils::replace('~~~ENTITY~~~', $this->entity, $pagetemplateFile);
209
    }
210
211
    /**
212
     * Update the getPossibleChildTypes function of the parent Page classes
213
     */
214 View Code Duplication
    private function updateParentPages()
0 ignored issues
show
This method seems to be duplicated in your project.

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.

Loading history...
215
    {
216
        $phpCode = "            [\n";
217
        $phpCode .= "                'name' => '" . $this->entity . "',\n";
218
        $phpCode .= "                'class'=> '" .
219
            $this->bundle->getNamespace() .
220
            '\\Entity\\Pages\\' . $this->entity . "'\n";
221
        $phpCode .= '            ],'."\n        ";
222
223
        // When there is a BehatTestPage, we should also allow the new page as sub page
224
        $behatTestPage = $this->bundle->getPath() . '/Entity/Pages/BehatTestPage.php';
225
        if (file_exists($behatTestPage)) {
226
            $this->parentPages[] = $behatTestPage;
227
        }
228
229
        foreach ($this->parentPages as $file) {
230
            $data = file_get_contents($file);
231
            $data = preg_replace(
232
                '/(function\s*getPossibleChildTypes\s*\(\)\s*\{\s*)(return\s*\[|return\s*array\()/',
233
                "$1$2\n$phpCode",
234
                $data
235
            );
236
            file_put_contents($file, $data);
237
        }
238
    }
239
}
240