Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
BuildService
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 compileStaticPages
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 transferMediaAssets
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 compilePagesForClass
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getClassPluralName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPageTypes
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Services;
6
7use Hyde\Hyde;
8use Hyde\Foundation\Facades\Routes;
9use Hyde\Foundation\Kernel\RouteCollection;
10use Hyde\Framework\Actions\StaticPageBuilder;
11use Hyde\Framework\Concerns\InteractsWithDirectories;
12use Hyde\Pages\Concerns\HydePage;
13use Hyde\Support\Filesystem\MediaFile;
14use Hyde\Support\Models\Route;
15use Illuminate\Console\Concerns\InteractsWithIO;
16use Illuminate\Console\OutputStyle;
17
18use function class_basename;
19use function preg_replace;
20use function collect;
21use function copy;
22
23/**
24 * Moves logic from the build command to a service.
25 *
26 * Handles the build loop which generates the static site.
27 *
28 * @see \Hyde\Console\Commands\BuildSiteCommand
29 */
30class BuildService
31{
32    use InteractsWithIO;
33    use InteractsWithDirectories;
34
35    protected RouteCollection $router;
36
37    public function __construct(OutputStyle $output)
38    {
39        $this->output = $output;
40
41        $this->router = Hyde::routes();
42    }
43
44    public function compileStaticPages(): void
45    {
46        collect($this->getPageTypes())->each(function (string $pageClass): void {
47            $this->compilePagesForClass($pageClass);
48        });
49    }
50
51    /** @deprecated This method will be replaced by a build task in v2.0 */
52    public function transferMediaAssets(): void
53    {
54        $this->needsDirectory(Hyde::siteMediaPath());
55
56        $this->comment('Transferring Media Assets...');
57        $this->withProgressBar(MediaFile::files(), function (string $identifier): void {
58            $sitePath = Hyde::siteMediaPath($identifier);
59            $this->needsParentDirectory($sitePath);
60            copy(Hyde::mediaPath($identifier), $sitePath);
61        });
62
63        $this->newLine(2);
64    }
65
66    /**
67     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
68     */
69    protected function compilePagesForClass(string $pageClass): void
70    {
71        $this->comment("Creating {$this->getClassPluralName($pageClass)}...");
72
73        $collection = Routes::getRoutes($pageClass);
74
75        $this->withProgressBar($collection, function (Route $route): void {
76            StaticPageBuilder::handle($route->getPage());
77        });
78
79        $this->newLine(2);
80    }
81
82    protected function getClassPluralName(string $pageClass): string
83    {
84        return preg_replace('/([a-z])([A-Z])/', '$1 $2', class_basename($pageClass)).'s';
85    }
86
87    /** @return array<class-string<\Hyde\Pages\Concerns\HydePage>> */
88    protected function getPageTypes(): array
89    {
90        return Hyde::pages()->map(function (HydePage $page): string {
91            return $page::class;
92        })->unique()->values()->toArray();
93    }
94}