Completed
Push — master ( 2fb3cf...1d8c7f )
by Marco
57:33 queued 48:50
created

ConsoleRunner::printCliConfigTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
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;
21
22
use Doctrine\DBAL\Tools\Console as DBALConsole;
23
use Doctrine\ORM\EntityManagerInterface;
24
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
25
use Doctrine\ORM\Version;
26
use Symfony\Component\Console\Application;
27
use Symfony\Component\Console\Helper\HelperSet;
28
29
/**
30
 * Handles running the Console Tools inside Symfony Console context.
31
 */
32
final class ConsoleRunner
33
{
34
    /**
35
     * Create a Symfony Console HelperSet
36
     *
37
     * @param EntityManagerInterface $entityManager
38
     *
39
     * @return HelperSet
40
     */
41
    public static function createHelperSet(EntityManagerInterface $entityManager) : HelperSet
42
    {
43
        return new HelperSet(
44
            [
45
                'db' => new DBALConsole\Helper\ConnectionHelper($entityManager->getConnection()),
46
                'em' => new EntityManagerHelper($entityManager),
47
            ]
48
        );
49
    }
50
51
    /**
52
     * Runs console with the given helper set.
53
     *
54
     * @param \Symfony\Component\Console\Helper\HelperSet  $helperSet
55
     * @param \Symfony\Component\Console\Command\Command[] $commands
56
     *
57
     * @return void
58
     */
59
    public static function run(HelperSet $helperSet, array $commands = []) : void
60
    {
61
        $cli = self::createApplication($helperSet, $commands);
62
        $cli->run();
63
    }
64
65
    /**
66
     * Creates a console application with the given helperset and
67
     * optional commands.
68
     *
69
     * @param \Symfony\Component\Console\Helper\HelperSet $helperSet
70
     * @param array                                       $commands
71
     *
72
     * @return \Symfony\Component\Console\Application
73
     */
74 2
    public static function createApplication(HelperSet $helperSet, array $commands = []) : Application
75
    {
76 2
        $cli = new Application('Doctrine Command Line Interface', Version::VERSION);
77 2
        $cli->setCatchExceptions(true);
78 2
        $cli->setHelperSet($helperSet);
79 2
        self::addCommands($cli);
80 2
        $cli->addCommands($commands);
81
82 2
        return $cli;
83
    }
84
85
    /**
86
     * @param Application $cli
87
     *
88
     * @return void
89
     */
90 2
    public static function addCommands(Application $cli) : void
91
    {
92 2
        $cli->addCommands(
93
            [
94
                // DBAL Commands
95 2
                new DBALConsole\Command\ImportCommand(),
96 2
                new DBALConsole\Command\ReservedWordsCommand(),
97 2
                new DBALConsole\Command\RunSqlCommand(),
98
99
                // ORM Commands
100 2
                new Command\ClearCache\CollectionRegionCommand(),
101 2
                new Command\ClearCache\EntityRegionCommand(),
102 2
                new Command\ClearCache\MetadataCommand(),
103 2
                new Command\ClearCache\QueryCommand(),
104 2
                new Command\ClearCache\QueryRegionCommand(),
105 2
                new Command\ClearCache\ResultCommand(),
106 2
                new Command\SchemaTool\CreateCommand(),
107 2
                new Command\SchemaTool\UpdateCommand(),
108 2
                new Command\SchemaTool\DropCommand(),
109 2
                new Command\EnsureProductionSettingsCommand(),
110 2
                new Command\ConvertDoctrine1SchemaCommand(),
111 2
                new Command\GenerateRepositoriesCommand(),
112 2
                new Command\GenerateEntitiesCommand(),
113 2
                new Command\GenerateProxiesCommand(),
114 2
                new Command\ConvertMappingCommand(),
115 2
                new Command\RunDqlCommand(),
116 2
                new Command\ValidateSchemaCommand(),
117 2
                new Command\InfoCommand(),
118 2
                new Command\MappingDescribeCommand(),
119
            ]
120
        );
121 2
    }
122
123
    public static function printCliConfigTemplate() : void
124
    {
125
        echo <<<'HELP'
126
You are missing a "cli-config.php" or "config/cli-config.php" file in your
127
project, which is required to get the Doctrine Console working. You can use the
128
following sample as a template:
129
130
<?php
131
use Doctrine\ORM\Tools\Console\ConsoleRunner;
132
133
// replace with file to your own project bootstrap
134
require_once 'bootstrap.php';
135
136
// replace with mechanism to retrieve EntityManager in your app
137
$entityManager = GetEntityManager();
138
139
return ConsoleRunner::createHelperSet($entityManager);
140
141
HELP;
142
    }
143
}
144