|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Author: mickael Louzet @micklouzet |
|
5
|
|
|
* File: FileParser.php |
|
6
|
|
|
* Created: 05/12/2019 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace ComposerLockParser; |
|
12
|
|
|
|
|
13
|
|
|
use ComposerLockFileParser\ParserCollection; |
|
|
|
|
|
|
14
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
15
|
|
|
|
|
16
|
|
|
class FileParser implements FileParserInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritDoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function parse(string $filePath): ParserCollection |
|
22
|
|
|
{ |
|
23
|
|
|
if (!\is_file($filePath)) { |
|
24
|
|
|
return new ParserCollection(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (!$json = \file_get_contents($filePath)) { |
|
28
|
|
|
return new ParserCollection(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$data = \json_decode($json, true); |
|
32
|
|
|
$components = []; |
|
33
|
|
|
|
|
34
|
|
|
if (!$data || !isset($data['packages'])) { |
|
35
|
|
|
return new ParserCollection(); |
|
36
|
|
|
} |
|
37
|
|
|
foreach ($data['packages'] as $vendor => $package) { |
|
38
|
|
|
$accessor = PropertyAccess::createPropertyAccessorBuilder() |
|
39
|
|
|
->enableExceptionOnInvalidIndex() |
|
40
|
|
|
->getPropertyAccessor(); |
|
41
|
|
|
|
|
42
|
|
|
$array = []; |
|
43
|
|
|
|
|
44
|
|
|
$component = [ |
|
45
|
|
|
'name' => $package['name'], |
|
46
|
|
|
'version' => $package['version'], |
|
47
|
|
|
'source' => $package['source'], |
|
48
|
|
|
'require' => $package['require'] ?? [], |
|
49
|
|
|
'description' => $package['description'], |
|
50
|
|
|
'keywords' => $package['keywords'], |
|
51
|
|
|
'time' => $package['time'], |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
$accessor->setValue($array, "[name]", $component); |
|
55
|
|
|
$components[] = $component; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return new ParserCollection($components); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// public function byVendor(string $vendor) |
|
62
|
|
|
// { |
|
63
|
|
|
// if (0 !== \strpos($Package['name'], "vendor/")) { |
|
64
|
|
|
// continue; |
|
65
|
|
|
// } |
|
66
|
|
|
// } |
|
67
|
|
|
} |
|
68
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: