Test Failed
Push — master ( c71310...b1cf76 )
by Julien
20:09
created

Module::getServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc;
13
14
use Phalcon\Autoload\Loader;
15
use Phalcon\Di\DiInterface;
16
use Phalcon\Mvc\ModuleDefinitionInterface;
17
use Zemit\Bootstrap\Config;
18
use Zemit\Di\Injectable;
19
use Zemit\Support\Utils;
20
21
/**
22
 * {@inheritDoc}
23
 */
24
class Module extends Injectable implements ModuleDefinitionInterface
25
{
26
    public const string NAME_FRONTEND = 'frontend';
27
    public const string NAME_ADMIN = 'admin';
28
    public const string NAME_API = 'api';
29
    public const string NAME_OAUTH2 = 'oauth2';
30
    
31
    public string $name;
32
    
33
    public ?Config $config = null;
34
    
35
    public ?Dispatcher $dispatcher = null;
36
    
37
    public ?Loader $loader = null;
38
    
39
    public ?Router $router = null;
40
    
41
    public ?View $view = null;
42
    
43
    public ?Url $url = null;
44
    
45
    /**
46
     * Registers an autoloader related to the frontend module
47
     */
48
    public function registerAutoloaders(DiInterface $container = null): void
49
    {
50
        $this->loader ??= $container['loader'] ?? new Loader();
51
        assert($this->loader instanceof Loader);
52
        
53
        $this->loader->setNamespaces($this->getNamespaces(), true);
54
        $this->loader->register();
55
    }
56
    
57
    /**
58
     * Registers services related to the module
59
     */
60
    public function registerServices(DiInterface $container): void
61
    {
62
        $this->getServices($container);
63
        
64
        assert($this->url instanceof Url);
65
        assert($this->view instanceof View);
66
        assert($this->router instanceof Router);
67
        assert($this->dispatcher instanceof Dispatcher);
68
        
69
        $defaultNamespace = $this->getDefaultNamespace();
70
        $this->dispatcher->setDefaultNamespace($defaultNamespace);
71
        $this->dispatcher->setNamespaceName($defaultNamespace);
72
        $this->view->setViewsDir($this->getViewsDir());
73
        
74
        // url settings
75
        $this->url->setBasePath('/' . $this->name . '/');
76
        $this->url->setStaticBaseUri('/' . $this->name . '/');
77
        $this->router->setDefaults([
78
            'namespace' => $defaultNamespace,
79
            'module' => $this->name,
80
            'controller' => 'index',
81
            'action' => 'index',
82
        ]);
83
        
84
        // router settings
85
        $this->router->notFound([
86
            'controller' => 'error',
87
            'action' => 'notFound',
88
        ]);
89
        $this->router->removeExtraSlashes(true);
90
        
91
        $this->setServices($container);
92
    }
93
    
94
    public function getServices(DiInterface $container = null): void
95
    {
96
        $this->loader ??= $container['loader'] ?? new Loader();
97
        $this->config ??= $container['config'] ?? new Config();
98
        $this->router ??= $container['router'] ?? new Router();
99
        $this->dispatcher ??= $container['dispatcher'] ?? new Dispatcher();
100
        $this->view ??= $container['view'] ?? new View();
101
        $this->url ??= $container['url'] ?? new Url();
102
    }
103
    
104
    public function setServices(DiInterface $container): void
105
    {
106
        $container->set('config', $this->config);
107
        $container->set('dispatcher', $this->dispatcher);
108
        $container->set('loader', $this->loader);
109
        $container->set('router', $this->router);
110
        $container->set('view', $this->view);
111
        $container->set('url', $this->url);
112
    }
113
    
114
    public function getNamespaces(): array
115
    {
116
        // Caller namespace
117
        $namespace = $this->getNamespace();
118
        $dirname = $this->getDirname();
119
        
120
        // register the vendor module controllers
121
        $namespaces = [];
122
        $namespaces[$namespace . '\\Controllers'] = $dirname . '/Controllers/';
123
        $namespaces[$namespace . '\\Models'] = $dirname . '/Models/';
124
        
125
        // add zemit core models
126
        $corePath = dirname(__DIR__);
127
        $namespaces['Zemit\\Models'] = $corePath . '/Models/';
128
        
129
        return $namespaces;
130
    }
131
    
132
    public function getDefaultNamespace(): string
133
    {
134
        return $this->getNamespace() . '\\Controllers';
135
    }
136
    
137
    public function getViewsDir(): array
138
    {
139
        return [$this->getDirname() . '/Views/'];
140
    }
141
    
142
    public function getDirname(): string
143
    {
144
        return Utils::getDirname($this);
145
    }
146
    
147
    public function getNamespace(): string
148
    {
149
        return Utils::getNamespace($this);
150
    }
151
}
152