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