1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Composer plugin for HiDev |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hidev-composer |
6
|
|
|
* @package hidev-composer |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\composer\components; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* `composer.json` config file. |
15
|
|
|
* @author Andrii Vasyliev <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ComposerJson extends \hidev\base\ConfigFile |
18
|
|
|
{ |
19
|
|
|
protected $_file = 'composer.json'; |
20
|
|
|
|
21
|
|
|
public function load() |
22
|
|
|
{ |
23
|
|
|
parent::load(); |
24
|
|
|
$sets = array_filter([ |
25
|
|
|
'name' => $this->getName(), |
26
|
|
|
'type' => $this->getType(), |
27
|
|
|
'description' => $this->take('package')->title, |
28
|
|
|
'keywords' => $this->take('package')->keywords, |
29
|
|
|
'homepage' => $this->take('package')->homepage, |
30
|
|
|
'license' => $this->take('package')->license, |
31
|
|
|
'support' => $this->getSupport(), |
32
|
|
|
'authors' => $this->getAuthors(), |
33
|
|
|
'require' => $this->getRequire(), |
34
|
|
|
'require-dev' => $this->getRequireDev(), |
35
|
|
|
]); |
36
|
|
|
$this->setItems($sets, 'first'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Converts hidev type to composer type. |
41
|
|
|
* TODO composer type can be different from package type. |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getType() |
45
|
|
|
{ |
46
|
|
|
return $this->take('package')->type; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Converts hidev full name to composer name. |
51
|
|
|
* TODO composer name can be different from package full name. |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getName() |
55
|
|
|
{ |
56
|
|
|
return $this->take('package')->fullName; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getFullName() |
63
|
|
|
{ |
64
|
|
|
return $this->getName(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getSupport() |
68
|
|
|
{ |
69
|
|
|
return array_merge(array_filter([ |
70
|
|
|
'email' => $this->take('vendor')->email, |
71
|
|
|
'source' => $this->take('package')->source, |
72
|
|
|
'issues' => $this->take('package')->issues, |
73
|
|
|
'wiki' => $this->take('package')->wiki, |
74
|
|
|
'forum' => $this->take('package')->forum, |
75
|
|
|
]), (array) $this->getItem('support')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getAuthors() |
79
|
|
|
{ |
80
|
|
|
if ($this->take('package')->authors) { |
81
|
|
|
foreach ($this->take('package')->authors as $all_data) { |
82
|
|
|
$data = []; |
83
|
|
|
foreach (['name', 'role', 'email', 'homepage'] as $k) { |
84
|
|
|
if (!empty($all_data[$k])) { |
85
|
|
|
$data[$k] = $all_data[$k]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
$res[] = $data; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $res; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* XXX DISABLED, think if it's needed :-/. |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function old_getAutoload() |
100
|
|
|
{ |
101
|
|
|
$autoload = $this->rawItem('autoload'); |
102
|
|
|
$psr4 = $autoload['psr-4'] ?: []; |
103
|
|
|
$namespace = $this->take('package')->namespace; |
104
|
|
|
if (!array_key_exists($namespace, $psr4)) { |
105
|
|
|
$psr4 = [$namespace . '\\' => $this->take('package')->src] + $psr4; |
106
|
|
|
$autoload['psr-4'] = $psr4; |
107
|
|
|
$this->setItem('autoload', $autoload); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $autoload; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getRequire() |
117
|
|
|
{ |
118
|
|
|
return $this->getItem('require') ?: []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getRequireDev() |
125
|
|
|
{ |
126
|
|
|
return $this->getItem('require-dev') ?: []; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.