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