Completed
Pull Request — master (#479)
by Michał
03:49
created

Module   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 94.64%

Importance

Changes 9
Bugs 2 Features 1
Metric Value
wmc 6
c 9
b 2
f 1
lcom 0
cbo 11
dl 0
loc 105
ccs 53
cts 56
cp 0.9464
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 52 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 DoctrineORMModule;
21
22
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
23
use Doctrine\ORM\EntityManager;
24
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
25
use Symfony\Component\Console\Application;
26
use Symfony\Component\Console\Helper\QuestionHelper;
27
use Zend\EventManager\EventInterface;
28
use Zend\ModuleManager\Feature\ConfigProviderInterface;
29
use Zend\ModuleManager\Feature\ControllerProviderInterface;
30
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
31
use Zend\ModuleManager\Feature\InitProviderInterface;
32
use Zend\ModuleManager\ModuleManagerInterface;
33
use Zend\ServiceManager\ServiceLocatorInterface;
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 56
    public function init(ModuleManagerInterface $manager)
54
    {
55 56
        $events = $manager->getEventManager();
56
        // Initialize logger collector once the profiler is initialized itself
57 56
        $events->attach(
58 56
            '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 56
        );
63 56
        $events->getSharedManager()->attach('doctrine', 'loadCli.post', [$this, 'initializeConsole']);
64 56
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 56
    public function getConfig()
70
    {
71 56
        return include __DIR__ . '/../config/module.config.php';
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 56
    public function getControllerConfig()
78
    {
79 56
        return include __DIR__ . '/../config/controllers.config.php';
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 56
    public function getModuleDependencies()
86
    {
87 56
        return ['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
     * @return void
95
     */
96 2
    public function initializeConsole(EventInterface $event)
97
    {
98
        /** @var $cli Application */
99 2
        $cli            = $event->getTarget();
100
        /** @var $serviceLocator ServiceLocatorInterface */
101 2
        $serviceLocator = $event->getParam('ServiceManager');
102
103
        $commands = [
104 2
            'doctrine.dbal_cmd.runsql',
105 2
            'doctrine.dbal_cmd.import',
106 2
            'doctrine.orm_cmd.clear_cache_metadata',
107 2
            'doctrine.orm_cmd.clear_cache_result',
108 2
            'doctrine.orm_cmd.clear_cache_query',
109 2
            'doctrine.orm_cmd.schema_tool_create',
110 2
            'doctrine.orm_cmd.schema_tool_update',
111 2
            'doctrine.orm_cmd.schema_tool_drop',
112 2
            'doctrine.orm_cmd.ensure_production_settings',
113 2
            'doctrine.orm_cmd.convert_d1_schema',
114 2
            'doctrine.orm_cmd.generate_repositories',
115 2
            'doctrine.orm_cmd.generate_entities',
116 2
            'doctrine.orm_cmd.generate_proxies',
117 2
            'doctrine.orm_cmd.convert_mapping',
118 2
            'doctrine.orm_cmd.run_dql',
119 2
            'doctrine.orm_cmd.validate_schema',
120 2
            'doctrine.orm_cmd.info',
121 2
        ];
122
123 2
        if (class_exists(\Doctrine\DBAL\Migrations\Version::class)) {
124 2
            $commands = ArrayUtils::merge(
125 2
                $commands,
126
                [
127 2
                    'doctrine.migrations_cmd.execute',
128 2
                    'doctrine.migrations_cmd.generate',
129 2
                    'doctrine.migrations_cmd.migrate',
130 2
                    'doctrine.migrations_cmd.status',
131 2
                    'doctrine.migrations_cmd.version',
132 2
                    'doctrine.migrations_cmd.diff',
133 2
                    'doctrine.migrations_cmd.latest',
134
                ]
135 2
            );
136 2
        }
137
138 2
        $cli->addCommands(array_map([$serviceLocator, 'get'], $commands));
139
140
        /** @var $entityManager EntityManager */
141 2
        $entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');
142 2
        $helperSet     = $cli->getHelperSet();
143
144 2
        $helperSet->set(new QuestionHelper(), 'dialog');
145 2
        $helperSet->set(new ConnectionHelper($entityManager->getConnection()), 'db');
146 2
        $helperSet->set(new EntityManagerHelper($entityManager), 'em');
147 2
    }
148
}
149