Passed
Push — master ( 99cab5...2782fc )
by Caen
04:06 queued 13s
created

GenerateBuildManifest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

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