1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cherrypulp\LaravelPackageGenerator\Commands\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
8
|
|
|
use Cherrypulp\LaravelPackageGenerator\Exceptions\RuntimeException; |
9
|
|
|
|
10
|
|
|
trait ChangesComposerJson |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Register package in composer.json. |
14
|
|
|
* |
15
|
|
|
* @param $vendor |
16
|
|
|
* @param $package |
17
|
|
|
* @param $relPackagePath |
18
|
|
|
* |
19
|
|
|
* @throws RuntimeException |
20
|
|
|
*/ |
21
|
|
|
protected function registerPackage($vendor, $package, $relPackagePath) |
22
|
|
|
{ |
23
|
|
|
$this->info('Register package in composer.json.'); |
|
|
|
|
24
|
|
|
|
25
|
|
|
$composerJson = $this->loadComposerJson(); |
26
|
|
|
|
27
|
|
|
if (! isset($composerJson['repositories'])) { |
28
|
|
|
Arr::set($composerJson, 'repositories', []); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$filtered = array_filter($composerJson['repositories'], function ($repository) use ($relPackagePath) { |
32
|
|
|
return $repository['type'] === 'path' |
33
|
|
|
&& $repository['url'] === $relPackagePath; |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
if (count($filtered) === 0) { |
37
|
|
|
$this->info('Register composer repository for package.'); |
38
|
|
|
|
39
|
|
|
$composerJson['repositories'][] = (object) [ |
40
|
|
|
'type' => 'path', |
41
|
|
|
'url' => $relPackagePath, |
42
|
|
|
]; |
43
|
|
|
} else { |
44
|
|
|
$this->info('Composer repository for package is already registered.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
Arr::set($composerJson, "require.$vendor/$package", 'dev-master'); |
48
|
|
|
|
49
|
|
|
$this->saveComposerJson($composerJson); |
50
|
|
|
|
51
|
|
|
$this->info('Package was successfully registered in composer.json.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Unregister package from composer.json. |
56
|
|
|
* |
57
|
|
|
* @param $vendor |
58
|
|
|
* @param $package |
59
|
|
|
* |
60
|
|
|
* @throws FileNotFoundException |
61
|
|
|
* @throws RuntimeException |
62
|
|
|
*/ |
63
|
|
|
protected function unregisterPackage($vendor, $package, $relPackagePath) |
64
|
|
|
{ |
65
|
|
|
$this->info('Unregister package from composer.json.'); |
66
|
|
|
|
67
|
|
|
$composerJson = $this->loadComposerJson(); |
68
|
|
|
|
69
|
|
|
unset($composerJson['require']["$vendor\\$package\\"]); |
70
|
|
|
|
71
|
|
|
$repositories = array_filter($composerJson['repositories'], function ($repository) use ($relPackagePath) { |
72
|
|
|
return $repository['type'] !== 'path' |
73
|
|
|
|| $repository['url'] !== $relPackagePath; |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
$composerJson['repositories'] = $repositories; |
77
|
|
|
|
78
|
|
|
if (count($composerJson['repositories']) === 0) { |
79
|
|
|
unset($composerJson['repositories']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->saveComposerJson($composerJson); |
83
|
|
|
|
84
|
|
|
$this->info('Package was successfully unregistered from composer.json.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Load and parse content of composer.json. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
* |
92
|
|
|
* @throws FileNotFoundException |
93
|
|
|
* @throws RuntimeException |
94
|
|
|
*/ |
95
|
|
|
protected function loadComposerJson() |
96
|
|
|
{ |
97
|
|
|
$composerJsonPath = $this->getComposerJsonPath(); |
98
|
|
|
|
99
|
|
|
if (! File::exists($composerJsonPath)) { |
100
|
|
|
throw new FileNotFoundException('composer.json does not exist'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$composerJsonContent = File::get($composerJsonPath); |
104
|
|
|
$composerJson = json_decode($composerJsonContent, true); |
105
|
|
|
|
106
|
|
|
if (! is_array($composerJson)) { |
107
|
|
|
throw new RuntimeException("Invalid composer.json file [$composerJsonPath]"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $composerJson; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $composerJson |
115
|
|
|
* |
116
|
|
|
* @throws RuntimeException |
117
|
|
|
*/ |
118
|
|
|
protected function saveComposerJson($composerJson) |
119
|
|
|
{ |
120
|
|
|
$newComposerJson = json_encode( |
121
|
|
|
$composerJson, |
122
|
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$composerJsonPath = $this->getComposerJsonPath(); |
126
|
|
|
if (File::put($composerJsonPath, $newComposerJson) === false) { |
127
|
|
|
throw new RuntimeException("Cannot write to composer.json [$composerJsonPath]"); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get composer.json path. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
protected function getComposerJsonPath() |
137
|
|
|
{ |
138
|
|
|
if (getcwd() !== base_path()) { |
|
|
|
|
139
|
|
|
return getcwd()."/"."composer.json"; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return base_path('composer.json'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|