Completed
Push — master ( 1b817e...6c15b7 )
by Jeroen
39:29
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
        );
131
132
        $extraCode = $this->render('/Entity/Pages/ExtraFunctions.php', $params);
133
        $defaultTemplate = 'Pages:Common/view.html.twig';
134
        $formPageTemplate = 'Pages\\'.$this->entity.':view.html.twig';
135
        $extraCode = str_replace(
136
            $defaultTemplate,
137
            $formPageTemplate,
138
            $extraCode
139
        );
140
141
        $pos = strrpos($entityCode, '}');
142
        $trimmed = substr($entityCode, 0, $pos);
143
        $entityCode = $trimmed . $extraCode . "\n}";
144
145
        // Write class to filesystem
146
        $this->filesystem->mkdir(dirname($entityPath));
147
        file_put_contents($entityPath, $entityCode);
148
149
        $this->assistant->writeLine('Generating entity : <info>OK</info>');
150
    }
151
152
    /**
153
     * Generate the admin form type entity.
154
     */
155
    private function generatePageFormType()
156
    {
157
        $this->generateEntityAdminType(
158
            $this->bundle,
159
            $this->entity,
160
            'Pages',
161
            $this->fields,
162
            '\Kunstmaan\FormBundle\Form\AbstractFormPageAdminType'
163
        );
164
165
        $this->assistant->writeLine('Generating form type : <info>OK</info>');
166
    }
167
168
    /**
169
     * Generate the page template and pagepart configuration.
170
     */
171
    private function generatePageTemplateConfiguration()
172
    {
173
        $this->copyTemplates();
174
        $this->installDefaultPagePartConfiguration($this->bundle);
175
        $this->copyTemplateConfig();
176
        $this->assistant->writeLine('Generating template configuration : <info>OK</info>');
177
    }
178
179
    /**
180
     * Copy and modify default formPage templates
181
     */
182
    private function copyTemplates()
183
    {
184
        $dirPath = $this->bundle->getPath();
185
        $this->filesystem->copy($this->skeletonDir . '/Resources/views/Pages/FormPage/view.html.twig', $dirPath . '/Resources/views/Pages/'.$this->entity.'/view.html.twig', true);
186
        $this->filesystem->copy($this->skeletonDir . '/Resources/views/Pages/FormPage/pagetemplate.html.twig', $dirPath . '/Resources/views/Pages/'.$this->entity.'/pagetemplate.html.twig', true);
187
        GeneratorUtils::replace('~~~BUNDLE~~~', $this->bundle->getName(), $dirPath . '/Resources/views/Pages/'.$this->entity.'/pagetemplate.html.twig');
188
189
        GeneratorUtils::prepend("{% extends '" . $this->bundle->getName() .":Page:layout.html.twig' %}\n", $dirPath . '/Resources/views/Pages/'.$this->entity.'/view.html.twig');
190
    }
191
192
    /**
193
     * Copy and modify default config files for the pagetemplate and pageparts.
194
     */
195
    private function copyTemplateConfig()
196
    {
197
        $dirPath = $this->bundle->getPath();
198
        $pagepartFile = $dirPath.'/Resources/config/pageparts/'.$this->template.'.yml';
199
        $namespace = $this->generateFormPageParts ? $this->bundle->getNamespace() : 'Kunstmaan\FormBundle';
200
        $this->filesystem->copy($this->skeletonDir.'/Resources/config/pageparts/formpage.yml', $pagepartFile, false);
201
        GeneratorUtils::replace('~~~ENTITY~~~', $this->entity, $pagepartFile);
202
        GeneratorUtils::replace('~~~FORM_BUNDLE~~~', $namespace, $pagepartFile);
203
204
        $pagetemplateFile = $dirPath.'/Resources/config/pagetemplates/'.$this->template.'.yml';
205
        $this->filesystem->copy($this->skeletonDir.'/Resources/config/pagetemplates/formpage.yml', $pagetemplateFile, false);
206
        GeneratorUtils::replace('~~~BUNDLE~~~', $this->bundle->getName(), $pagetemplateFile);
207
        GeneratorUtils::replace('~~~ENTITY~~~', $this->entity, $pagetemplateFile);
208
    }
209
210
    /**
211
     * Update the getPossibleChildTypes function of the parent Page classes
212
     */
213 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...
214
    {
215
        $phpCode = "            array(\n";
216
        $phpCode .= "                'name' => '" . $this->entity . "',\n";
217
        $phpCode .= "                'class'=> '" .
218
            $this->bundle->getNamespace() .
219
            '\\Entity\\Pages\\' . $this->entity . "'\n";
220
        $phpCode .= '            ),';
221
222
        // When there is a BehatTestPage, we should also allow the new page as sub page
223
        $behatTestPage = $this->bundle->getPath() . '/Entity/Pages/BehatTestPage.php';
224
        if (file_exists($behatTestPage)) {
225
            $this->parentPages[] = $behatTestPage;
226
        }
227
228
        foreach ($this->parentPages as $file) {
229
            $data = file_get_contents($file);
230
            $data = preg_replace(
231
                '/(function\s*getPossibleChildTypes\s*\(\)\s*\{\s*return\s*array\s*\()/',
232
                "$1\n$phpCode",
233
                $data
234
            );
235
            file_put_contents($file, $data);
236
        }
237
    }
238
}
239