Completed
Push — master ( 6ac6c3...a597ac )
by Андрей
19:37 queued 17:03
created

Module::getModuleDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Doctrine;
7
8
9
use Nnx\Doctrine\ManagerRegistry\ParamsFromDoctrineModuleListener;
10
use Zend\ModuleManager\Feature\ConfigProviderInterface;
11
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;
12
use Zend\ModuleManager\Listener\ServiceListenerInterface;
13
use Zend\ModuleManager\ModuleManager;
14
use Zend\ModuleManager\ModuleManagerInterface;
15
use Zend\Mvc\ModuleRouteListener;
16
use Zend\Mvc\MvcEvent;
17
use Zend\EventManager\EventInterface;
18
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
19
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
20
use Zend\ModuleManager\Feature\InitProviderInterface;
21
use Zend\ServiceManager\ServiceLocatorInterface;
22
use Nnx\Doctrine\ObjectManager\DoctrineObjectManager;
23
use Nnx\Doctrine\ObjectManager\DoctrineObjectManagerProviderInterface;
24
use Nnx\Doctrine\EntityManager\EntityManager;
25
use Nnx\Doctrine\EntityManager\EntityManagerProviderInterface;
26
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
27
use Zend\ModuleManager\Feature\ConsoleBannerProviderInterface;
28
use Zend\Console\Adapter\AdapterInterface as Console;
29
use Nnx\Doctrine\ManagerRegistry\ResolverManagerRegistryResourceListener;
30
use Nnx\Doctrine\Listener\ManagerRegistryListener;
31
use Nnx\ModuleOptions\Module as ModuleOptions;
32
33
/**
34
 * Class Module
35
 *
36
 * @package Nnx\ModuleOptions
37
 */
38
class Module implements
39
    BootstrapListenerInterface,
40
    AutoloaderProviderInterface,
41
    InitProviderInterface,
42
    ConfigProviderInterface,
43
    ConsoleUsageProviderInterface,
44
    ConsoleBannerProviderInterface,
45
    DependencyIndicatorInterface
46
{
47
    /**
48
     * Имя секции в конфиги приложения отвечающей за настройки модуля
49
     *
50
     * @var string
51
     */
52
    const CONFIG_KEY = 'nnx_doctrine';
53
54
    /**
55
     * Имя модуля
56
     *
57
     * @var string
58
     */
59
    const MODULE_NAME = __NAMESPACE__;
60
61
    /**
62
     * @return array
63
     */
64
    public function getModuleDependencies()
65
    {
66
        return [
67
            ModuleOptions::MODULE_NAME
68
        ];
69
    }
70
71
72
    /**
73
     * @param ModuleManagerInterface $manager
74
     *
75
     * @throws Exception\InvalidArgumentException
76
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
77
     */
78
    public function init(ModuleManagerInterface $manager)
79
    {
80
        if (!$manager instanceof ModuleManager) {
81
            $errMsg = sprintf('Module manager not implement %s', ModuleManager::class);
82
            throw new Exception\InvalidArgumentException($errMsg);
83
        }
84
85
        /** @var ServiceLocatorInterface $sm */
86
        $sm = $manager->getEvent()->getParam('ServiceManager');
87
88
        if (!$sm instanceof ServiceLocatorInterface) {
89
            $errMsg = sprintf('Service locator not implement %s', ServiceLocatorInterface::class);
90
            throw new Exception\InvalidArgumentException($errMsg);
91
        }
92
        /** @var ServiceListenerInterface $serviceListener */
93
        $serviceListener = $sm->get('ServiceListener');
94
        if (!$serviceListener instanceof ServiceListenerInterface) {
95
            $errMsg = sprintf('ServiceListener not implement %s', ServiceListenerInterface::class);
96
            throw new Exception\InvalidArgumentException($errMsg);
97
        }
98
99
        $serviceListener->addServiceManager(
100
            DoctrineObjectManager::class,
101
            DoctrineObjectManager::CONFIG_KEY,
102
            DoctrineObjectManagerProviderInterface::class,
103
            'getDoctrineObjectManagerConfig'
104
        );
105
106
        $serviceListener->addServiceManager(
107
            EntityManager::class,
108
            EntityManager::CONFIG_KEY,
109
            EntityManagerProviderInterface::class,
110
            'getEntityManagerConfig'
111
        );
112
    }
113
114
    /**
115
     * @inheritdoc
116
     *
117
     * @param EventInterface $e
118
     *
119
     * @return array|void
120
     * @throws \Nnx\Doctrine\Exception\InvalidArgumentException
121
     *
122
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
123
     */
124
    public function onBootstrap(EventInterface $e)
125
    {
126
        if (!$e instanceof MvcEvent) {
127
            $errMsg = sprintf('Event not implement %s', MvcEvent::class);
128
            throw new Exception\InvalidArgumentException($errMsg);
129
        }
130
131
        $app = $e->getApplication();
132
133
        $eventManager        = $app->getEventManager();
134
        $moduleRouteListener = new ModuleRouteListener();
135
        $moduleRouteListener->attach($eventManager);
136
137
        $sl = $app->getServiceManager();
138
        /** @var ParamsFromDoctrineModuleListener $paramsFromDoctrineModuleListener */
139
        $paramsFromDoctrineModuleListener = $sl->get(ParamsFromDoctrineModuleListener::class);
140
        $paramsFromDoctrineModuleListener->attach($eventManager);
141
142
143
        /** @var ResolverManagerRegistryResourceListener $resolverManagerRegistryResourceListener */
144
        $resolverManagerRegistryResourceListener = $sl->get(ResolverManagerRegistryResourceListener::class);
145
        $resolverManagerRegistryResourceListener->attach($eventManager);
146
147
        /** @var ManagerRegistryListener $managerRegistryListener */
148
        $managerRegistryListener = $sl->get(ManagerRegistryListener::class);
149
        $managerRegistryListener->attach($eventManager);
150
151
    }
152
153
154
155
    /**
156
     * @return array
157
     */
158
    public function getAutoloaderConfig()
159
    {
160
        return array(
161
            'Zend\Loader\StandardAutoloader' => array(
162
                'namespaces' => array(
163
                    __NAMESPACE__ => __DIR__ . '/src/',
164
                ),
165
            ),
166
        );
167
    }
168
169
170
    /**
171
     * @return mixed
172
     */
173
    public function getConfig()
174
    {
175
        return include __DIR__ . '/config/module.config.php';
176
    }
177
178
    /**
179
     * @inheritdoc
180
     *
181
     * @param Console $console
182
     *
183
     * @return string
184
     */
185
    public function getConsoleBanner(Console $console){
186
        return 'Tools for entity map';
187
    }
188
189
    /**
190
     * @inheritdoc
191
     *
192
     * @param Console $console
193
     *
194
     * @return array
195
     */
196
    public function getConsoleUsage(Console $console){
197
        return [
198
            'Entity map builder',
199
            'entity-map build --objectManager=' => 'Generation entity maps and cached',
200
            ['--objectManager=MANAGER_NAME', 'ObjectManager name'],
201
            'entity-map clear --objectManager=' => 'Clear entity maps from cached',
202
            ['--objectManager=MANAGER_NAME', 'ObjectManager name'],
203
        ];
204
    }
205
}