Passed
Push — master ( a84245...032205 )
by Caen
11:56 queued 13s
created

GenerateBuildManifest::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 4
b 0
f 0
nc 2
nop 0
dl 0
loc 15
rs 10
1
<?php
2
3
namespace Hyde\Framework\Actions\PostBuildTasks;
4
5
use Hyde\Framework\Concerns\AbstractBuildTask;
6
use Hyde\Framework\Concerns\HydePage;
7
use Hyde\Framework\Hyde;
8
use Illuminate\Console\OutputStyle;
9
use Illuminate\Support\Collection;
10
11
/**
12
 * @see \Hyde\Framework\Testing\Unit\GenerateBuildManifestTest
13
 */
14
class GenerateBuildManifest extends AbstractBuildTask
15
{
16
    public static string $description = 'Generating build manifest';
17
18
    public function __construct(?OutputStyle $output = null)
19
    {
20
        parent::__construct($output);
21
        $this->output = null;
22
    }
23
24
    public function run(): void
25
    {
26
        $pages = new Collection();
27
28
        /** @var \Hyde\Framework\Concerns\HydePage $page */
29
        foreach (Hyde::pages() as $page) {
30
            $pages->push([
31
                'source_path' => $page->getSourcePath(),
32
                'output_path' => $page->getOutputPath(),
33
                'source_hash' => $this->hashSourcePath($page),
34
                'output_hash' => $this->hashOutputPath($page),
35
            ]);
36
        }
37
38
        file_put_contents($this->getManifestPath(), $this->jsonEncodeOutput($pages));
39
    }
40
41
    protected function hashOutputPath(HydePage $page): ?string
42
    {
43
        $path = Hyde::sitePath($page->getOutputPath());
44
45
        return file_exists($path) ? md5_file($path) : null;
46
    }
47
48
    protected function hashSourcePath(HydePage $page): string
49
    {
50
        return md5_file(Hyde::path($page->getSourcePath()));
51
    }
52
53
    protected function getManifestPath(): string
54
    {
55
        return Hyde::path(config(
56
            'hyde.build_manifest_path',
57
            'storage/framework/cache/build-manifest.json'
58
        ));
59
    }
60
61
    protected function jsonEncodeOutput(Collection $pages): string
62
    {
63
        return json_encode([
64
            'generated' => now(),
65
            'pages' => $pages,
66
        ], JSON_PRETTY_PRINT);
67
    }
68
}
69