CompilePreload::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 25
rs 9.7998
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 realpath;
12
use function sprintf;
13
14
final class CompilePreload
15
{
16
    /** @param ArrayObject<int, string> $classes */
17
    public function __construct(
18
        private FakeRun $fakeRun,
19
        private NewInstance $newInstance,
20
        private CompileAutoload $dumpAutoload,
21
        private FilePutContents $filePutContents,
22
        private ArrayObject $classes,
23
        private string $context,
24
    ) {
25
        $this->fakeRun = $fakeRun;
26
    }
27
28
    public function __invoke(AbstractAppMeta $appMeta, string $context): string
29
    {
30
        ($this->fakeRun)();
31
        $this->loadResources($appMeta->name, $context, $appMeta->appDir);
32
        /** @var list<string> $classes */
33
        $classes = (array) $this->classes;
34
        $paths = $this->dumpAutoload->getPaths($classes);
0 ignored issues
show
Bug introduced by
$classes of type BEAR\Package\Compiler\list is incompatible with the type array expected by parameter $classes of BEAR\Package\Compiler\CompileAutoload::getPaths(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $paths = $this->dumpAutoload->getPaths(/** @scrutinizer ignore-type */ $classes);
Loading history...
35
        $requiredOnceFile = '';
36
        foreach ($paths as $path) {
37
            $requiredOnceFile .= sprintf(
38
                "require %s;\n",
39
                $path,
40
            );
41
        }
42
43
        $preloadFile = sprintf("<?php
44
45
// %s preload
46
require __DIR__ . '/vendor/autoload.php';
47
48
%s", $this->context, $requiredOnceFile);
49
        $fileName = realpath($appMeta->appDir) . '/preload.php';
50
        ($this->filePutContents)($fileName, $preloadFile);
51
52
        return $fileName;
53
    }
54
55
    public function loadResources(string $appName, string $context, string $appDir): void
56
    {
57
        $meta = new Meta($appName, $context, $appDir);
58
59
        $resMetas = $meta->getGenerator('*');
60
        foreach ($resMetas as $resMeta) {
61
            ($this->newInstance)($resMeta->class);
62
        }
63
    }
64
}
65