Skip to content

Commit e4e0838

Browse files
authored
Merge pull request #3 from xdevor/feat/parse-all-method
feat: add parseAll method
2 parents ebc8419 + d50d0d9 commit e4e0838

File tree

7 files changed

+234
-7
lines changed

7 files changed

+234
-7
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ jobs:
4141
run: composer install --prefer-dist --no-progress
4242

4343
- name: Run test suite
44-
run: composer run-script test & composer run-script test:type
44+
run: composer run-script test:cov & composer run-script test:type

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ composer require xdevor/composer-parser
1717

1818
## Usage
1919

20-
1. parse composer.json file of all installed PHP packages
20+
1. parse specific installed package by key
2121
```php
2222
...
2323
use Xdevor\ComposerParser\Parser;
@@ -29,6 +29,15 @@ use Xdevor\ComposerParser\Parser;
2929
(new Parser(__DIR__ . '/customize/path/installed.json'))->parse('the/package', 'name'); // parse customize path
3030
```
3131

32+
2. parse all installed package by key
33+
```php
34+
...
35+
use Xdevor\ComposerParser\Parser;
36+
...
37+
(new Parser())->parseAll($key = 'name'); // return name of all installed PHP packages
38+
(new Parser())->parseAll($key = 'extra.laravel.providers'); // return providers of all installed PHP packages
39+
```
40+
3241
## Contributing
3342

3443
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"scripts": {
3333
"test": "vendor/bin/pest",
34+
"test:cov": "vendor/bin/pest --coverage",
3435
"test:type": "vendor/bin/phpstan analyse src tests --ansi --memory-limit=-1 --level=5"
3536
},
3637
"config": {

src/Concerns/ParseInstalledPackages.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ public function setInstalledJsonPath(?string $installedJsonPath)
1616
$this->installedJsonPath = $installedJsonPath;
1717
}
1818

