Completed
Pull Request — master (#2094)
by Sander
50:34 queued 34:10
created

GeneratorBundle/Generator/FormPageGenerator.php (2 issues)

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