Completed
Push — master ( b4a6ad...e2bab1 )
by Andrii
13:50
created

StartController::includeAll()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 8.8571
cc 5
eloc 8
nc 5
nop 0
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) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use hidev\base\Application;
15
use hidev\base\File;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
19
/**
20
 * Start goal.
21
 * Chdirs to the project's root directory and loads dependencies and configs.
22
 */
23
class StartController extends CommonController
24
{
25
    const MAIN_CONFIG = '.hidev/config.yml';
26
27
    static $started = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $started.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
28
29
    public function actionMake()
30
    {
31
        Yii::setAlias('@prjdir', $this->findDir());
32
        $this->takeConfig()->includeConfig(static::MAIN_CONFIG);
33
        $this->requireAll();
34
        $this->includeAll();
35
        self::$started = true;
36
    }
37
38
    protected function requireAll()
39
    {
40
        $require = $this->takeConfig()->rawItem('require');
41
        if ($require) {
42
            $require['hiqdev/composer-extension-plugin'] = '*@dev';
43
            $saved = File::create('.hidev/composer.json')->save(compact('require'));
44
            if ($saved || !is_dir('.hidev/vendor')) {
45
                $this->takeGoal('update')->makeUpdate();
46
            }
47
            /// backup config then reset with extra config then restore
48
            $config = $this->takeConfig()->getItems();
49
            Yii::$app->clear('config');
50
            Yii::$app->loadExtraVendor('.hidev/vendor');
51
            $this->takeConfig()->mergeItems($config);
52
        }
53
    }
54
55
    /**
56
     * Include all configs.
57
     * @return void
58
     */
59
    public function includeAll()
60
    {
61
        $still = true;
62
        while ($still) {
63
            $still = false;
64
            $include = $this->takeConfig()->rawItem('include');
65
            if ($include) {
66
                foreach ($include as $path) {
67
                    $still = $still || $this->takeConfig()->includeConfig($path);
68
                }
69
            }
70
        }
71
    }
72
73
    /**
74
     * Chdirs to project's root by looking for config file in the current directory and up.
75
     * @return string path to the root directory of hidev project
76
     * @throws InvalidConfigException when failed to find
77
     */
78
    protected function findDir()
79
    {
80
        $configDir = '.hidev';
81
        for ($i = 0;$i < 9;++$i) {
82
            if (is_dir($configDir)) {
83
                return getcwd();
84
            }
85
            chdir('..');
86
        }
87
        throw new InvalidConfigException('Not a hidev project (or any of the parent directories). Use `hidev init` to initialize hidev project.');
88
    }
89
}
90