Completed
Push — master ( a316bd...20e459 )
by Andrea
40:10
created

GenerateFormCommand::cleanTemplatePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Finder\Finder;
11
12
class GenerateFormCommand extends ContainerAwareCommand
13
{
14
15
    protected $apppaths;
16
    protected $genhelper;
17
    protected $pammutils;
18
19 2
    protected function configure()
20
    {
21 2
        $this
22 2
                ->setName('pannelloamministrazione:generateformcrud')
23 2
                ->setDescription('Genera le views per il crud')
24 2
                ->setHelp('Genera le views per il crud, <br/>fifree.mwb Fi/CoreBundle default [--schemaupdate]<br/>')
25 2
                ->addArgument('bundlename', InputArgument::REQUIRED, 'Nome del bundle, Fi/CoreBundle')
26 2
                ->addArgument('entityform', InputArgument::REQUIRED, 'Il nome entity del form da creare');
27 2
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        set_time_limit(0);
32
        $this->apppaths = $this->getContainer()->get("pannelloamministrazione.projectpath");
33
34
        $bundlename = $input->getArgument('bundlename');
35
        $entityform = $input->getArgument('entityform');
36
        $fs = new Filesystem();
37
        //Controller
38
        $controlleFile = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR .
39
                $bundlename . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR .
40
                $entityform . 'Controller.php';
41
        $code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform);
42
        $fs->dumpFile($controlleFile, $code);
43
44
        //Routing
45
        $retmsg = $this->generateFormRouting($bundlename, $entityform);
46
        //Twig template (Crea i template per new edit show)
47
        $this->generateFormWiew($bundlename, $entityform, 'edit');
48
        $this->generateFormWiew($bundlename, $entityform, 'index');
49
        $this->generateFormWiew($bundlename, $entityform, 'new');
50
        $appviews = $this->apppaths->getAppPath() . DIRECTORY_SEPARATOR . 'Resources'
51
                . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . strtolower($entityform);
52
        $fs->remove($appviews);
53
54
        $this->generateFormsDefaultTableValues($entityform);
55
56
        return $retmsg;
57
    }
58
59
    private function generateFormRouting($bundlename, $entityform)
60
    {
61
        //Routing del form
62
        $fs = new Filesystem();
63
        $routingFile = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename .
64
                DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' .
65
                DIRECTORY_SEPARATOR . 'routing' . DIRECTORY_SEPARATOR . strtolower($entityform) . '.yml';
66
67
        $code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform);
68
        $fs->dumpFile($routingFile, $code);
69
70
        //Fixed: Adesso questa parte la fa da solo symfony (05/2015)
71
        //Refixed dalla versione 2.8 non lo fa più (04/2016)
72
73
        $dest = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
74
                'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'routing.yml';
75
76
        $routingContext = "\n" . str_replace('/', '', $bundlename) . '_' . $entityform . ': ' . "\n" .
77
                '  resource: "@' . str_replace('/', '', $bundlename) . '/Resources/config/routing/' . strtolower($entityform) . '.yml"' . "\n" .
78
                '  prefix: /' . $entityform . "\n";
79
80
        //Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo
81
        $fh = fopen($dest, 'a');
82
        fwrite($fh, $routingContext);
83
        fclose($fh);
84
        $retmsg = 'Routing ' . $dest . " generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n";
85
86
        return $retmsg;
87
    }
88
89
    private function generateFormWiew($bundlename, $entityform, $view)
90
    {
91
        $fs = new Filesystem();
92
        $folderview = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
93
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR .
94
                $entityform . DIRECTORY_SEPARATOR;
95
        $dest = $folderview . $view . '.html.twig';
96
        $fs->mkdir($folderview);
97
        file_put_contents($dest, "{% include 'FiCoreBundle:Standard:" . $view . ".html.twig' %}");
98
    }
99
100
    private function generateFormsDefaultTableValues($entityform)
101
    {
102
        //Si inserisce il record di default nella tabella permessi
103
        $em = $this->getContainer()->get('doctrine')->getManager();
104
        $ruoloAmm = $em->getRepository('FiCoreBundle:Ruoli')->findOneBy(array('is_superadmin' => true)); //SuperAdmin
105
106
        $newPermesso = new \Fi\CoreBundle\Entity\Permessi();
107
        $newPermesso->setCrud('crud');
108
        $newPermesso->setModulo($entityform);
109
        $newPermesso->setRuoli($ruoloAmm);
110
        $em->persist($newPermesso);
111
        $em->flush();
112
113
        $tabelle = new \Fi\CoreBundle\Entity\Tabelle();
114
        $tabelle->setNometabella($entityform);
115
        $em->persist($tabelle);
116
        $em->flush();
117
    }
118
119
    private function getControllerCode($bundlename, $tabella)
120
    {
121
        $codeTemplate = <<<EOF
122
<?php
123
namespace [bundle]\Controller;
124
125
use Fi\CoreBundle\Controller\FiController;
126
use Symfony\Component\HttpFoundation\Request;
127
use Symfony\Component\HttpFoundation\Response;
128
use Fi\CoreBundle\Controller\Griglia;
129
use [bundle]\Entity\[tabella];
130
use [bundle]\Form\[tabella]Type;
131
132
133
/**
134
* [tabella] controller.
135
*
136
*/
137
138
class [tabella]Controller extends FiController {
139
140
}
141
EOF;
142
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
143
        $code = str_replace('[tabella]', $tabella, $codebundle);
144
145
        return $code;
146
    }
147
148
    private function getRoutingCode($bundlename, $tabella)
149
    {
150
        $codeTemplate = <<<'EOF'
151
[tabella]_container:
152
    path:  /
153
    defaults: { _controller: "[bundle]:[tabella]:index" }
154
155
[tabella]_new:
156
    path:  /new
157
    defaults: { _controller: "[bundle]:[tabella]:new" }
158
159
[tabella]_create:
160
    path:  /create
161
    defaults: { _controller: "[bundle]:[tabella]:create" }
162
    requirements: { methods: post }
163
164
[tabella]_edit:
165
    path:  /{id}/edit
166
    defaults: { _controller: "[bundle]:[tabella]:edit" }
167
168
[tabella]_update:
169
    path:  /{id}/update
170
    defaults: { _controller: "[bundle]:[tabella]:update" }
171
    requirements: { methods: post|put }
172
173
[tabella]_aggiorna:
174
    path:  /aggiorna
175
    defaults: { _controller: "[bundle]:[tabella]:aggiorna" }
176
    requirements: { methods: post|put }
177
178
[tabella]_delete:
179
    path:  /{id}/delete
180
    defaults: { _controller: "[bundle]:[tabella]:delete" }
181
    requirements: { methods: post|delete }
182
183
[tabella]_deletemultiple:
184
    path:  /delete
185
    defaults: { _controller: "[bundle]:[tabella]:delete" }
186
    requirements: { methods: post|delete }
187
188
[tabella]_griglia:
189
    path:  /griglia
190
    defaults: { _controller: "[bundle]:[tabella]:Griglia" }
191
    requirements: { methods: get|post }
192
EOF;
193
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
194
        $code = str_replace('[tabella]', $tabella, $codebundle);
195
196
        return $code;
197
    }
198
}
199