|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Automation tool mixed with code generator for easier continuous development. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/hidev |
|
6
|
|
|
* @package hidev |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hidev\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use hidev\base\File; |
|
15
|
|
|
use hidev\helpers\Helper; |
|
16
|
|
|
use yii\base\InvalidParamException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Init controller. |
|
20
|
|
|
* Builds .hidev/config.yml by template and params. |
|
21
|
|
|
*/ |
|
22
|
|
|
class InitController extends TemplateController |
|
23
|
|
|
{ |
|
24
|
|
|
protected $_file = '.hidev/config.yml'; |
|
25
|
|
|
|
|
26
|
|
|
public $name; |
|
27
|
|
|
public $vendor; |
|
28
|
|
|
public $package; |
|
29
|
|
|
|
|
30
|
3 |
|
public function prepareData($name) |
|
31
|
|
|
{ |
|
32
|
3 |
|
$this->name = $name; |
|
33
|
3 |
|
list($vendor, $package) = explode('/', $name, 2); |
|
34
|
3 |
|
if ($vendor) { |
|
35
|
3 |
|
$this->vendor = $vendor; |
|
36
|
3 |
|
$vendorPlugin = "$vendor/hidev-$vendor"; |
|
37
|
|
|
try { |
|
38
|
3 |
|
$exists = @file_get_contents("https://packagist.org/packages/$vendorPlugin.json"); |
|
39
|
|
|
} catch (Exception $e) { |
|
40
|
|
|
$exists = false; |
|
41
|
|
|
} |
|
42
|
3 |
|
if ($exists) { |
|
43
|
1 |
|
$this->setItem('vendorRequire', $vendorPlugin); |
|
44
|
1 |
|
$this->setItem('novendor', true); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
3 |
|
if ($package) { |
|
48
|
3 |
|
$this->package = $package; |
|
49
|
|
|
} |
|
50
|
3 |
|
if (!$this->package || !$this->vendor) { |
|
51
|
|
|
throw new InvalidParamException('Wrong vendor/package given: ' . $name); |
|
52
|
|
|
} |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
public function actionPerform($name = null, $template = '.hidev/config') |
|
56
|
|
|
{ |
|
57
|
3 |
|
$this->_template = $template; |
|
58
|
3 |
|
$this->prepareData($name); |
|
59
|
3 |
|
if (!file_exists($this->dirname)) { |
|
|
|
|
|
|
60
|
3 |
|
mkdir($this->dirname); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
3 |
|
if (!$this->nocomposer) { |
|
|
|
|
|
|
63
|
2 |
|
$this->actionComposer(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
return parent::actionPerform(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public function actionComposer() |
|
70
|
|
|
{ |
|
71
|
2 |
|
$file = new File(['path' => 'composer.json']); |
|
72
|
2 |
|
$data = array_filter([ |
|
73
|
2 |
|
'name' => $this->name, |
|
74
|
2 |
|
'type' => $this->getType(), |
|
75
|
2 |
|
'description' => $this->getTitle(), |
|
76
|
2 |
|
'keywords' => preg_split('/\s*,\s*/', $this->getKeywords()), |
|
77
|
2 |
|
'require-dev' => $this->getPlugins(), |
|
78
|
2 |
|
'license' => $this->getItem('license'), |
|
79
|
|
|
]); |
|
80
|
2 |
|
$file->save($data); |
|
81
|
2 |
|
$this->setItem('norequire', true); |
|
|
|
|
|
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
public function options($actionId) |
|
85
|
|
|
{ |
|
86
|
3 |
|
return array_merge(parent::options($actionId), explode(',', 'namespace,headline,title,type,license,keywords,description,year,nick,author,email,novendor,norequire,nocomposer')); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
public function getType() |
|
90
|
|
|
{ |
|
91
|
3 |
|
return $this->getItem('type') ?: 'project'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
public function getTitle() |
|
95
|
|
|
{ |
|
96
|
3 |
|
return $this->getItem('title') ?: Helper::titleize($this->package); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
public function getKeywords() |
|
100
|
|
|
{ |
|
101
|
3 |
|
return $this->getItem('keywords') ?: implode(', ', explode('-', $this->package)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/// TODO think of better getting nick |
|
105
|
3 |
|
public function getNick() |
|
106
|
|
|
{ |
|
107
|
3 |
|
return $this->getItem('nick') ?: preg_replace('/[^a-zA-Z_0-9]+/', '', `id -un`); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
public function getAuthor() |
|
111
|
|
|
{ |
|
112
|
2 |
|
return $this->getItem('author') ?: $this->takeVcs()->getUserName(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
public function getEmail() |
|
116
|
|
|
{ |
|
117
|
2 |
|
return $this->getItem('email') ?: $this->takeVcs()->getUserEmail(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Returns list of plugins in composer requirements format: name => version. |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function getPlugins() |
|
125
|
|
|
{ |
|
126
|
3 |
|
if ($this->norequire) { |
|
|
|
|
|
|
127
|
3 |
|
return []; |
|
128
|
|
|
} |
|
129
|
|
|
$res = [ |
|
130
|
2 |
|
'hiqdev/hidev-php' => '<2.0 || dev-master', |
|
131
|
|
|
]; |
|
132
|
2 |
|
if ($this->vendorRequire) { |
|
|
|
|
|
|
133
|
1 |
|
$res[$this->vendorRequire] = '<2.0 || dev-master'; |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
return $res; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
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: