Completed
Pull Request — master (#5)
by David
02:09
created

Dumper::getAssetsBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\Discovery;
5
6
use Composer\Composer;
7
use Composer\Factory;
8
use Composer\IO\IOInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
/**
12
 * Class in charge of generating the .discovery directory with the Discovery class and files.
13
 */
14
class Dumper
15
{
16
    /**
17
     * @var Composer
18
     */
19
    private $composer;
20
    /**
21
     * @var IOInterface
22
     */
23
    private $io;
24
25
    public function __construct(Composer $composer, IOInterface $io)
26
    {
27
        $this->composer = $composer;
28
        $this->io = $io;
29
    }
30
31
    public function dumpDiscoveryFiles()
32
    {
33
        $fileSystem = new Filesystem();
34
35
        $localRepos = $this->composer->getRepositoryManager()->getLocalRepository();
36
        $assetTypes = $this->getAssetsBuilder()->findAssetTypes($localRepos);
37
38
        // Let's get an array of values, indexed by asset type (to store in the discovery_values.php file)
39
        $values = array_map(function (AssetType $assetType) {
40
            return $assetType->getValues();
41
        }, $assetTypes);
42
43
        $fileSystem->dumpFile('.discovery/discovery_values.php', '<?php
44
return '.var_export($values, true).";\n");
45
46
        // Let's get an array of assetTypes, indexed by asset type (to store in the discovery_asset_types.php file)
47
        $assetTypes = array_map(function (AssetType $assetType) {
48
            return $assetType->jsonSerialize();
49
        }, $assetTypes);
50
51
        $fileSystem->dumpFile('.discovery/discovery_asset_types.php', '<?php
52
return '.var_export($assetTypes, true).";\n");
53
54
        // Let's copy the Discovery class in the .discovery directory. This is needed because otherwise, we have no way to "find" the .discovery directory easily.
55
        $fileSystem->dumpFile('.discovery/Discovery.php', file_get_contents(__DIR__.'/Discovery.php.tpl'));
56
    }
57
58
    /**
59
     * @return AssetsBuilder
60
     */
61
    private function getAssetsBuilder() : AssetsBuilder
62
    {
63
        $installationManager = $this->composer->getInstallationManager();
64
        $rootDir = dirname(Factory::getComposerFile());
65
        return new AssetsBuilder($installationManager, $this->io, $rootDir);
66
    }
67
}