|
25
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the WikiTagBundle package. |
|
|
5 |
* |
|
|
6 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace IRI\Bundle\WikiTagBundle\Command; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
|
15 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
16 |
use Symfony\Component\Console\Input\InputArgument; |
|
|
17 |
use Symfony\Component\Console\Input\InputOption; |
|
|
18 |
use Symfony\Component\Console\Input\InputInterface; |
|
|
19 |
use Symfony\Component\Console\Output\OutputInterface; |
|
|
20 |
use Symfony\Component\Console\Output\Output; |
|
|
21 |
use Doctrine\ORM\Tools\SchemaTool; |
|
|
22 |
use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand; |
|
|
23 |
use Symfony\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper; |
|
|
24 |
|
|
|
25 |
/** |
|
|
26 |
* Command to execute the SQL needed to generate the database schema for |
|
|
27 |
* a given entity manager. |
|
|
28 |
* |
|
|
29 |
* This file is a direct adaptation of the Symfony\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand |
|
|
30 |
* |
|
|
31 |
*/ |
|
|
32 |
class CreateSchemaDoctrineCommand extends CreateCommand implements ContainerAwareInterface |
|
|
33 |
{ |
|
|
34 |
protected function configure() |
|
|
35 |
{ |
|
|
36 |
parent::configure(); |
|
|
37 |
|
|
|
38 |
$this |
|
|
39 |
->setName('wikitag:schema:create') |
|
|
40 |
->setDescription('Executes (or dumps) the SQL needed to generate the database schema') |
|
|
41 |
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command') |
|
|
42 |
->setHelp(<<<EOT |
|
|
43 |
The <info>doctrine:schema:create</info> command executes the SQL needed to |
|
|
44 |
generate the database schema for the default entity manager: |
|
|
45 |
|
|
|
46 |
<info>php app/console doctrine:schema:create</info> |
|
|
47 |
|
|
|
48 |
You can also generate the database schema for a specific entity manager: |
|
|
49 |
|
|
|
50 |
<info>php app/console doctrine:schema:create --em=default</info> |
|
|
51 |
|
|
|
52 |
Finally, instead of executing the SQL, you can output the SQL: |
|
|
53 |
|
|
|
54 |
<info>php app/console doctrine:schema:create --dump-sql</info> |
|
|
55 |
EOT |
|
|
56 |
); |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
/** |
|
|
60 |
* @var ContainerInterface |
|
|
61 |
*/ |
|
|
62 |
private $container; |
|
|
63 |
|
|
|
64 |
protected function getContainer() |
|
|
65 |
{ |
|
|
66 |
if (null === $this->container) { |
|
|
67 |
$this->container = $this->getApplication()->getKernel()->getContainer(); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
return $this->container; |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
/** |
|
|
74 |
* @see ContainerAwareInterface::setContainer() |
|
|
75 |
*/ |
|
|
76 |
public function setContainer(ContainerInterface $container = null) |
|
|
77 |
{ |
|
|
78 |
$this->container = $container; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
protected function filterCreateSchema($sqls) |
|
|
83 |
{ |
|
|
84 |
|
|
|
85 |
// get service |
|
|
86 |
$schema_utils = $this->getContainer()->get("wikitag.shema_utils"); |
|
|
87 |
|
|
|
88 |
$res_sqls = $schema_utils->filter_foreign_key($sqls); |
|
|
89 |
$res_sqls = $schema_utils->filter_index_creation($res_sqls); |
|
|
90 |
|
|
|
91 |
|
|
|
92 |
return $res_sqls; |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) |
|
|
96 |
{ |
|
|
97 |
$output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL . PHP_EOL); |
|
|
98 |
$sqls = $schemaTool->getCreateSchemaSql($metadatas); |
|
|
99 |
|
|
|
100 |
$createSchemaSql = $this->filterCreateSchema($sqls); |
|
|
101 |
|
|
|
102 |
if ($input->getOption('dump-sql') === true) { |
|
|
103 |
|
|
|
104 |
$output->write(implode(';' . PHP_EOL, $createSchemaSql) . PHP_EOL); |
|
|
105 |
} else { |
|
|
106 |
$output->write('Creating database schema...' . PHP_EOL); |
|
|
107 |
$emHelper = $this->getHelper('em'); |
|
|
108 |
|
|
|
109 |
$conn = $emHelper->getEntityManager()->getConnection(); |
|
|
110 |
|
|
|
111 |
foreach ($createSchemaSql as $sql) { |
|
|
112 |
$conn->executeQuery($sql); |
|
|
113 |
} |
|
|
114 |
$output->write('Database schema created successfully!' . PHP_EOL); |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
} |
|
|
118 |
|
|
|
119 |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
120 |
{ |
|
|
121 |
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em')); |
|
|
122 |
|
|
|
123 |
parent::execute($input, $output); |
|
|
124 |
} |
|
|
125 |
} |