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
|
6 |
|
protected function configure() |
21
|
|
|
{ |
22
|
|
|
$this |
23
|
6 |
|
->setName('pannelloamministrazione:generateformcrud') |
24
|
6 |
|
->setDescription('Genera le views per il crud') |
25
|
6 |
|
->setHelp('Genera le views per il crud, <br/>fifree.mwb Fi/CoreBundle default [--schemaupdate]<br/>') |
26
|
6 |
|
->addArgument('entityform', InputArgument::REQUIRED, 'Il nome entity del form da creare'); |
27
|
6 |
|
} |
28
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
29
|
|
|
{ |
30
|
2 |
|
set_time_limit(0); |
31
|
2 |
|
$this->apppaths = $this->getContainer()->get("pannelloamministrazione.projectpath"); |
|
|
|
|
32
|
2 |
|
$pammutils = $this->getContainer()->get("pannelloamministrazione.utils"); |
|
|
|
|
33
|
|
|
|
34
|
2 |
|
$bundlename = "App"; |
|
|
|
|
35
|
2 |
|
$entityform = $input->getArgument('entityform'); |
36
|
|
|
|
37
|
2 |
|
$phpPath = OsFunctions::getPHPExecutableFromPath(); |
|
|
|
|
38
|
2 |
|
$command = $phpPath . ' ' . $this->apppaths->getConsole() . ' --env=dev make:form '; |
|
|
|
|
39
|
2 |
|
$resultcrud = $pammutils->runCommand($command . $entityform . "Type" . " " . $entityform); |
|
|
|
|
40
|
2 |
|
if ($resultcrud['errcode'] == 0) { |
41
|
2 |
|
$fs = new Filesystem(); |
42
|
|
|
//Controller |
43
|
2 |
|
$controlleFile = $this->apppaths->getSrcPath() . '/Controller/' . $entityform . 'Controller.php'; |
44
|
2 |
|
$code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform); |
|
|
|
|
45
|
2 |
|
$fs->dumpFile($controlleFile, $code); |
46
|
2 |
|
$output->writeln("<info>Creato " . $controlleFile . "</info>"); |
|
|
|
|
47
|
|
|
|
48
|
|
|
//Routing |
49
|
2 |
|
$retmsg = $this->generateFormRouting($bundlename, $entityform); |
50
|
|
|
//Twig template (Crea i template per new edit show) |
51
|
2 |
|
$this->generateFormWiew($bundlename, $entityform, 'edit'); |
52
|
2 |
|
$this->generateFormWiew($bundlename, $entityform, 'index'); |
53
|
2 |
|
$this->generateFormWiew($bundlename, $entityform, 'new'); |
54
|
|
|
|
55
|
2 |
|
$this->generateFormsDefaultTableValues($entityform); |
56
|
2 |
|
$output->writeln("<info>" . $retmsg . "</info>"); |
|
|
|
|
57
|
2 |
|
return 0; |
58
|
|
|
} else { |
59
|
|
|
$output->writeln("<error>" . $resultcrud['errmsg'] . "</error>"); |
|
|
|
|
60
|
|
|
return 1; |
61
|
|
|
} |
62
|
|
|
} |
63
|
2 |
|
private function generateFormRouting($bundlename, $entityform) |
64
|
|
|
{ |
65
|
|
|
//Routing del form |
66
|
2 |
|
$fs = new Filesystem(); |
|
|
|
|
67
|
2 |
|
$routingFile = $this->apppaths->getSrcPath() . '/../config/routes/' . strtolower($entityform) . '.yml'; |
68
|
|
|
|
69
|
2 |
|
$code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform); |
70
|
2 |
|
$fs->dumpFile($routingFile, $code); |
71
|
|
|
|
72
|
|
|
//Fixed: Adesso questa parte la fa da solo symfony (05/2015) |
73
|
|
|
//Refixed dalla versione 2.8 non lo fa più (04/2016) |
74
|
|
|
|
75
|
2 |
|
$dest = $this->apppaths->getSrcPath() . '/../config/routes.yaml'; |
76
|
|
|
|
77
|
2 |
|
$routingContext = str_replace('/', '', $bundlename) . '_' . $entityform . ':' . "\n" . |
78
|
2 |
|
' resource: routes/' . strtolower($entityform) . '.yml' . "\n" . |
79
|
2 |
|
' prefix: /' . $entityform . "\n\n"; |
80
|
|
|
|
81
|
|
|
//Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo |
82
|
2 |
|
$fh = file_get_contents($dest); |
83
|
2 |
|
if ($fh !== false) { |
84
|
2 |
|
file_put_contents($dest, $routingContext . $fh); |
85
|
2 |
|
$retmsg = 'Routing ' . $dest . " generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n"; |
86
|
|
|
} else { |
87
|
|
|
$retmsg = 'Impossibile generare il ruoting automaticamente da pannelloammonistrazionebundle\n'; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return $retmsg; |
91
|
|
|
} |
92
|
2 |
|
private function generateFormWiew($bundlename, $entityform, $view) |
|
|
|
|
93
|
|
|
{ |
94
|
2 |
|
$fs = new Filesystem(); |
|
|
|
|
95
|
2 |
|
$folderview = $this->apppaths->getSrcPath() . '/../templates/' . $entityform . DIRECTORY_SEPARATOR; |
96
|
2 |
|
$dest = $folderview . $view . '.html.twig'; |
|
|
|
|
97
|
2 |
|
$fs->mkdir($folderview); |
98
|
2 |
|
file_put_contents($dest, "{% include 'FiCoreBundle:Standard:" . $view . ".html.twig' %}"); |
99
|
2 |
|
} |
100
|
2 |
|
private function generateFormsDefaultTableValues($entityform) |
101
|
|
|
{ |
102
|
|
|
//Si inserisce il record di default nella tabella permessi |
103
|
2 |
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
|
|
|
|
104
|
2 |
|
$ruoloAmm = $em->getRepository('FiCoreBundle:Ruoli')->findOneBy(array('is_superadmin' => true)); //SuperAdmin |
105
|
|
|
|
106
|
2 |
|
$newPermesso = new \Fi\CoreBundle\Entity\Permessi(); |
107
|
2 |
|
$newPermesso->setCrud('crud'); |
108
|
2 |
|
$newPermesso->setModulo($entityform); |
109
|
2 |
|
$newPermesso->setRuoli($ruoloAmm); |
110
|
2 |
|
$em->persist($newPermesso); |
111
|
2 |
|
$em->flush(); |
112
|
|
|
|
113
|
2 |
|
$tabelle = new \Fi\CoreBundle\Entity\Tabelle(); |
114
|
2 |
|
$tabelle->setNometabella($entityform); |
115
|
2 |
|
$em->persist($tabelle); |
116
|
2 |
|
$em->flush(); |
117
|
2 |
|
} |
118
|
2 |
|
private function getControllerCode($bundlename, $tabella) |
119
|
|
|
{ |
120
|
|
|
$codeTemplate = <<<EOF |
121
|
2 |
|
<?php |
122
|
|
|
namespace [bundle]\Controller; |
123
|
|
|
|
124
|
|
|
use Fi\CoreBundle\Controller\FiController; |
125
|
|
|
use Symfony\Component\HttpFoundation\Request; |
126
|
|
|
use Symfony\Component\HttpFoundation\Response; |
127
|
|
|
use Fi\CoreBundle\Controller\Griglia; |
128
|
|
|
use [bundle]\Entity\[tabella]; |
129
|
|
|
use [bundle]\Form\[tabella]Type; |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* [tabella] controller. |
134
|
|
|
* |
135
|
|
|
*/ |
136
|
|
|
|
137
|
|
|
class [tabella]Controller extends FiController { |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
EOF; |
141
|
2 |
|
$codebundle = str_replace('[bundle]', $bundlename, $codeTemplate); |
|
|
|
|
142
|
2 |
|
$code = str_replace('[tabella]', $tabella, $codebundle); |
|
|
|
|
143
|
|
|
|
144
|
2 |
|
return $code; |
145
|
|
|
} |
146
|
2 |
|
private function getRoutingCode($bundlename, $tabella) |
147
|
|
|
{ |
148
|
|
|
$codeTemplate = <<<'EOF' |
149
|
2 |
|
[tabella]_container: |
150
|
|
|
path: / |
151
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::indexAction' } |
152
|
|
|
|
153
|
|
|
[tabella]_new: |
154
|
|
|
path: /new |
155
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::newAction' } |
156
|
|
|
|
157
|
|
|
[tabella]_create: |
158
|
|
|
path: /create |
159
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::createAction' } |
160
|
|
|
requirements: { methods: post } |
161
|
|
|
|
162
|
|
|
[tabella]_edit: |
163
|
|
|
path: /{id}/edit |
164
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::editAction' } |
165
|
|
|
|
166
|
|
|
[tabella]_update: |
167
|
|
|
path: /{id}/update |
168
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::updateAction' } |
169
|
|
|
requirements: { methods: post|put } |
170
|
|
|
|
171
|
|
|
[tabella]_aggiorna: |
172
|
|
|
path: /aggiorna |
173
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::aggiornaAction' } |
174
|
|
|
requirements: { methods: post|put } |
175
|
|
|
|
176
|
|
|
[tabella]_delete: |
177
|
|
|
path: /{id}/delete |
178
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::deleteAction' } |
179
|
|
|
requirements: { methods: post|delete } |
180
|
|
|
|
181
|
|
|
[tabella]_deletemultiple: |
182
|
|
|
path: /delete |
183
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::deleteAction' } |
184
|
|
|
requirements: { methods: post|delete } |
185
|
|
|
|
186
|
|
|
[tabella]_griglia: |
187
|
|
|
path: /griglia |
188
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::GrigliaAction' } |
189
|
|
|
requirements: { methods: get|post } |
190
|
|
|
EOF; |
191
|
2 |
|
$codebundle = str_replace('[bundle]', $bundlename, $codeTemplate); |
|
|
|
|
192
|
2 |
|
$code = str_replace('[tabella]', $tabella, $codebundle); |
|
|
|
|
193
|
|
|
|
194
|
2 |
|
return $code; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.