Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Generator/DefaultPagePartGenerator.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 Symfony\Component\HttpKernel\Bundle\BundleInterface;
6
use Symfony\Component\Yaml\Yaml;
7
8
/**
9
 * Generates all classes/files for a new pagepart
10
 */
11
class DefaultPagePartGenerator 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 $sections;
32
33
    /**
34
     * Generate the pagepart.
35
     *
36
     * @param BundleInterface $bundle    The bundle
37
     * @param string          $entity    The entity name
38
     * @param string          $prefix    The database prefix
39
     * @param array           $sections  The page sections
40
     * @param bool            $behatTest If we need to generate a behat test for this pagepart
41
     *
42
     * @throws \RuntimeException
43
     */
44 View Code Duplication
    public function generate(BundleInterface $bundle, $entity, $prefix, array $sections, $behatTest)
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...
45
    {
46
        $this->bundle = $bundle;
47
        $this->entity = $entity;
48
        $this->prefix = $prefix;
49
        $this->sections = $sections;
50
51
        $this->generatePagePartEntity();
52
        if ($entity != 'AbstractPagePart') {
53
            $this->generateFormType();
54
            $this->generateResourceTemplate();
55
            $this->generateSectionConfig();
56
            if ($behatTest) {
57
                $this->generateBehatTest();
58
            }
59
        }
60
    }
61
62
    /**
63
     * Generate the pagepart entity.
64
     */
65
    private function generatePagePartEntity()
66
    {
67
        $params = array(
68
            'bundle' => $this->bundle->getName(),
69
            'namespace' => $this->bundle->getNamespace(),
70
            'pagepart' => $this->entity,
71
            'pagepartname' => str_replace('PagePart', '', $this->entity),
72
            'adminType' => '\\' . $this->bundle->getNamespace(
73
                ) . '\\Form\\PageParts\\' . $this->entity . 'AdminType',
74
            'underscoreName' => strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $this->entity)),
75
            'prefix' => $this->prefix,
76
            'isV4' => $this->isSymfony4(),
77
        );
78
79
        $this->renderSingleFile(
80
            $this->skeletonDir . '/Entity/PageParts/' . $this->entity . '/',
81
            $this->bundle->getPath() . '/Entity/PageParts/',
82
            $this->entity . '.php',
83
            $params,
84
            true
85
        );
86
87
        $this->assistant->writeLine('Generating ' . $this->entity . ' Entity:       <info>OK</info>');
88
    }
89
90
    /**
91
     * Generate the admin form type entity.
92
     */
93
    private function generateFormType()
94
    {
95
        $params = array(
96
            'bundle' => $this->bundle->getName(),
97
            'namespace' => $this->bundle->getNamespace(),
98
            'pagepart' => $this->entity,
99
            'pagepartname' => str_replace('PagePart', '', $this->entity),
100
            'adminType' => '\\' . $this->bundle->getNamespace() . '\\Form\\PageParts\\' . $this->entity . 'AdminType',
101
        );
102
103
        $this->renderSingleFile(
104
            $this->skeletonDir . '/Form/PageParts/' . $this->entity . '/',
105
            $this->bundle->getPath() . '/Form/PageParts/',
106
            $this->entity . 'AdminType.php',
107
            $params,
108
            true
109
        );
110
111
        $this->assistant->writeLine('Generating ' . $this->entity . ' FormType:     <info>OK</info>');
112
    }
113
114
    /**
115
     * Generate the twig template.
116
     */
117
    private function generateResourceTemplate()
118
    {
119
        $params = array(
120
            'pagepart' => strtolower(
121
                    preg_replace('/([a-z])([A-Z])/', '$1-$2', str_ireplace('PagePart', '', $this->entity))
122
                ) . '-pp',
123
        );
124
125
        $this->renderSingleFile(
126
            $this->skeletonDir . '/Resources/views/PageParts/' . $this->entity . '/',
127
            $this->getTemplateDir($this->bundle) . '/PageParts/' . $this->entity . '/',
128
            'view.html.twig',
129
            $params,
130
            true
131
        );
132
133
        $this->renderSingleFile(
134
            $this->skeletonDir . '/Resources/views/PageParts/' . $this->entity . '/',
135
            $this->getTemplateDir($this->bundle) . '/PageParts/' . $this->entity . '/',
136
            'admin-view.html.twig',
137
            $params,
138
            true
139
        );
140
141
        $this->assistant->writeLine('Generating ' . $this->entity . ' template:     <info>OK</info>');
142
    }
143
144
    /**
145
     * Update the page section config files
146
     */
147
    private function generateSectionConfig()
148
    {
149
        if (count($this->sections) > 0) {
150
            $dir = $this->isSymfony4() ? $this->container->getParameter('kernel.project_dir') . '/config/kunstmaancms/pageparts/' : $this->bundle->getPath() . '/Resources/config/pageparts/';
151
            foreach ($this->sections as $section) {
152
                $data = $originalData = Yaml::parse(file_get_contents($dir . $section));
153 View Code Duplication
                if (array_key_exists('kunstmaan_page_part', $data)) {
154
                    $data['types'] = $originalData['kunstmaan_page_part']['pageparts'][substr($section, 0, -4)]['types'];
155
                }
156
157
                if (!array_key_exists('types', $data)) {
158
                    $data['types'] = array();
159
                }
160
                $class = $this->bundle->getNamespace() . '\\Entity\\PageParts\\' . $this->entity;
161
                $found = false;
162
                foreach ($data['types'] as $type) {
163
                    if ($type['class'] == $class) {
164
                        $found = true;
165
                    }
166
                }
167
168
                if (!$found) {
169
                    $data['types'][] = array(
170
                        'name' => str_replace('PagePart', '', $this->entity),
171
                        'class' => $class,
172
                    );
173
                }
174
175 View Code Duplication
                if (array_key_exists('kunstmaan_page_part', $originalData)) {
176
                    $originalData['kunstmaan_page_part']['pageparts'][substr($section, 0, -4)]['types'] = $data['types'];
177
                    $data = $originalData;
178
                }
179
180
                //Sf4 structure of the config file is nested deeper, increase the level when values are inlined
181
                $ymlData = Yaml::dump($data, $this->isSymfony4() ? 5 : 2);
182
                file_put_contents($dir . $section, $ymlData);
183
            }
184
185
            $this->assistant->writeLine('Updating ' . $this->entity . ' section config: <info>OK</info>');
186
        }
187
    }
188
189
    /**
190
     * Generate the admin form type entity.
191
     */
192
    private function generateBehatTest()
193
    {
194
        // TODO
195
196
        $this->assistant->writeLine('Generating behat test : <info>OK</info>');
197
    }
198
}
199