Newer
Older
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
<?php
namespace LakeDrops\Component\Composer;
use Composer\Composer;
use Composer\Json\JsonFile;
/**
* Manages composer.json files.
*/
final class Utils {
/**
* The composer object running this session.
*
* @var \Composer\Composer
*/
protected $composer;
/**
* Content of the project's composer.json file.
*
* @var array
*/
protected $content;
/**
* The path of the root project's directory.
*
* @var string
*/
protected $sourcePath;
/**
* The full filename of the composer.json file.
*
* @var string
*/
protected $composerJsonPath;
/**
* Utils constructor.
*
* @param \Composer\Composer $composer
* The composer object running this session.
*/
public function __construct(Composer $composer) {
$this->composer = $composer;
$this->sourcePath = getcwd() . $composer->getPackage()->getTargetDir();
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
$this->composerJsonPath = $this->sourcePath . '/composer.json';
$this->read();
}
/**
* Read the current content of the project's composer.json file.
*
* @return $this
*/
public function read() {
$this->content = [];
if (file_exists($this->composerJsonPath)) {
$jsonFile = new JsonFile($this->composerJsonPath);
$this->content = $jsonFile->read();
}
return $this;
}
/**
* Write the config to composer.json file.
*
* @return $this
*/
public function write() {
$jsonFile = new JsonFile($this->composerJsonPath);
if (file_exists($this->composerJsonPath)) {
$content = $jsonFile->read();
if (md5(json_encode($content)) == md5(json_encode($this->content))) {
// Nothing has changed.
return $this;
}
}
$jsonFile->write($this->content);
return $this;
}
/**
* Get settings for [$key].
*
* @param string $key
* The key for the section.
*
* @return array|string
* The settings of the key.
*/
public function getSection($key) {
return isset($this->content[$key]) ? $this->content[$key] : [];
}
/**
* Get settings for [$key1][$key2].
*
* @param string $key1
* The key for the section.
* @param string $key2
* The key for the sub-section.
*
* @return array|string
* The settings of the key.
*/
public function getSubSection($key1, $key2) {
$section = $this->getSection($key1);
return isset($section[$key2]) ? $section[$key2] : [];
}
/**
* Set settings for [$key].
*
* @param string $key
* The key for the section.
* @param array|string $value
* The value for the section.
*
* @return $this
*/
public function setSection($key, $value) {
$this->content[$key] = $value;
return $this;
}
/**
* Set settings for [$key1][$key2].
*
* @param string $key1
* The key for the section.
* @param string $key2
* The key for the sub-section.
* @param array|string $value
* The value for the sub-section.
*
* @return $this
*/
public function setSubSection($key1, $key2, $value) {
$this->content[$key1][$key2] = $value;
return $this;
}
}