Completed
Push — master ( d48ef1...47bb75 )
by
unknown
166:45 queued 146:49
created

GeneratorBundle/Generator/PageGenerator.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
7
/**
8
 * Generates all classes/files for a new page
9
 */
10
class PageGenerator extends KunstmaanGenerator
11
{
12
    /**
13
     * @var BundleInterface
14
     */
15
    private $bundle;
16
17
    /**
18
     * @var string
19
     */
20
    private $entity;
21
22
    /**
23
     * @var string
24
     */
25
    private $prefix;
26
27
    /**
28
     * @var array
29
     */
30
    private $fields;
31
32
    /**
33
     * @var string
34
     */
35
    private $template;
36
37
    /**
38
     * @var array
39
     */
40
    private $sections;
41
42
    /**
43
     * @var array
44
     */
45
    private $parentPages;
46
47
    /**
48
     * Generate the page.
49
     *
50
     * @param BundleInterface $bundle      The bundle
51
     * @param string          $entity      The entity name
52
     * @param string          $prefix      The database prefix
53
     * @param array           $fields      The fields
54
     * @param string          $template    The page template
55
     * @param array           $sections    The page sections
56
     * @param array           $parentPages The parent pages
57
     *
58
     * @throws \RuntimeException
59
     */
60 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...
61
        BundleInterface $bundle,
62
        $entity,
63
        $prefix,
64
        array $fields,
65
        $template,
66
        array $sections,
67
        array $parentPages
68
    ) {
69
        $this->bundle      = $bundle;
70
        $this->entity      = $entity;
71
        $this->prefix      = $prefix;
72
        $this->fields      = $fields;
73
        $this->template    = $template;
74
        $this->sections    = $sections;
75
        $this->parentPages = $parentPages;
76
77
        $this->generatePageEntity();
78
        $this->generatePageFormType();
79
        $this->generatePageTemplateConfiguration();
80
        $this->updateParentPages();
81
    }
82
83
    /**
84
     * Generate the page entity.
85
     *
86
     * @throws \RuntimeException
87
     */
88
    private function generatePageEntity()
89
    {
90
        list($entityCode, $entityPath) = $this->generateEntity(
91
            $this->bundle,
92
            $this->entity,
93
            $this->fields,
94
            'Pages',
95
            $this->prefix,
96
            'Kunstmaan\NodeBundle\Entity\AbstractPage'
97
        );
98
99
        // Add implements HasPageTemplateInterface
100
        $search     = 'extends \Kunstmaan\NodeBundle\Entity\AbstractPage';
101
        $entityCode = str_replace(
102
            $search,
103
            $search . ' implements \Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface',
104
            $entityCode
105
        );
106
107
        // Add some extra functions in the generated entity :s
108
        $params    = array(
109
            'bundle'    => $this->bundle->getName(),
110
            'page'      => $this->entity,
111
            'template'  => substr($this->template, 0, strlen($this->template) - 4),
112
            'sections'  => array_map(
113
                function ($val) {
114
                    return substr($val, 0, strlen($val) - 4);
115
                },
116
                $this->sections
117
            ),
118
            'adminType' => '\\' . $this->bundle->getNamespace() . '\\Form\\Pages\\' . $this->entity . 'AdminType',
119
            'namespace' => $this->registry->getAliasNamespace($this->bundle->getName()) . '\\Pages\\' . $this->entity
120
        );
121
        $extraCode = $this->render('/Entity/Pages/ExtraFunctions.php', $params);
122
123
        $pos        = strrpos($entityCode, "\n}");
124
        $trimmed    = substr($entityCode, 0, $pos);
125
        $entityCode = $trimmed."\n\n".$extraCode."\n}\n";
126
127
        // Write class to filesystem
128
        $this->filesystem->mkdir(dirname($entityPath));
129
        file_put_contents($entityPath, $entityCode);
130
131
        $this->assistant->writeLine('Generating entity : <info>OK</info>');
132
    }
133
134
    /**
135
     * Generate the admin form type entity.
136
     */
137
    private function generatePageFormType()
138
    {
139
        $this->generateEntityAdminType(
140
            $this->bundle,
141
            $this->entity,
142
            'Pages',
143
            $this->fields,
144
            '\Kunstmaan\NodeBundle\Form\PageAdminType'
145
        );
146
147
        $this->assistant->writeLine('Generating form type : <info>OK</info>');
148
    }
149
150
    /**
151
     * Generate the page template -and pagepart configuration.
152
     */
153
    private function generatePageTemplateConfiguration()
154
    {
155
        $this->installDefaultPageTemplates($this->bundle);
156
        $this->installDefaultPagePartConfiguration($this->bundle);
157
158
        $this->assistant->writeLine('Generating template configuration : <info>OK</info>');
159
    }
160
161
    /**
162
     * Update the getPossibleChildTypes function of the parent Page classes
163
     */
164 View Code Duplication
    private function updateParentPages()
165
    {
166
        $phpCode = "            array(\n";
167
        $phpCode .= "                'name' => '" . $this->entity . "',\n";
168
        $phpCode .= "                'class'=> '" .
169
            $this->bundle->getNamespace() .
170
            "\\Entity\\Pages\\" . $this->entity . "'\n";
171
        $phpCode .= "            ),";
172
173
        // When there is a BehatTestPage, we should also allow the new page as sub page
174
        $behatTestPage = $this->bundle->getPath() . '/Entity/Pages/BehatTestPage.php';
175
        if (file_exists($behatTestPage)) {
176
            $this->parentPages[] = $behatTestPage;
177
        }
178
179
        foreach ($this->parentPages as $file) {
180
            $data = file_get_contents($file);
181
            $data = preg_replace(
182
                '/(function\s*getPossibleChildTypes\s*\(\)\s*\{\s*return\s*array\s*\()/',
183
                "$1\n$phpCode",
184
                $data
185
            );
186
            file_put_contents($file, $data);
187
        }
188
    }
189
}
190