Completed
Push — master ( 1ab4f8...e0cd62 )
by Andrii
03:21
created

StartController::addAutoloader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use hidev\base\File;
15
use Yii;
16
use yii\base\InvalidParamException;
17
18
/**
19
 * Start goal.
20
 * Chdirs to the project's root directory and loads dependencies and configs.
21
 */
22
class StartController extends CommonController
23
{
24
    const MAIN_CONFIG = '.hidev/config.yml';
25
26
    /**
27
     * @var string absolute path to the project root directory
28
     */
29
    public $prjdir;
30
31
    /**
32
     * @var bool hidev already started flag
33
     */
34
    public static $started = false;
35
36
    /**
37
     * Make action.
38
     */
39
    public function actionMake()
40
    {
41
        $this->getPrjDir();
42
        $this->takeConfig()->includeConfig(static::MAIN_CONFIG);
43
        $this->addAliases();
44
        $this->addAutoloader();
45
        $this->requireAll();
46
        $this->includeAll();
47
        $this->loadConfig();
48
        self::$started = true;
49
    }
50
51
    public function addAutoloader()
52
    {
53
        $autoloader = Yii::getAlias('@prjdir/vendor/autoload.php');
54
        if (file_exists($autoloader)) {
55
            spl_autoload_unregister(['Yii', 'autoload']);
56
            require $autoloader;
57
            spl_autoload_register(['Yii', 'autoload'], true, true);
58
        }
59
    }
60
61
    /**
62
     * Update action.
63
     * @return int exit code
64
     */
65
    public function actionUpdate()
66
    {
67
        return $this->passthru('composer', ['update', '-d', '.hidev', '--prefer-source', '--ansi']);
68
    }
69
70
    /**
71
     * Adds aliases:
72
     * - @prjdir alias to current project root dir
73
     * - current package namespace for it could be used from hidev.
74
     */
75
    public function addAliases()
76
    {
77
        Yii::setAlias('@prjdir', $this->getPrjDir());
78
        $config = $this->takeConfig()->rawItem('package');
79
        $alias  = strtr($config['namespace'], '\\', '/');
80
        if ($alias && !Yii::getAlias('@' . $alias, false)) {
81
            $srcdir = Yii::getAlias('@prjdir/' . ($config['src'] ?: 'src'));
82
            Yii::setAlias($alias, $srcdir);
0 ignored issues
show
Bug introduced by
It seems like $srcdir defined by \Yii::getAlias('@prjdir/...onfig['src'] ?: 'src')) on line 81 can also be of type boolean; however, yii\BaseYii::setAlias() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
83
        }
84
    }
85
86
    /**
87
     * Require all configured requires.
88
     */
89
    protected function requireAll()
90
    {
91
        $require = $this->takeConfig()->rawItem('require');
92
        $vendors = [];
93
        if ($require) {
94
            $saved = File::create('.hidev/composer.json')->save(compact('require'));
95
            if ($saved || !is_dir('.hidev/vendor')) {
96
                $this->runAction('update');
97
            }
98
            $vendors[] = '.hidev/vendor';
99
        }
100
        if (file_exists('vendor/hiqdev')) {
101
            $vendors[] = 'vendor';
102
        }
103
        if (!empty($vendors)) {
104
            /// backup config then reset with extra config then restore
105
            $config = $this->takeConfig()->getItems();
106
            Yii::$app->clear('config');
107
            foreach (array_unique($vendors) as $dir) {
108
                Yii::$app->loadExtraVendor($dir);
109
            }
110
            $this->takeConfig()->mergeItems($config);
111
        }
112
    }
113
114
    /**
115
     * Include all configs.
116
     */
117
    public function includeAll()
118
    {
119
        $still = true;
120
        while ($still) {
121
            $still = false;
122
            $include = $this->takeConfig()->rawItem('include');
123
            if ($include) {
124
                foreach ($include as $path) {
125
                    $still = $still || $this->takeConfig()->includeConfig($path);
126
                }
127
            }
128
        }
129
    }
130
131
    /**
132
     * Load project's config if configured.
133
     */
134
    public function loadConfig()
135
    {
136
        $path = $this->takeConfig()->rawItem('config');
137
        if ($path) {
138
            Yii::$app->loadExtraConfig($path);
139
        }
140
    }
141
142
    public function getPrjDir()
143
    {
144
        if ($this->prjdir === null) {
145
            $this->prjdir = $this->findPrjDir();
146
        }
147
148
        return $this->prjdir;
149
    }
150
151
    /**
152
     * Chdirs to project's root by looking for config file in the current directory and up.
153
     * @throws InvalidParamException when failed to find
154
     * @return string path to the root directory of hidev project
155
     */
156
    protected function findPrjDir()
157
    {
158
        $configDir = '.hidev';
159
        for ($i = 0;$i < 9;++$i) {
160
            if (is_dir($configDir)) {
161
                $this->prjdir = getcwd();
162
                return $this->prjdir;
163
            }
164
            chdir('..');
165
        }
166
        throw new InvalidParamException("Not a hidev project (or any of the parent directories).\nUse `hidev init` to initialize hidev project.");
167
    }
168
}
169