Completed
Push — master ( 1b573a...3fe54f )
by Basil
03:31 queued 01:20
created

BaseBootstrap::startModules()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace luya\base;
4
5
use Yii;
6
use yii\base\BootstrapInterface;
7
8
/**
9
 * Base class for luya bootsrapping proccess.
10
 *
11
 * @author Basil Suter <[email protected]>
12
 * @since 1.0.0
13
 */
14
abstract class BaseBootstrap implements BootstrapInterface
15
{
16
    /**
17
     * @var array Readonly variable contains all module Objects.
18
     */
19
    private $_modules;
20
21
    /**
22
     * Boostrap method will be invoken by Yii Application bootrapping proccess containing
23
     * the Application ($app) Object to get/set data.
24
     *
25
     * @param object $app Luya Application `luya\base\Application`.
26
     */
27
    public function bootstrap($app)
28
    {
29
        // add trace
30
        Yii::beginProfile('LUYA Boostrap process profiling', __METHOD__);
31
        
32
        // register luya core translation message source
33
        if (!isset($app->i18n->translations['luya'])) {
34
            $app->i18n->translations['luya'] = [
35
                'class' => 'yii\i18n\PhpMessageSource',
36
                'basePath' => '@luya/messages',
37
            ];
38
        }
39
        
40
        $this->extractModules($app);
41
        $this->beforeRun($app);
42
        $this->startModules($app);
43
        $this->run($app);
44
        
45
        // end trace
46
        Yii::trace('End of the LUYA bootstraping process', __METHOD__);
0 ignored issues
show
Deprecated Code introduced by
The method yii\BaseYii::trace() has been deprecated with message: since 2.0.14. Use [[debug()]] instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
        Yii::endProfile('LUYA Boostrap process profiling');
48
    }
49
50
    /**
51
     * Extract and load all modules from the Application-Object.
52
     *
53
     * @param object $app Luya Application `luya\base\Application`.
54
     */
55
    public function extractModules($app)
56
    {
57
        if ($this->_modules === null) {
58
            foreach ($app->getModules() as $id => $obj) {
59
                // create module object
60
                $moduleObject = Yii::$app->getModule($id);
61
                // see if the module is a luya base module, otherwise ignore
62
                if ($moduleObject instanceof \luya\base\Module) {
63
                    $this->_modules[$id] = $moduleObject;
64
                }
65
            }
66
            // when no luya modules are registered an empty array will be returned.
67
            if ($this->_modules === null) {
68
                $this->_modules = [];
69
            }
70
        }
71
    }
72
73
    /**
74
     * Check if a Module exists in the module list `getModules()`.
75
     *
76
     * @param string $module The name of the Module
77
     * @return bool
78
     */
79
    public function hasModule($module)
80
    {
81
        return array_key_exists($module, $this->_modules);
82
    }
83
84
    /**
85
     * Return all modules prepared by `extractModules()` method.
86
     *
87
     * @return array An array containg all modules where the key is the module name and the value is the Module Object `luya\base\Module`.
88
     */
89
    public function getModules()
90
    {
91
        return $this->_modules;
92
    }
93
94
    /**
95
     * Register all components from the modules `registerComponents()` method to the
96
     * Applcation.
97
     *
98
     * @param object $app Luya Appliation `\luya\base\Application`.
99
     */
100
    private function startModules($app)
101
    {
102
        foreach ($this->getModules() as $id => $module) {
103
            // set an alias for all user modules
104
            Yii::setAlias('@'.$id, $module->getBasePath());
105
106
            $module->luyaBootstrap($app);
107
108
            // see if the module has a registerComponents method
109
            foreach ($module->registerComponents() as $componentId => $definition) {
110
                if (!$app->has($componentId)) {
111
                    Yii::trace('Register component ' . $componentId, __METHOD__);
0 ignored issues
show
Deprecated Code introduced by
The method yii\BaseYii::trace() has been deprecated with message: since 2.0.14. Use [[debug()]] instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
112
                    $app->set($componentId, $definition);
113
                }
114
            }
115
        }
116
    }
117
118
    /**
119
     * This method will be invoke before the `run()` method.
120
     *
121
     * @param object $app Luya Application `luya\base\Application`
122
     */
123
    abstract public function beforeRun($app);
124
125
    /**
126
     * This method will be invoke after the `beforeRun()` method.
127
     *
128
     * @param object $app Luya Application `luya\base\Application`
129
     */
130
    abstract public function run($app);
131
}
132