Completed
Push — master ( 61af37...142460 )
by Andrii
03:25
created

StartController::findPrjDir()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
crap 12
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
    public static $started = false;
32
33
    public function actionMake()
34
    {
35
        Yii::setAlias('@prjdir', $this->findPrjDir());
36
        $this->takeConfig()->includeConfig(static::MAIN_CONFIG);
37
        $this->requireAll();
38
        $this->includeAll();
39
        self::$started = true;
40
    }
41
42
    public function actionUpdate()
43
    {
44
        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...
45
    }
46
47
    protected function requireAll()
48
    {
49
        $require = $this->takeConfig()->rawItem('require');
50
        if ($require) {
51
            $require['hiqdev/composer-extension-plugin'] = '*@dev';
52
            $saved = File::create('.hidev/composer.json')->save(compact('require'));
53
            if ($saved || !is_dir('.hidev/vendor')) {
54
                $this->runAction('update');
55
            }
56
            /// backup config then reset with extra config then restore
57
            $config = $this->takeConfig()->getItems();
58
            Yii::$app->clear('config');
59
            Yii::$app->loadExtraVendor('.hidev/vendor');
60
            $this->takeConfig()->mergeItems($config);
61
        }
62
    }
63
64
    /**
65
     * Include all configs.
66
     * @return void
67
     */
68
    public function includeAll()
69
    {
70
        $still = true;
71
        while ($still) {
72
            $still = false;
73
            $include = $this->takeConfig()->rawItem('include');
74
            if ($include) {
75
                foreach ($include as $path) {
76
                    $still = $still || $this->takeConfig()->includeConfig($path);
77
                }
78
            }
79
        }
80
    }
81
82
    /**
83
     * Chdirs to project's root by looking for config file in the current directory and up.
84
     * @throws InvalidParamException when failed to find
85
     * @return string path to the root directory of hidev project
86
     */
87
    protected function findPrjDir()
88
    {
89
        $configDir = '.hidev';
90
        for ($i = 0;$i < 9;++$i) {
91
            if (is_dir($configDir)) {
92
                $this->prjdir = getcwd();
93
                return $this->prjdir;
94
            }
95
            chdir('..');
96
        }
97
        throw new InvalidParamException("Not a hidev project (or any of the parent directories).\nUse `hidev init` to initialize hidev project.");
98
    }
99
}
100