1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Composer plugin for HiDev |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hidev-composer |
7
|
|
|
* @package hidev-composer |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hidev\composer\components; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* `composer.json` config file. |
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ComposerJson extends \hidev\base\ConfigFile |
19
|
|
|
{ |
20
|
|
|
protected $_file = 'composer.json'; |
21
|
|
|
|
22
|
|
|
public function load() |
23
|
|
|
{ |
24
|
|
|
parent::load(); |
25
|
|
|
$sets = [ |
26
|
|
|
'name' => $this->getName(), |
|
|
|
|
27
|
|
|
'type' => $this->getType(), |
28
|
|
|
'description' => $this->take('package')->title, |
29
|
|
|
'keywords' => $this->take('package')->keywords, |
30
|
|
|
'homepage' => $this->take('package')->homepage, |
31
|
|
|
'license' => $this->take('package')->license, |
32
|
|
|
'support' => $this->getSupport(), |
33
|
|
|
'authors' => $this->getAuthors(), |
34
|
|
|
'require' => $this->getRequire(), |
35
|
|
|
'require-dev' => $this->getRequireDev(), |
36
|
|
|
'autoload' => $this->getAutoload(), |
37
|
|
|
]; |
38
|
|
|
$this->setItems($sets, 'first'); |
39
|
|
|
foreach (['require', 'require-dev'] as $k) { |
40
|
|
|
if (!$this->get($k)) { |
41
|
|
|
$this->delete($k); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Converts hidev type to composer type. |
48
|
|
|
* TODO composer type can be different from package type. |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getType() |
52
|
|
|
{ |
53
|
|
|
return $this->take('package')->type; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Converts hidev full name to composer name. |
58
|
|
|
* TODO composer name can be different from package full name. |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getName() |
62
|
|
|
{ |
63
|
|
|
return $this->take('package')->fullName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getFullName() |
70
|
|
|
{ |
71
|
|
|
return $this->getName(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getSupport() |
75
|
|
|
{ |
76
|
|
|
return array_merge(array_filter([ |
77
|
|
|
'email' => $this->take('vendor')->email, |
78
|
|
|
'source' => $this->take('package')->source, |
79
|
|
|
'issues' => $this->take('package')->issues, |
80
|
|
|
'wiki' => $this->take('package')->wiki, |
81
|
|
|
'forum' => $this->take('package')->forum, |
82
|
|
|
]), (array) $this->getItem('support')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getAuthors() |
86
|
|
|
{ |
87
|
|
|
if ($this->take('package')->authors) { |
88
|
|
|
foreach ($this->take('package')->authors as $all_data) { |
89
|
|
|
$data = []; |
90
|
|
|
foreach (['name', 'role', 'email', 'homepage'] as $k) { |
91
|
|
|
if (!empty($all_data[$k])) { |
92
|
|
|
$data[$k] = $all_data[$k]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
$res[] = $data; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $res; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getAutoload() |
103
|
|
|
{ |
104
|
|
|
$autoload = $this->rawItem('autoload'); |
105
|
|
|
$psr4 = $autoload['psr-4'] ?: []; |
106
|
|
|
$namespace = $this->take('package')->namespace; |
107
|
|
|
if (!array_key_exists($namespace, $psr4)) { |
108
|
|
|
$psr4 = [$namespace . '\\' => $this->take('package')->src] + $psr4; |
109
|
|
|
$autoload['psr-4'] = $psr4; |
110
|
|
|
$this->setItem('autoload', $autoload); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $autoload; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function getRequire() |
120
|
|
|
{ |
121
|
|
|
return $this->getItem('require') ?: []; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getRequireDev() |
128
|
|
|
{ |
129
|
|
|
return $this->getItem('require-dev') ?: []; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|