1 | <?php |
||
23 | class InitController extends \yii\console\Controller |
||
24 | { |
||
25 | /** |
||
26 | * @var string package full name like: vendor/package |
||
27 | */ |
||
28 | public $name; |
||
29 | |||
30 | /** |
||
31 | * @var string package type e.g.: package, yii2-extension |
||
32 | */ |
||
33 | public $type; |
||
34 | |||
35 | public $title; |
||
36 | public $package; |
||
37 | public $license; |
||
38 | public $headline; |
||
39 | public $keywords; |
||
40 | public $namespace; |
||
41 | public $description; |
||
42 | |||
43 | public $vendor; |
||
44 | public $nick; |
||
45 | public $author; |
||
46 | public $email; |
||
47 | |||
48 | public $vendorRequire; |
||
49 | |||
50 | /** |
||
51 | * @var bool don't put vendor config |
||
52 | */ |
||
53 | public $noVendor; |
||
54 | /** |
||
55 | * @var bool don't put requires |
||
56 | */ |
||
57 | public $noRequire; |
||
58 | |||
59 | /** |
||
60 | * @var bool don't generate `composer.json` file |
||
61 | */ |
||
62 | public $noComposer; |
||
63 | |||
64 | 3 | public function options($actionId) |
|
65 | { |
||
66 | 3 | return array_diff( |
|
67 | 3 | array_keys(get_object_vars($this)), |
|
68 | 3 | explode(',', 'color,help,id,module,layout,action,interactive,defaultAction') |
|
69 | 3 | ); |
|
70 | } |
||
71 | |||
72 | 3 | public function prepareData($name) |
|
73 | { |
||
74 | 3 | $this->name = $name; |
|
75 | 3 | list($vendor, $package) = explode('/', $name, 2); |
|
76 | 3 | if ($vendor) { |
|
77 | 3 | $this->vendor = $vendor; |
|
78 | 3 | $vendorPlugin = "$vendor/hidev-$vendor"; |
|
79 | 1 | try { |
|
80 | 3 | $exists = @file_get_contents("https://packagist.org/packages/$vendorPlugin.json"); |
|
81 | 3 | } catch (Exception $e) { |
|
82 | $exists = false; |
||
83 | } |
||
84 | 3 | if ($exists) { |
|
85 | 1 | $this->vendorRequire = $vendorPlugin; |
|
86 | 1 | $this->noVendor = true; |
|
87 | 1 | } |
|
88 | 3 | } |
|
89 | 3 | if ($package) { |
|
90 | 3 | $this->package = $package; |
|
91 | 3 | } |
|
92 | 3 | if (!$this->package || !$this->vendor) { |
|
93 | throw new InvalidParamException('Wrong vendor/package given: ' . $name); |
||
94 | } |
||
95 | 3 | } |
|
96 | |||
97 | /** |
||
98 | * Creates initial configuration for hidev: `hidev.yml` and `composer.json`. |
||
99 | * @param string $name package full name like: vendor/package |
||
100 | */ |
||
101 | 3 | public function actionIndex($name = null) |
|
102 | { |
||
103 | 3 | $this->prepareData($name); |
|
104 | 3 | if (!$this->noComposer) { |
|
105 | 2 | $this->writeComposer(); |
|
106 | 2 | } |
|
107 | |||
108 | 3 | return $this->writeConfig(); |
|
109 | } |
||
110 | |||
111 | public function actionComposer() |
||
115 | |||
116 | 3 | public function writeConfig($path = 'hidev.yml') |
|
117 | { |
||
118 | 3 | $file = new File(['path' => $path]); |
|
119 | 3 | $file->save(array_filter([ |
|
120 | 3 | 'package' => array_filter([ |
|
121 | 3 | 'type' => $this->getType(), |
|
122 | 3 | 'name' => $this->package, |
|
123 | 3 | 'title' => $this->getTitle(), |
|
124 | 3 | 'license' => $this->license, |
|
125 | 3 | 'headline' => $this->headline, |
|
126 | 3 | 'keywords' => $this->getKeywords(), |
|
127 | 3 | 'namespace' => $this->namespace, |
|
128 | 3 | 'description' => $this->description, |
|
129 | 3 | ]), |
|
130 | 3 | 'vendor' => $this->noVendor ? null : array_filter([ |
|
131 | 1 | 'name' => $this->vendor, |
|
132 | 1 | 'authors' => array_filter([ |
|
133 | 1 | $this->getNick() => [ |
|
134 | 1 | 'name' => $this->getAuthor(), |
|
135 | 1 | 'email' => $this->getEmail(), |
|
136 | 1 | ], |
|
137 | 1 | ]), |
|
138 | 3 | ]), |
|
139 | 3 | ])); |
|
140 | 3 | } |
|
141 | |||
142 | 2 | public function writeComposer($path = 'composer.json') |
|
143 | { |
||
144 | 2 | $file = new File(['path' => $path]); |
|
145 | 2 | $file->save(array_filter([ |
|
146 | 2 | 'name' => $this->name, |
|
147 | 2 | 'type' => $this->getType(), |
|
148 | 2 | 'description' => $this->getTitle(), |
|
149 | 2 | 'keywords' => preg_split('/\s*,\s*/', $this->getKeywords()), |
|
150 | 2 | 'require-dev' => $this->getPlugins(), |
|
151 | 2 | 'license' => $this->license, |
|
152 | 2 | ])); |
|
153 | 2 | } |
|
154 | |||
155 | 3 | public function getType() |
|
159 | |||
160 | 3 | public function getTitle() |
|
164 | |||
165 | 3 | public function getKeywords() |
|
169 | |||
170 | /// TODO think of better getting nick |
||
171 | 1 | public function getNick() |
|
175 | |||
176 | 1 | public function getAuthor() |
|
180 | |||
181 | 1 | public function getEmail() |
|
185 | |||
186 | /** |
||
187 | * Returns list of plugins in composer requirements format: name => version. |
||
188 | * @return array |
||
189 | */ |
||
190 | 2 | public function getPlugins() |
|
204 | } |
||
205 |