Completed
Push — master ( 6e09bc...9d1ae5 )
by Andrea
11:59
created

generateFormsDefaultTableValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

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