1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\Maintenance\Util; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\Core\Extensible; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The composer loader class is responsible for dealing directly with composer.json and composer.lock files, |
10
|
|
|
* in terms of loading and parsing their contents. |
11
|
|
|
* |
12
|
|
|
* Any requirements for dealing with these files directly should use this class as a proxy. |
13
|
|
|
*/ |
14
|
|
|
class ComposerLoader |
15
|
|
|
{ |
16
|
|
|
use Extensible; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var object |
20
|
|
|
*/ |
21
|
|
|
protected $json; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var object |
25
|
|
|
*/ |
26
|
|
|
protected $lock; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $basePath; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $basePath |
35
|
|
|
* @throws Exception |
36
|
|
|
*/ |
37
|
|
|
public function __construct($basePath = '') |
38
|
|
|
{ |
39
|
|
|
if ($basePath) { |
40
|
|
|
$this->setBasePath($basePath); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->build(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Load and build the composer.json and composer.lock files |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
* @throws Exception If either file could not be loaded |
51
|
|
|
*/ |
52
|
|
|
public function build() |
53
|
|
|
{ |
54
|
|
|
$basePath = $this->getBasePath(); |
55
|
|
|
$composerJson = file_get_contents($basePath . '/composer.json'); |
56
|
|
|
$composerLock = file_get_contents($basePath . '/composer.lock'); |
57
|
|
|
|
58
|
|
|
if (!$composerJson || !$composerLock) { |
59
|
|
|
throw new Exception('composer.json or composer.lock could not be found!'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->setJson(json_decode($composerJson)); |
63
|
|
|
$this->setLock(json_decode($composerLock)); |
64
|
|
|
|
65
|
|
|
$this->extend('onAfterBuild'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param object $json |
70
|
|
|
* @return ComposerLoader |
71
|
|
|
*/ |
72
|
|
|
public function setJson($json) |
73
|
|
|
{ |
74
|
|
|
$this->json = $json; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return object |
80
|
|
|
*/ |
81
|
|
|
public function getJson() |
82
|
|
|
{ |
83
|
|
|
return $this->json; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param object $lock |
88
|
|
|
* @return ComposerLoader |
89
|
|
|
*/ |
90
|
|
|
public function setLock($lock) |
91
|
|
|
{ |
92
|
|
|
$this->lock = $lock; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return object |
98
|
|
|
*/ |
99
|
|
|
public function getLock() |
100
|
|
|
{ |
101
|
|
|
return $this->lock; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the base path, if not specified the default will be `BASE_PATH` |
106
|
|
|
* |
107
|
|
|
* @param string $basePath |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function setBasePath($basePath) |
111
|
|
|
{ |
112
|
|
|
$this->basePath = $basePath; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getBasePath() |
120
|
|
|
{ |
121
|
|
|
return $this->basePath ?: BASE_PATH; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|