Completed
Push — master ( 928a65...13f514 )
by Tom
12s
created

Module::getControllerConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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 DoctrineORMModule;
21
22
use Symfony\Component\Console\Helper\DialogHelper;
23
use Symfony\Component\Console\Helper\QuestionHelper;
24
use Symfony\Component\Console\Input\ArgvInput;
25
use Symfony\Component\Console\Input\InputOption;
26
use Zend\ModuleManager\Feature\ControllerProviderInterface;
27
use Zend\ModuleManager\Feature\ConfigProviderInterface;
28
use Zend\ModuleManager\Feature\InitProviderInterface;
29
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
30
use Zend\ModuleManager\ModuleManagerInterface;
31
use Zend\EventManager\EventInterface;
32
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
33
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
34
use Zend\Stdlib\ArrayUtils;
35
36
/**
37
 * Base module for Doctrine ORM.
38
 *
39
 * @license MIT
40
 * @link    www.doctrine-project.org
41
 * @author  Kyle Spraggs <[email protected]>
42
 * @author  Marco Pivetta <[email protected]>
43
 */
44
class Module implements
45
    ControllerProviderInterface,
46
    ConfigProviderInterface,
47
    InitProviderInterface,
48
    DependencyIndicatorInterface
49
{
50
    /**
51
     * {@inheritDoc}
52
     */
53 72
    public function init(ModuleManagerInterface $manager)
54
    {
55 72
        $events = $manager->getEventManager();
56
        // Initialize logger collector once the profiler is initialized itself
57 72
        $events->attach(
58 72
            'profiler_init',
59
            function () use ($manager) {
60
                $manager->getEvent()->getParam('ServiceManager')->get('doctrine.sql_logger_collector.orm_default');
0 ignored issues
show
Bug introduced by
The method getEvent() does not exist on Zend\ModuleManager\ModuleManagerInterface. Did you maybe mean getEventManager()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
            }
62 72
        );
63 72
        $events->getSharedManager()->attach('doctrine', 'loadCli.post', array($this, 'initializeConsole'));
64 72
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 72
    public function getConfig()
70
    {
71 72
        return include __DIR__ . '/../../config/module.config.php';
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 72
    public function getControllerConfig()
78
    {
79 72
        return include __DIR__ . '/../../config/controllers.config.php';
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 72
    public function getModuleDependencies()
86
    {
87 72
        return array('DoctrineModule');
88
    }
89
90
    /**
91
     * Initializes the console with additional commands from the ORM, DBAL and (optionally) DBAL\Migrations
92
     *
93
     * @param \Zend\EventManager\EventInterface $event
94
     *
95
     * @return void
96
     */
97 18
    public function initializeConsole(EventInterface $event)
98
    {
99
        /* @var $cli \Symfony\Component\Console\Application */
100 18
        $cli            = $event->getTarget();
101
        /* @var $serviceLocator \Zend\ServiceManager\ServiceLocatorInterface */
102 18
        $serviceLocator = $event->getParam('ServiceManager');
103
104
        $commands = array(
105 18
            'doctrine.dbal_cmd.runsql',
106 18
            'doctrine.dbal_cmd.import',
107 18
            'doctrine.orm_cmd.clear_cache_metadata',
108 18
            'doctrine.orm_cmd.clear_cache_result',
109 18
            'doctrine.orm_cmd.clear_cache_query',
110 18
            'doctrine.orm_cmd.schema_tool_create',
111 18
            'doctrine.orm_cmd.schema_tool_update',
112 18
            'doctrine.orm_cmd.schema_tool_drop',
113 18
            'doctrine.orm_cmd.ensure_production_settings',
114 18
            'doctrine.orm_cmd.convert_d1_schema',
115 18
            'doctrine.orm_cmd.generate_repositories',
116 18
            'doctrine.orm_cmd.generate_entities',
117 18
            'doctrine.orm_cmd.generate_proxies',
118 18
            'doctrine.orm_cmd.convert_mapping',
119 18
            'doctrine.orm_cmd.run_dql',
120 18
            'doctrine.orm_cmd.validate_schema',
121 18
            'doctrine.orm_cmd.info',
122 18
        );
123
124 18
        if (class_exists('Doctrine\\DBAL\\Migrations\\Version')) {
125 18
            $commands = ArrayUtils::merge(
126 18
                $commands,
127
                array(
128 18
                    'doctrine.migrations_cmd.execute',
129 18
                    'doctrine.migrations_cmd.generate',
130 18
                    'doctrine.migrations_cmd.migrate',
131 18
                    'doctrine.migrations_cmd.status',
132 18
                    'doctrine.migrations_cmd.version',
133 18
                    'doctrine.migrations_cmd.diff',
134 18
                    'doctrine.migrations_cmd.latest',
135
                )
136 18
            );
137 18
        }
138
139 18
        foreach ($commands as $commandName) {
140
            /* @var $command \Symfony\Component\Console\Command\Command */
141 18
            $command = $serviceLocator->get($commandName);
142 18
            $command->getDefinition()->addOption(new InputOption(
143 18
                'object-manager',
144 18
                null,
145 18
                InputOption::VALUE_OPTIONAL,
146 18
                'The name of the object manager to use.',
147
                'doctrine.entitymanager.orm_default'
148 18
            ));
149
150 18
            $cli->add($command);
151 18
        }
152
153 18
        $arguments = new ArgvInput();
154 18
        $objectManagerName = ($arguments->getParameterOption('--object-manager')) ?:
155 18
            'doctrine.entitymanager.orm_default';
156
157
        /* @var $objectManager \Doctrine\ORM\EntityManagerInterface */
158 18
        $objectManager = $serviceLocator->get($objectManagerName);
159 18
        $helperSet     = $cli->getHelperSet();
160
161 18
        if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
162 18
            $helperSet->set(new QuestionHelper(), 'dialog');
163 18
        } else {
164
            $helperSet->set(new DialogHelper(), 'dialog');
165
        }
166
167 18
        $helperSet->set(new ConnectionHelper($objectManager->getConnection()), 'db');
168 18
        $helperSet->set(new EntityManagerHelper($objectManager), 'em');
169 18
    }
170
}
171