1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Discovery; |
5
|
|
|
|
6
|
|
|
use Composer\Installer\InstallationManager; |
7
|
|
|
use Composer\IO\IOInterface; |
8
|
|
|
use Composer\Package\PackageInterface; |
9
|
|
|
use Composer\Package\RootPackageInterface; |
10
|
|
|
use Composer\Repository\RepositoryInterface; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use TheCodingMachine\Discovery\Utils\JsonException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Transforms discovery.json files into arrays of assets. |
16
|
|
|
*/ |
17
|
|
|
class AssetsBuilder |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var InstallationManager |
23
|
|
|
*/ |
24
|
|
|
private $installationManager; |
25
|
|
|
/** |
26
|
|
|
* @var IOInterface |
27
|
|
|
*/ |
28
|
|
|
private $io; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $rootDir; |
33
|
|
|
|
34
|
|
|
public function __construct(InstallationManager $installationManager, IOInterface $io, string $rootDir) |
35
|
|
|
{ |
36
|
|
|
$this->installationManager = $installationManager; |
37
|
|
|
$this->io = $io; |
38
|
|
|
$this->rootDir = $rootDir; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Find discovery.json files in the passed repository and builds an asset type. |
43
|
|
|
* |
44
|
|
|
* @param RepositoryInterface $repository |
45
|
|
|
* @param RootPackageInterface $rootPackage |
|
|
|
|
46
|
|
|
* @return array|AssetType[] |
47
|
|
|
*/ |
48
|
|
|
public function findAssetTypes(RepositoryInterface $repository) : array |
49
|
|
|
{ |
50
|
|
|
$unorderedPackagesList = $repository->getPackages(); |
51
|
|
|
|
52
|
|
|
$orderedPackageList = PackagesOrderer::reorderPackages($unorderedPackagesList); |
53
|
|
|
|
54
|
|
|
$packages = array_filter($orderedPackageList, function (PackageInterface $package) { |
55
|
|
|
$packageInstallPath = $this->getInstallPath($package); |
56
|
|
|
|
57
|
|
|
return file_exists($packageInstallPath.'/discovery.json'); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
return $this->buildAssetTypes($packages); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Builds the AssetTypes that will be exported in the generated TheCodingMachine\Discovery class. |
65
|
|
|
* |
66
|
|
|
* @param PackageInterface[] $discoveryPackages |
67
|
|
|
* |
68
|
|
|
* @return AssetType[] An array of AssetType, indexed by asset type name. |
69
|
|
|
*/ |
70
|
|
|
public function buildAssetTypes(array $discoveryPackages) : array |
71
|
|
|
{ |
72
|
|
|
/* @var $assetTypes AssetType[] */ |
73
|
|
|
$assetTypes = []; |
74
|
|
|
|
75
|
|
|
foreach ($discoveryPackages as $package) { |
76
|
|
|
try { |
77
|
|
|
$assetOperationsByType = $this->getDiscoveryJson($package); |
78
|
|
|
// TODO display warnings if problem with discovery.json without crashing! |
79
|
|
|
|
80
|
|
|
foreach ($assetOperationsByType as $type => $assetOperations) { |
81
|
|
|
$assetTypes[$type] = $assetTypes[$type] ?? new AssetType($type); |
82
|
|
|
|
83
|
|
|
foreach ($assetOperations as $assetOperation) { |
84
|
|
|
$assetTypes[$type]->addAssetOperation($assetOperation); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} catch (JsonException $exception) { |
88
|
|
|
$this->io->writeError($exception->getMessage()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $assetTypes; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the parsed JSON of the discovery.json file of a package. |
97
|
|
|
* |
98
|
|
|
* @param PackageInterface $package |
99
|
|
|
* |
100
|
|
|
* @return AssetOperation[][] |
101
|
|
|
* |
102
|
|
|
* @throws \TheCodingMachine\Discovery\Utils\JsonException |
103
|
|
|
*/ |
104
|
|
|
private function getDiscoveryJson(PackageInterface $package) : array |
105
|
|
|
{ |
106
|
|
|
$packageInstallPath = $this->getInstallPath($package); |
107
|
|
|
|
108
|
|
|
$fileSystem = new Filesystem(); |
109
|
|
|
|
110
|
|
|
$packageDir = $fileSystem->makePathRelative($packageInstallPath, realpath($this->rootDir)); |
111
|
|
|
|
112
|
|
|
$path = $packageInstallPath.'/discovery.json'; |
113
|
|
|
|
114
|
|
|
$discoveryFileLoader = new DiscoveryFileLoader(); |
115
|
|
|
|
116
|
|
|
return $discoveryFileLoader->loadDiscoveryFile(new \SplFileInfo($path), $package->getName(), $packageDir); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getInstallPath(PackageInterface $package) : string |
120
|
|
|
{ |
121
|
|
|
if ($package instanceof RootPackageInterface) { |
122
|
|
|
return getcwd(); |
123
|
|
|
} else { |
124
|
|
|
return $this->installationManager->getInstallPath($package); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.