Completed
Push — master ( 10bbaf...0fa0d2 )
by Андрей
8s
created

Module::init()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 6.7272
cc 7
eloc 15
nc 16
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\Module;
7
8
9
use Nnx\Module\Listener\IntegrationModuleListener;
10
use Zend\ModuleManager\Feature\ConfigProviderInterface;
11
use Zend\ModuleManager\ModuleManager;
12
use Zend\ModuleManager\ModuleManagerInterface;
13
use Zend\Mvc\ModuleRouteListener;
14
use Zend\Mvc\MvcEvent;
15
use Zend\EventManager\EventInterface;
16
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
17
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
18
use Zend\ModuleManager\Feature\InitProviderInterface;
19
use Zend\ServiceManager\ServiceLocatorInterface;
20
use Nnx\Module\Listener\IntegrationModuleListenerInterface;
21
22
/**
23
 * Class Module
24
 *
25
 * @package Nnx\ModuleOptions
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_module';
39
40
    /**
41
     * @param ModuleManagerInterface $manager
42
     *
43
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
44
     * @throws Exception\InvalidIntegrationModuleListenerException
45
     */
46
    public function init(ModuleManagerInterface $manager)
47
    {
48
        $integrationModuleListener = null;
49
50
        if ($manager instanceof ModuleManager) {
51
            $event =  $manager->getEvent();
52
            if ($event instanceof EventInterface) {
53
                $sl = $event->getParam('ServiceManager');
54
                if ($sl instanceof ServiceLocatorInterface && $sl->has(IntegrationModuleListenerInterface::class)) {
55
                    $integrationModuleListener = $sl->get(IntegrationModuleListenerInterface::class);
56
                }
57
            }
58
        }
59
60
        if (null === $integrationModuleListener) {
61
            $integrationModuleListener = new IntegrationModuleListener();
62
        }
63
64
        if (!$integrationModuleListener instanceof IntegrationModuleListenerInterface) {
65
            $errMsg = sprintf('Integration module listener not implement %s', IntegrationModuleListenerInterface::class);
66
            throw new Exception\InvalidIntegrationModuleListenerException($errMsg);
67
        }
68
69
        $moduleEventManager = $manager->getEventManager();
70
        $integrationModuleListener->attach($moduleEventManager);
71
72
    }
73
74
    /**
75
     * @inheritdoc
76
     *
77
     * @param EventInterface $e
78
     *
79
     * @return array|void
80
     *
81
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
82
     */
83
    public function onBootstrap(EventInterface $e)
84
    {
85
        /** @var MvcEvent $e */
86
        $eventManager        = $e->getApplication()->getEventManager();
87
        $moduleRouteListener = new ModuleRouteListener();
88
        $moduleRouteListener->attach($eventManager);
89
90
    }
91
92
93
    /**
94
     * @return array
95
     */
96
    public function getAutoloaderConfig()
97
    {
98
        return array(
99
            'Zend\Loader\StandardAutoloader' => array(
100
                'namespaces' => array(
101
                    __NAMESPACE__ => __DIR__ . '/src/',
102
                ),
103
            ),
104
        );
105
    }
106
107
108
    /**
109
     * @return mixed
110
     */
111
    public function getConfig()
112
    {
113
        return include __DIR__ . '/config/module.config.php';
114
    }
115
116
}