Completed
Pull Request — 1.x (#352)
by Akihito
01:26
created

CompilePreload::getRelativePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use ArrayObject;
8
use BEAR\AppMeta\AbstractAppMeta;
9
use BEAR\AppMeta\Meta;
10
11
use function preg_quote;
12
use function preg_replace;
13
use function realpath;
14
use function sprintf;
15
use function strpos;
16
17
final class CompilePreload
18
{
19
    /** @var NewInstance */
20
    private $newInstance;
21
22
    /** @var CompileAutoload */
23
    private $dumpAutoload;
24
25
    /** @var ArrayObject<int, string> */
26
    private $classes;
27
28
    /** @var FilePutContents */
29
    private $filePutContents;
30
31
    /** @var string */
32
    private $context;
33
34
    /**
35
     * @param ArrayObject<int, string> $classes
36
     */
37
    public function __construct(NewInstance $newInstance, CompileAutoload $dumpAutoload, FilePutContents $filePutContents, ArrayObject $classes, string $context)
38
    {
39
        $this->newInstance = $newInstance;
40
        $this->dumpAutoload = $dumpAutoload;
41
        $this->classes = $classes;
42
        $this->filePutContents = $filePutContents;
43
        $this->context = $context;
44
    }
45
46
    public function __invoke(AbstractAppMeta $appMeta, string $context): string
47
    {
48
        $this->loadResources($appMeta->name, $context, $appMeta->appDir);
49
        /** @var list<string> $classes */
0 ignored issues
show
Documentation introduced by
The doc-type list<string> could not be parsed: Expected "|" or "end of type", but got "<" at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
50
        $classes = (array) $this->classes;
51
        $paths = $this->dumpAutoload->getPaths($classes);
52
        $requiredOnceFile = '';
53
        foreach ($paths as $path) {
54
            $requiredOnceFile .= sprintf(
55
                "require_once %s';\n",
56
                $this->getRelativePath($appMeta->appDir, $path)
57
            );
58
        }
59
60
        $preloadFile = sprintf("<?php
61
62
// %s preload
63
64
require __DIR__ . '/vendor/autoload.php';
65
66
%s", $this->context, $requiredOnceFile);
67
        $fileName = realpath($appMeta->appDir) . '/preload.php';
68
        ($this->filePutContents)($fileName, $preloadFile);
69
70
        return $fileName;
71
    }
72
73
    public function loadResources(string $appName, string $context, string $appDir): void
74
    {
75
        $meta = new Meta($appName, $context, $appDir);
76
77
        $resMetas = $meta->getGenerator('*');
78
        foreach ($resMetas as $resMeta) {
79
            ($this->newInstance)($resMeta->class);
80
        }
81
    }
82
83
    private function getRelativePath(string $rootDir, string $path): string
84
    {
85
        $dir = (string) realpath($rootDir);
86
        if (strpos($path, $dir) !== false) {
87
            return (string) preg_replace('#^' . preg_quote($dir, '#') . '#', "__DIR__ . '", $path);
88
        }
89
90
        return $path;
91
    }
92
}
93