Completed
Push — master ( ba3223...b47a39 )
by Luís
17s
created

CreateCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
ccs 0
cts 22
cp 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B executeSchemaCommand() 0 26 3
A configure() 0 6 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Console\Command\SchemaTool;
21
22
use Doctrine\ORM\Tools\SchemaTool;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Style\SymfonyStyle;
27
28
/**
29
 * Command to create the database schema for a set of classes based on their mappings.
30
 *
31
 * @link    www.doctrine-project.org
32
 * @since   2.0
33
 * @author  Benjamin Eberlei <[email protected]>
34
 * @author  Guilherme Blanco <[email protected]>
35
 * @author  Jonathan Wage <[email protected]>
36
 * @author  Roman Borschel <[email protected]>
37
 */
38
class CreateCommand extends AbstractCommand
39
{
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function configure()
44
    {
45
        $this->setName('orm:schema-tool:create')
46
             ->setDescription('Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output')
47
             ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.')
48
             ->setHelp(<<<EOT
49
Processes the schema and either create it directly on EntityManager Storage Connection or generate the SQL output.
50
51
<comment>Hint:</comment> If you have a database with tables that should not be managed
52
by the ORM, you can use a DBAL functionality to filter the tables and sequences down
53
on a global level:
54
55
    \$config->setFilterSchemaAssetsExpression(\$regexp);
56
EOT
57
             );
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui)
64
    {
65
        $dumpSql = true === $input->getOption('dump-sql');
66
67
        if ($dumpSql) {
68
            $sqls = $schemaTool->getCreateSchemaSql($metadatas);
69
            $ui->text('The following SQL statements will be executed:');
70
            $ui->newLine();
71
72
            foreach ($sqls as $sql) {
73
                $ui->text(sprintf('    %s;', $sql));
74
            }
75
76
            return 0;
77
        }
78
79
        $ui->caution('This operation should not be executed in a production environment!');
80
81
        $ui->text('Creating database schema...');
82
        $ui->newLine();
83
84
        $schemaTool->createSchema($metadatas);
85
86
        $ui->success('Database schema created successfully!');
87
88
        return 0;
89
    }
90
}
91