Completed
Pull Request — feature/stable-doctrine-module (#518)
by
unknown
23:58 queued 21:46
created

Module   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 11
dl 0
loc 111
ccs 0
cts 71
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 12 1
A getConfig() 0 4 1
A getControllerConfig() 0 4 1
A getModuleDependencies() 0 4 1
A initializeConsole() 0 57 3
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 Zend\ModuleManager\Feature\ControllerProviderInterface;
25
use Zend\ModuleManager\Feature\ConfigProviderInterface;
26
use Zend\ModuleManager\Feature\InitProviderInterface;
27
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
28
use Zend\ModuleManager\ModuleManagerInterface;
29
use Zend\EventManager\EventInterface;
30
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
31
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
32
use Zend\Stdlib\ArrayUtils;
33
34
/**
35
 * Base module for Doctrine ORM.
36
 *
37
 * @license MIT
38
 * @link    www.doctrine-project.org
39
 * @author  Kyle Spraggs <[email protected]>
40
 * @author  Marco Pivetta <[email protected]>
41
 */
42
class Module implements
43
    ControllerProviderInterface,
44
    ConfigProviderInterface,
45
    InitProviderInterface,
46
    DependencyIndicatorInterface
47
{
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function init(ModuleManagerInterface $manager)
52
    {
53
        $events = $manager->getEventManager();
54
        // Initialize logger collector once the profiler is initialized itself
55
        $events->attach(
56
            'profiler_init',
57
            function () use ($manager) {
58
                $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...
59
            }
60
        );
61
        $events->getSharedManager()->attach('doctrine', 'loadCli.post', array($this, 'initializeConsole'));
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function getConfig()
68
    {
69
        return include __DIR__ . '/../../config/module.config.php';
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function getControllerConfig()
76
    {
77
        return include __DIR__ . '/../../config/controllers.config.php';
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function getModuleDependencies()
84
    {
85
        return array('DoctrineModule');
86
    }
87
88
    /**
89
     * Initializes the console with additional commands from the ORM, DBAL and (optionally) DBAL\Migrations
90
     *
91
     * @param \Zend\EventManager\EventInterface $event
92
     *
93
     * @return void
94
     */
95
    public function initializeConsole(EventInterface $event)
96
    {
97
        /* @var $cli \Symfony\Component\Console\Application */
98
        $cli            = $event->getTarget();
99
        /* @var $serviceLocator \Zend\ServiceManager\ServiceLocatorInterface */
100
        $serviceLocator = $event->getParam('ServiceManager');
101
102
        $commands = array(
103
            'doctrine.dbal_cmd.runsql',
104
            'doctrine.dbal_cmd.import',
105
            'doctrine.orm_cmd.clear_cache_metadata',
106
            'doctrine.orm_cmd.clear_cache_result',
107
            'doctrine.orm_cmd.clear_cache_query',
108
            'doctrine.orm_cmd.schema_tool_create',
109
            'doctrine.orm_cmd.schema_tool_update',
110
            'doctrine.orm_cmd.schema_tool_drop',
111
            'doctrine.orm_cmd.ensure_production_settings',
112
            'doctrine.orm_cmd.convert_d1_schema',
113
            'doctrine.orm_cmd.generate_repositories',
114
            'doctrine.orm_cmd.generate_entities',
115
            'doctrine.orm_cmd.generate_proxies',
116
            'doctrine.orm_cmd.convert_mapping',
117
            'doctrine.orm_cmd.run_dql',
118
            'doctrine.orm_cmd.validate_schema',
119
            'doctrine.orm_cmd.info',
120
        );
121
122
        if (class_exists('Doctrine\\DBAL\\Migrations\\Version')) {
123
            $commands = ArrayUtils::merge(
124
                $commands,
125
                array(
126
                    'doctrine.migrations_cmd.execute',
127
                    'doctrine.migrations_cmd.generate',
128
                    'doctrine.migrations_cmd.migrate',
129
                    'doctrine.migrations_cmd.status',
130
                    'doctrine.migrations_cmd.version',
131
                    'doctrine.migrations_cmd.diff',
132
                    'doctrine.migrations_cmd.latest',
133
                )
134
            );
135
        }
136
137
        $cli->addCommands(array_map(array($serviceLocator, 'get'), $commands));
138
139
        /* @var $entityManager \Doctrine\ORM\EntityManager */
140
        $entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');
141
        $helperSet     = $cli->getHelperSet();
142
143
        if (class_exists('Symfony\Component\Console\Helper\QuestionHelper')) {
144
            $helperSet->set(new QuestionHelper(), 'dialog');
145
        } else {
146
            $helperSet->set(new DialogHelper(), 'dialog');
147
        }
148
149
        $helperSet->set(new ConnectionHelper($entityManager->getConnection()), 'db');
150
        $helperSet->set(new EntityManagerHelper($entityManager), 'em');
151
    }
152
}
153