Completed
Push — master ( 53b99e...05e097 )
by Basil
03:48
created

BaseBootstrap::run()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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->registerComponents($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 registerComponents($app)
101
    {
102
        foreach ($this->getModules() as $id => $module) {
103
            // set an alias for all user modules
104
            Yii::setAlias('@'.$id, $module->getBasePath());
105
            // see if the module has a registerComponents method
106
            foreach ($module->registerComponents() as $componentId => $definition) {
107
                if (!$app->has($componentId)) {
108
                    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...
109
                    $app->set($componentId, $definition);
110
                }
111
            }
112
        }
113
    }
114
115
    /**
116
     * This method will be invoke before the `run()` method.
117
     *
118
     * @param object $app Luya Application `luya\base\Application`
119
     */
120
    abstract public function beforeRun($app);
121
122
    /**
123
     * This method will be invoke after the `beforeRun()` method.
124
     *
125
     * @param object $app Luya Application `luya\base\Application`
126
     */
127
    abstract public function run($app);
128
}
129