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 Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
use TheCodingMachine\Discovery\Utils\JsonException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Transforms discovery.json files into arrays of assets. |
14
|
|
|
*/ |
15
|
|
|
class AssetsBuilder |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var InstallationManager |
21
|
|
|
*/ |
22
|
|
|
private $installationManager; |
23
|
|
|
/** |
24
|
|
|
* @var IOInterface |
25
|
|
|
*/ |
26
|
|
|
private $io; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $rootDir; |
31
|
|
|
|
32
|
|
|
public function __construct(InstallationManager $installationManager, IOInterface $io, string $rootDir) |
33
|
|
|
{ |
34
|
|
|
$this->installationManager = $installationManager; |
35
|
|
|
$this->io = $io; |
36
|
|
|
$this->rootDir = $rootDir; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Builds the AssetTypes that will be exported in the generated TheCodingMachine\Discovery class. |
42
|
|
|
* |
43
|
|
|
* @param PackageInterface[] $discoveryPackages |
44
|
|
|
* |
45
|
|
|
* @return AssetType[] An array of AssetType, indexed by asset type name. |
46
|
|
|
*/ |
47
|
|
|
public function buildAssetTypes(array $discoveryPackages) : array |
48
|
|
|
{ |
49
|
|
|
/* @var $assetTypes AssetType[] */ |
50
|
|
|
$assetTypes = []; |
51
|
|
|
|
52
|
|
|
foreach ($discoveryPackages as $package) { |
53
|
|
|
try { |
54
|
|
|
$assetOperationsByType = $this->getDiscoveryJson($package); |
55
|
|
|
// TODO display warnings if problem with discovery.json without crashing! |
56
|
|
|
|
57
|
|
|
foreach ($assetOperationsByType as $type => $assetOperations) { |
58
|
|
|
$assetTypes[$type] = $assetTypes[$type] ?? new AssetType($type); |
59
|
|
|
|
60
|
|
|
foreach ($assetOperations as $assetOperation) { |
61
|
|
|
$assetTypes[$type]->addAssetOperation($assetOperation); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} catch (JsonException $exception) { |
65
|
|
|
$this->io->writeError($exception->getMessage()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $assetTypes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the parsed JSON of the discovery.json file of a package. |
74
|
|
|
* |
75
|
|
|
* @param PackageInterface $package |
76
|
|
|
* |
77
|
|
|
* @return AssetOperation[][] |
78
|
|
|
* |
79
|
|
|
* @throws \TheCodingMachine\Discovery\Utils\JsonException |
80
|
|
|
*/ |
81
|
|
|
private function getDiscoveryJson(PackageInterface $package) : array |
82
|
|
|
{ |
83
|
|
|
$packageInstallPath = $this->installationManager->getInstallPath($package); |
84
|
|
|
|
85
|
|
|
$fileSystem = new Filesystem(); |
86
|
|
|
|
87
|
|
|
$packageDir = $fileSystem->makePathRelative($packageInstallPath, realpath($this->rootDir)); |
88
|
|
|
|
89
|
|
|
$path = $packageInstallPath.'/discovery.json'; |
90
|
|
|
|
91
|
|
|
$discoveryFileLoader = new DiscoveryFileLoader(); |
92
|
|
|
|
93
|
|
|
return $discoveryFileLoader->loadDiscoveryFile(new \SplFileInfo($path), $package->getName(), $packageDir); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|