Completed
Push — master ( 58a2e7...d6a573 )
by David
05:06
created

Dumper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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