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
|
1 |
|
public function __construct($workingDir) |
23
|
|
|
{ |
24
|
1 |
|
$this->workingDir = \realpath($workingDir); |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
1 |
|
public function getComposer() |
31
|
|
|
{ |
32
|
1 |
|
$composerFile = $this->workingDir . '/composer.json'; |
33
|
|
|
|
34
|
1 |
|
if (!\file_exists($composerFile)) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
if (false !== $content = \file_get_contents($composerFile)) { |
39
|
1 |
|
return \json_decode($content, true); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $configKey |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
1 |
|
public function getConfig($configKey) |
51
|
|
|
{ |
52
|
1 |
|
$composer = $this->getComposer() + ['config' => []]; |
53
|
|
|
|
54
|
1 |
|
return $composer['config'][$configKey] ?? null; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function getExtra() |
58
|
|
|
{ |
59
|
1 |
|
return $this->getComposer()['extra'] ?? []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
1 |
|
public function getName() |
66
|
|
|
{ |
67
|
1 |
|
return $this->hasName() ? $this->getComposer()['name'] : ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
1 |
|
public function getProject() |
74
|
|
|
{ |
75
|
1 |
|
return $this->hasName() ? \explode('/', $this->getName())[1] : ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public function getType() |
82
|
|
|
{ |
83
|
1 |
|
return $this->hasType() ? $this->getComposer()['type'] : ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
1 |
|
public function getVendor() |
90
|
|
|
{ |
91
|
1 |
|
return $this->hasName() ? \explode('/', $this->getName())[0] : ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
1 |
|
public function hasName() |
98
|
|
|
{ |
99
|
1 |
|
return isset($this->getComposer()['name']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
1 |
|
public function hasType() |
106
|
|
|
{ |
107
|
1 |
|
return isset($this->getComposer()->type); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $workingDir |
112
|
|
|
* |
113
|
|
|
* @return Composer |
114
|
|
|
*/ |
115
|
1 |
|
public function setWorkingDir($workingDir) |
116
|
|
|
{ |
117
|
1 |
|
$this->workingDir = $workingDir; |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|