Completed
Push — master ( 9777ec...f588f4 )
by Andrii
13:07
created

StartController::loadConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

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 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
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->takeConfig()->includeConfig(static::MAIN_CONFIG);
42
        $this->addAliases();
43
        $this->addAutoloader();
44
        $this->requireAll();
45
        $this->includeAll();
46
        $this->loadConfig();
47
        self::$started = true;
48
    }
49
50
    public function addAutoloader()
51
    {
52
        $autoloader = Yii::getAlias('@prjdir/vendor/autoload.php');
53
        if (file_exists($autoloader)) {
54
            spl_autoload_unregister(['Yii', 'autoload']);
55
            require $autoloader;
56
            spl_autoload_register(['Yii', 'autoload'], true, true);
57
        }
58
    }
59
60
    public function loadConfig()
61
    {
62
        $config = $this->takeConfig()->rawItem('config');
63
        if ($config) {
64
            Yii::$app->loadExtraConfig($config);
65
        }
66
    }
67
68
    /**
69
     * Update action.
70
     * @return int exit code
71
     */
72
    public function actionUpdate()
73
    {
74
        return $this->passthru('composer', ['update', '-d', '.hidev', '--prefer-source', '--ansi']);
0 ignored issues
show
Documentation introduced by
array('update', '-d', '....efer-source', '--ansi') is of type array<integer,string,{"0..."string","4":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
    }
76
77
    /**
78
     * Adds aliases:
79
     * - @prjdir alias to current project root dir
80
     * - current package namespace for it could be used from hidev.
81
     */
82
    public function addAliases()
83
    {
84
        Yii::setAlias('@prjdir', $this->findPrjDir());
85
        $config = $this->takeConfig()->rawItem('package');
86
        $alias  = '@' . strtr($config['namespace'], '\\', '/');
87
        if ($alias && !Yii::getAlias($alias, false)) {
88
            $srcdir = Yii::getAlias('@prjdir/' . ($config['src'] ?: 'src'));
89
            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 88 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...
90
        }
91
    }
92
93
    /**
94
     * Require all configured requires.
95
     */
96
    protected function requireAll()
97
    {
98
        $require = $this->takeConfig()->rawItem('require');
99
        if ($require) {
100
            $require['hiqdev/composer-extension-plugin'] = '*@dev';
101
            $saved = File::create('.hidev/composer.json')->save(compact('require'));
102
            if ($saved || !is_dir('.hidev/vendor')) {
103
                $this->runAction('update');
104
            }
105
            /// backup config then reset with extra config then restore
106
            $config = $this->takeConfig()->getItems();
107
            Yii::$app->clear('config');
108
            Yii::$app->loadExtraVendor('.hidev/vendor');
109
            $this->takeConfig()->mergeItems($config);
110
        }
111
    }
112
113
    /**
114
     * Include all configs.
115
     */
116
    public function includeAll()
117
    {
118
        $still = true;
119
        while ($still) {
120
            $still = false;
121
            $include = $this->takeConfig()->rawItem('include');
122
            if ($include) {
123
                foreach ($include as $path) {
124
                    $still = $still || $this->takeConfig()->includeConfig($path);
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * Chdirs to project's root by looking for config file in the current directory and up.
132
     * @throws InvalidParamException when failed to find
133
     * @return string path to the root directory of hidev project
134
     */
135
    protected function findPrjDir()
136
    {
137
        $configDir = '.hidev';
138
        for ($i = 0;$i < 9;++$i) {
139
            if (is_dir($configDir)) {
140
                $this->prjdir = getcwd();
141
                return $this->prjdir;
142
            }
143
            chdir('..');
144
        }
145
        throw new InvalidParamException("Not a hidev project (or any of the parent directories).\nUse `hidev init` to initialize hidev project.");
146
    }
147
}
148