1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace phptaskman\core\Services; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Parse composer package information. |
9
|
|
|
*/ |
10
|
|
|
final class Composer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $workingDir; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Composer constructor. |
19
|
|
|
* |
20
|
|
|
* @param string $workingDir |
21
|
|
|
*/ |
22
|
|
|
public function __construct($workingDir) |
23
|
|
|
{ |
24
|
|
|
$this->workingDir = $workingDir; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function getComposer() |
31
|
|
|
{ |
32
|
|
|
if (false !== $content = \file_get_contents($this->workingDir . '/composer.json')) { |
33
|
|
|
return \json_decode($content, true); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return []; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $configKey |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function getConfig($configKey) |
45
|
|
|
{ |
46
|
|
|
$composer = $this->getComposer() + ['config' => []]; |
47
|
|
|
|
48
|
|
|
return $composer['config'][$configKey] ?? null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getExtra() |
52
|
|
|
{ |
53
|
|
|
return $this->getComposer()['extra'] ?? []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getName() |
60
|
|
|
{ |
61
|
|
|
return $this->hasName() ? $this->getComposer()->name : ''; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getProject() |
68
|
|
|
{ |
69
|
|
|
return $this->hasName() ? \explode('/', $this->getName())[1] : ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getType() |
76
|
|
|
{ |
77
|
|
|
return $this->hasType() ? $this->getComposer()['type'] : ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getVendor() |
84
|
|
|
{ |
85
|
|
|
return $this->hasName() ? \explode('/', $this->getName())[0] : ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function hasName() |
92
|
|
|
{ |
93
|
|
|
return isset($this->getComposer()->name); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function hasType() |
100
|
|
|
{ |
101
|
|
|
return isset($this->getComposer()->type); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $workingDir |
106
|
|
|
* |
107
|
|
|
* @return Composer |
108
|
|
|
*/ |
109
|
|
|
public function setWorkingDir($workingDir) |
110
|
|
|
{ |
111
|
|
|
$this->workingDir = $workingDir; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|