|
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-2015, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hidev\goals; |
|
13
|
|
|
|
|
14
|
|
|
use hidev\helpers\Helper; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yii\base\BootstrapInterface; |
|
17
|
|
|
use yii\base\InvalidParamException; |
|
18
|
|
|
use yii\helpers\ArrayHelper; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The Config. Keeps config and Goals. |
|
22
|
|
|
*/ |
|
23
|
|
|
class ConfigGoal extends FileGoal implements BootstrapInterface |
|
24
|
|
|
{ |
|
25
|
|
|
//protected $_itemClass = 'hiqdev\collection\Manager'; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($config = []) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct('config', Yii::$app, $config); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public $types = ['yaml', 'json']; |
|
36
|
|
|
|
|
37
|
|
|
public $_file = '.hidev/config.yml'; |
|
38
|
|
|
|
|
39
|
|
|
public function hasGoal($name) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->hasItem($name) || class_exists($this->getGoalClass($name)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getGoalClass($name) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->getItemConfig($name)['class']; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getGoal($name) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->getItem($name); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function findGoal($name) |
|
55
|
|
|
{ |
|
56
|
|
|
$config = Yii::$app->pluginManager->get('goals')[$name]; |
|
57
|
|
|
return is_scalar($config) ? ['class' => $config] : (array) $config; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getItemConfig($name = null, array $config = []) |
|
61
|
|
|
{ |
|
62
|
|
|
$config = ArrayHelper::merge([ |
|
63
|
|
|
'goalName' => $name, |
|
64
|
|
|
'class' => 'hidev\goals\DefaultGoal', |
|
65
|
|
|
], static::findGoal($name), $config); |
|
66
|
|
|
|
|
67
|
|
|
return $config; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function createItem($name, array $config = []) |
|
71
|
|
|
{ |
|
72
|
|
|
#return Yii::createObject($this->getItemConfig($name, $config), [$name, $this->module]); |
|
|
|
|
|
|
73
|
|
|
return Yii::createObject($this->getItemConfig($name, $config), [$id, $this->module]); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getItem($name) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($name === 'default') { |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
$item = &$this->_items[$name]; |
|
82
|
|
|
if (is_array($item) || is_null($item)) { |
|
83
|
|
|
$item = $this->createItem($name, $item ?: []); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $item; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Bootstraps config. Reads or creates if doesn't exist. |
|
91
|
|
|
* |
|
92
|
|
|
* @param yii\base\Application $app application |
|
93
|
|
|
*/ |
|
94
|
|
|
public function bootstrap($app) |
|
95
|
|
|
{ |
|
96
|
|
|
if ($app->isInit) { |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
if (!$this->file->find($this->types)) { |
|
|
|
|
|
|
100
|
|
|
throw new InvalidParamException('No config found. Use hidev init vendor/package'); |
|
101
|
|
|
} |
|
102
|
|
|
if ($app->pluginManager->configFiles) { |
|
103
|
|
|
foreach ($app->pluginManager->configFiles as $path) { |
|
104
|
|
|
$this->includeConfig($path); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
$this->actionLoad(); |
|
108
|
|
|
if ($this->has('include')) { |
|
109
|
|
|
foreach (Helper::csplit($this->rawItem('include')) as $path) { |
|
110
|
|
|
$this->includeConfig($path); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function includeConfig($path) |
|
116
|
|
|
{ |
|
117
|
|
|
$file = Yii::createObject(array_merge([ |
|
118
|
|
|
'class' => 'hidev\base\File', |
|
119
|
|
|
], is_array($path) ? $path : compact('path'))); |
|
120
|
|
|
$this->setItems($file->load()); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.