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