Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
CleanSiteDirectory
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
9
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 printFinishMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isItSafeToCleanOutputDirectory
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 isOutputDirectoryWhitelisted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 askIfUnsafeDirectoryShouldBeEmptied
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 safeOutputDirectories
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Actions\PreBuildTasks;
6
7use Hyde\Hyde;
8use Hyde\Facades\Config;
9use Hyde\Facades\Filesystem;
10use Hyde\Framework\Features\BuildTasks\PreBuildTask;
11
12use function basename;
13use function glob;
14use function in_array;
15use function sprintf;
16
17class CleanSiteDirectory extends PreBuildTask
18{
19    protected static string $message = 'Removing all files from build directory';
20
21    public function handle(): void
22    {
23        if ($this->isItSafeToCleanOutputDirectory()) {
24            Filesystem::unlink(glob(Hyde::sitePath('*.{html,json}'), GLOB_BRACE));
25            Filesystem::cleanDirectory(Hyde::siteMediaPath());
26        }
27    }
28
29    public function printFinishMessage(): void
30    {
31        $this->newLine();
32    }
33
34    protected function isItSafeToCleanOutputDirectory(): bool
35    {
36        if (! $this->isOutputDirectoryWhitelisted() && ! $this->askIfUnsafeDirectoryShouldBeEmptied()) {
37            $this->info('Output directory will not be emptied.');
38
39            return false;
40        }
41
42        return true;
43    }
44
45    protected function isOutputDirectoryWhitelisted(): bool
46    {
47        return in_array(basename(Hyde::sitePath()), $this->safeOutputDirectories());
48    }
49
50    protected function askIfUnsafeDirectoryShouldBeEmptied(): bool
51    {
52        return $this->confirm(sprintf(
53            'The configured output directory (%s) is potentially unsafe to empty. '.
54            'Are you sure you want to continue?',
55            Hyde::getOutputDirectory()
56        ));
57    }
58
59    /** @return array<string> */
60    protected function safeOutputDirectories(): array
61    {
62        /** @var array<string> $directories */
63        $directories = Config::getArray('hyde.safe_output_directories', ['_site', 'docs', 'build']);
64
65        return $directories;
66    }
67}