19+
protected function parseAllByKey(string $key): array
20+
{
21+
$packages = $this->installedPackages();
22+
$result = [];
23+
24+
foreach ($packages as $package) {
25+
$value = self::parseArray($package, $key);
26+
if (!is_null($value)) {
27+
$result[] = $value;
28+
}
29+
}
30+
31+
return $result;
32+
}
33+
1934
protected function parsePackageComposer(string $packageName, string $key, $default = null)
2035
{
2136
return self::parseArray($this->packageComposer($packageName), $key, $default);
@@ -38,9 +53,14 @@ protected function packageComposer(string $templatePackageName): array
3853
protected function installedPackages(): array
3954
{
4055
$packages = [];
41-
$installedJsonPath = $this->installedJsonPath ?? file_exists(__DIR__ . '/../../vendor/composer/installed.json')
42-
? __DIR__ . '/../../vendor/composer/installed.json'
43-
: __DIR__ . '/../../../../composer/installed.json';
56+
var_dump($this->installedJsonPath);
57+
if (is_null($this->installedJsonPath)) {
58+
$installedJsonPath = file_exists(__DIR__ . '/../../vendor/composer/installed.json')
59+
? __DIR__ . '/../../vendor/composer/installed.json'
60+
: __DIR__ . '/../../../../composer/installed.json';
61+
} else {
62+
$installedJsonPath = $this->installedJsonPath;
63+
}
4464

4565
if (file_exists($installedJsonPath)) {
4666
$installed = json_decode(file_get_contents($installedJsonPath), true);

src/Parser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public function parse(string $packageName, string $key, $default = null)
1717
{
1818
return $this->parsePackageComposer($packageName, $key, $default);
1919
}
20+
21+
public function parseAll(string $key): array
22+
{
23+
return $this->parseAllByKey($key);
24+
}
2025
}

tests/ParserTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,15 @@
3232
test('It can use custom installed.json path', function () {
3333
$packageName = 'filp/whoops';
3434
$actual = (new Parser(__DIR__ . '/../tests/assets/fakeInstalled.json'))->parse($packageName, 'extra.branch-alias.dev-master');
35-
expect($actual)->toBe('2.7-dev');
35+
expect($actual)->toBe('2.7-test-dev');
36+
});
37+
38+
test('It can parse all by key', function () {
39+
$actual = (new Parser(__DIR__ . '/../tests/assets/fakeInstalled.json'))->parseAll('name');
40+
expect($actual)->toBe(['filp/whoops', 'phar-io/manifest', 'phar-io/version', 'phpstan/phpstan']);
41+
});
42+
43+
test('It return empty array if key not exist on parse all method', function () {
44+
$actual = (new Parser(__DIR__ . '/../tests/assets/fakeInstalled.json'))->parseAll('xxx.ooo.xxx');
45+
expect($actual)->toBe([]);
3646
});

tests/assets/fakeInstalled.json

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"type": "library",
3333
"extra": {
3434
"branch-alias": {
35-
"dev-master": "2.7-dev"
35+
"dev-master": "2.7-test-dev"
3636
}
3737
},
3838
"installation-source": "dist",
@@ -73,6 +73,188 @@
7373
}
7474
],
7575
"install-path": "../filp/whoops"
76+
},
77+
{
78+
"name": "phar-io/manifest",
79+
"version": "2.0.3",
80+
"version_normalized": "2.0.3.0",
81+
"source": {
82+
"type": "git",
83+
"url": "https://github.com/phar-io/manifest.git",
84+
"reference": "97803eca37d319dfa7826cc2437fc020857acb53"
85+
},
86+
"dist": {
87+
"type": "zip",
88+
"url": "https://api.github.com/repos/phar-io/manifest/zipball/97803eca37d319dfa7826cc2437fc020857acb53",
89+
"reference": "97803eca37d319dfa7826cc2437fc020857acb53",
90+
"shasum": ""
91+
},
92+
"require": {
93+
"ext-dom": "*",
94+
"ext-phar": "*",
95+
"ext-xmlwriter": "*",
96+
"phar-io/version": "^3.0.1",
97+
"php": "^7.2 || ^8.0"
98+
},
99+
"time": "2021-07-20T11:28:43+00:00",
100+
"type": "library",
101+
"extra": {
102+
"branch-alias": {
103+
"dev-master": "2.0.x-dev"
104+
}
105+
},
106+
"installation-source": "dist",
107+
"autoload": {
108+
"classmap": [
109+
"src/"
110+
]
111+
},
112+
"notification-url": "https://packagist.org/downloads/",
113+
"license": [
114+
"BSD-3-Clause"
115+
],
116+
"authors": [
117+
{
118+
"name": "Arne Blankerts",
119+
"email": "[email protected]",
120+
"role": "Developer"
121+
},
122+
{
123+
"name": "Sebastian Heuer",
124+
"email": "[email protected]",
125+
"role": "Developer"
126+
},
127+
{
128+
"name": "Sebastian Bergmann",
129+
"email": "[email protected]",
130+
"role": "Developer"
131+
}
132+
],
133+
"description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)",
134+
"support": {
135+
"issues": "https://github.com/phar-io/manifest/issues",
136+
"source": "https://github.com/phar-io/manifest/tree/2.0.3"
137+
},
138+
"install-path": "../phar-io/manifest"
139+
},
140+
{
141+
"name": "phar-io/version",
142+
"version": "3.2.1",
143+
"version_normalized": "3.2.1.0",
144+
"source": {
145+
"type": "git",
146+
"url": "https://github.com/phar-io/version.git",
147+
"reference": "4f7fd7836c6f332bb2933569e566a0d6c4cbed74"
148+
},
149+
"dist": {
150+
"type": "zip",
151+
"url": "https://api.github.com/repos/phar-io/version/zipball/4f7fd7836c6f332bb2933569e566a0d6c4cbed74",
152+
"reference": "4f7fd7836c6f332bb2933569e566a0d6c4cbed74",
153+
"shasum": ""
154+
},
155+
"require": {
156+
"php": "^7.2 || ^8.0"
157+
},
158+
"time": "2022-02-21T01:04:05+00:00",
159+
"type": "library",
160+
"installation-source": "dist",
161+
"autoload": {
162+
"classmap": [
163+
"src/"
164+
]
165+
},
166+
"notification-url": "https://packagist.org/downloads/",
167+
"license": [
168+
"BSD-3-Clause"
169+
],
170+
"authors": [
171+
{
172+
"name": "Arne Blankerts",
173+
"email": "[email protected]",
174+
"role": "Developer"
175+
},
176+
{
177+
"name": "Sebastian Heuer",
178+
"email": "[email protected]",
179+
"role": "Developer"
180+
},
181+
{
182+
"name": "Sebastian Bergmann",
183+
"email": "[email protected]",
184+
"role": "Developer"
185+
}
186+
],
187+
"description": "Library for handling version information and constraints",
188+
"support": {
189+
"issues": "https://github.com/phar-io/version/issues",
190+
"source": "https://github.com/phar-io/version/tree/3.2.1"
191+
},
192+
"install-path": "../phar-io/version"
193+
},
194+
{
195+
"name": "phpstan/phpstan",
196+
"version": "1.10.8",
197+
"version_normalized": "1.10.8.0",
198+
"source": {
199+
"type": "git",
200+
"url": "https://github.com/phpstan/phpstan.git",
201+
"reference": "0166aef76e066f0dd2adc2799bdadfa1635711e9"
202+
},
203+
"dist": {
204+
"type": "zip",
205+
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/0166aef76e066f0dd2adc2799bdadfa1635711e9",
206+
"reference": "0166aef76e066f0dd2adc2799bdadfa1635711e9",
207+
"shasum": ""
208+
},
209+
"require": {
210+
"php": "^7.2|^8.0"
211+
},
212+
"conflict": {
213+
"phpstan/phpstan-shim": "*"
214+
},
215+
"time": "2023-03-24T10:28:16+00:00",
216+
"bin": [
217+
"phpstan",
218+
"phpstan.phar"
219+
],
220+
"type": "library",
221+
"installation-source": "dist",
222+
"autoload": {
223+
"files": [
224+
"bootstrap.php"
225+
]
226+
},
227+
"notification-url": "https://packagist.org/downloads/",
228+
"license": [
229+
"MIT"
230+
],
231+
"description": "PHPStan - PHP Static Analysis Tool",
232+
"keywords": [
233+
"dev",
234+
"static analysis"
235+
],
236+
"support": {
237+
"docs": "https://phpstan.org/user-guide/getting-started",
238+
"forum": "https://github.com/phpstan/phpstan/discussions",
239+
"issues": "https://github.com/phpstan/phpstan/issues",
240+
"security": "https://github.com/phpstan/phpstan/security/policy",
241+
"source": "https://github.com/phpstan/phpstan-src"
242+
},
243+
"funding": [
244+
{
245+
"url": "https://github.com/ondrejmirtes",
246+
"type": "github"
247+
},
248+
{
249+
"url": "https://github.com/phpstan",
250+
"type": "github"
251+
},
252+
{
253+
"url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan",
254+
"type": "tidelift"
255+
}
256+
],
257+
"install-path": "../phpstan/phpstan"
76258
}
77259
]
78260
}

0 commit comments

Comments
 (0)