Completed
Push — dev ( d8e2c0...b30194 )
by Андрей
02:16
created

Module::init()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 38
rs 8.5806
cc 4
eloc 22
nc 4
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/container
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Container;
7
8
use Zend\ModuleManager\Feature\ConfigProviderInterface;
9
use Zend\ModuleManager\Listener\ServiceListenerInterface;
10
use Zend\ModuleManager\ModuleManager;
11
use Zend\ModuleManager\ModuleManagerInterface;
12
use Zend\Mvc\ModuleRouteListener;
13
use Zend\Mvc\MvcEvent;
14
use Zend\EventManager\EventInterface;
15
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
16
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
17
use Zend\ModuleManager\Feature\InitProviderInterface;
18
use Zend\ServiceManager\ServiceLocatorInterface;
19
use Nnx\Container\EntryNameResolver\EntryNameResolverManager;
20
use Nnx\Container\EntryNameResolver\EntryNameResolverProviderInterface;
21
22
/**
23
 * Class Module
24
 *
25
 * @package Nnx\ContainerOptions
26
 */
27
class Module implements
28
    BootstrapListenerInterface,
29
    AutoloaderProviderInterface,
30
    InitProviderInterface,
31
    ConfigProviderInterface
32
{
33
    /**
34
     * Имя секции в конфиги приложения отвечающей за настройки модуля
35
     *
36
     * @var string
37
     */
38
    const CONFIG_KEY = 'nnx_container_module_options';
39
40
    /**
41
     * Имя модуля
42
     *
43
     * @var string
44
     */
45
    const MODULE_NAME = __NAMESPACE__;
46
47
    /**
48
     * @param ModuleManagerInterface $manager
49
     *
50
     * @throws Exception\InvalidArgumentException
51
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
52
     */
53
    public function init(ModuleManagerInterface $manager)
54
    {
55
        if (!$manager instanceof ModuleManager) {
56
            $errMsg =sprintf('Module manager not implement %s', ModuleManager::class);
57
            throw new Exception\InvalidArgumentException($errMsg);
58
        }
59
60
        /** @var ServiceLocatorInterface $sm */
61
        $sm = $manager->getEvent()->getParam('ServiceManager');
62
63
        if (!$sm instanceof ServiceLocatorInterface) {
64
            $errMsg = sprintf('Service locator not implement %s', ServiceLocatorInterface::class);
65
            throw new Exception\InvalidArgumentException($errMsg);
66
        }
67
        /** @var ServiceListenerInterface $serviceListener */
68
        $serviceListener = $sm->get('ServiceListener');
69
        if (!$serviceListener instanceof ServiceListenerInterface) {
70
            $errMsg = sprintf('ServiceListener not implement %s', ServiceListenerInterface::class);
71
            throw new Exception\InvalidArgumentException($errMsg);
72
        }
73
74
75
        $serviceListener->addServiceManager(
76
            EntryNameResolverManager::class,
77
            EntryNameResolverManager::CONFIG_KEY,
78
            EntryNameResolverProviderInterface::class,
79
            'getEntryNameResolverConfig'
80
        );
81
        
82
        $serviceListener->addServiceManager(
83
            ContainerInterface::class,
84
            Container::CONFIG_KEY,
85
            ContainerProviderInterface::class,
86
            'getContainerConfig'
87
        );
88
89
90
    }
91
92
    /**
93
     * @inheritdoc
94
     *
95
     * @param EventInterface $e
96
     *
97
     * @return array|void
98
     *
99
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
100
     */
101
    public function onBootstrap(EventInterface $e)
102
    {
103
        /** @var MvcEvent $e */
104
        $eventManager        = $e->getApplication()->getEventManager();
105
        $moduleRouteListener = new ModuleRouteListener();
106
        $moduleRouteListener->attach($eventManager);
107
108
    }
109
110
111
    /**
112
     * @return array
113
     */
114
    public function getAutoloaderConfig()
115
    {
116
        return [
117
            'Zend\Loader\StandardAutoloader' => [
118
                'namespaces' => [
119
                    __NAMESPACE__ => __DIR__ . '/src/',
120
                ],
121
            ],
122
        ];
123
    }
124
125
126
    /**
127
     * @return mixed
128
     */
129
    public function getConfig()
130
    {
131
        return include __DIR__ . '/config/module.config.php';
132
    }
133
134
}