Completed
Push — master ( 60927c...41af59 )
by Andrii
05:05
created

StartController::getPrjDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 0
cts 7
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->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
        if ($require) {
93
            $require['hiqdev/composer-extension-plugin'] = 'dev-master';
94
            $saved = File::create('.hidev/composer.json')->save(compact('require'));
95
            if ($saved || !is_dir('.hidev/vendor')) {
96
                $this->runAction('update');
97
            }
98
            /// backup config then reset with extra config then restore
99
            $config = $this->takeConfig()->getItems();
100
            Yii::$app->clear('config');
101
            Yii::$app->loadExtraVendor('.hidev/vendor');
102
            $this->takeConfig()->mergeItems($config);
103
        }
104
    }
105
106
    /**
107
     * Include all configs.
108
     */
109
    public function includeAll()
110
    {
111
        $still = true;
112
        while ($still) {
113
            $still = false;
114
            $include = $this->takeConfig()->rawItem('include');
115
            if ($include) {
116
                foreach ($include as $path) {
117
                    $still = $still || $this->takeConfig()->includeConfig($path);
118
                }
119
            }
120
        }
121
    }
122
123
    /**
124
     * Load project's config if configured.
125
     */
126
    public function loadConfig()
127
    {
128
        $path = $this->takeConfig()->rawItem('config');
129
        if ($path) {
130
            Yii::$app->loadExtraConfig($path);
131
        }
132
    }
133
134
    public function getPrjDir()
135
    {
136
        if ($this->prjdir === null) {
137
            $this->prjdir = $this->findPrjDir();
138
        }
139
140
        return $this->prjdir;
141
    }
142
143
    /**
144
     * Chdirs to project's root by looking for config file in the current directory and up.
145
     * @throws InvalidParamException when failed to find
146
     * @return string path to the root directory of hidev project
147
     */
148
    protected function findPrjDir()
149
    {
150
        $configDir = '.hidev';
151
        for ($i = 0;$i < 9;++$i) {
152
            if (is_dir($configDir)) {
153
                $this->prjdir = getcwd();
154
                return $this->prjdir;
155
            }
156
            chdir('..');
157
        }
158
        throw new InvalidParamException("Not a hidev project (or any of the parent directories).\nUse `hidev init` to initialize hidev project.");
159
    }
160
}
161