Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
RegistersFileLocations | |
100.00% |
15 / 15 |
|
100.00% |
8 / 8 |
10 | |
100.00% |
1 / 1 |
registerSourceDirectories | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
registerOutputDirectories | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
discoverBladeViewsIn | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
storeCompiledSiteIn | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
useMediaDirectory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSourceDirectoryConfiguration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOutputDirectoryConfiguration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPageConfiguration | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Framework\Concerns; |
6 | |
7 | use Hyde\Hyde; |
8 | use Hyde\Facades\Config; |
9 | use Hyde\Pages\Concerns\HydePage; |
10 | use Illuminate\Support\Str; |
11 | |
12 | use function class_basename; |
13 | use function array_unique; |
14 | use function array_merge; |
15 | use function base_path; |
16 | use function Hyde\unslash; |
17 | |
18 | /** |
19 | * This trait registers the file paths for important Hyde locations. |
20 | * |
21 | * If you want to customize these directories, the recommended way is to |
22 | * create a service provider that uses this trait, and change your |
23 | * paths in the register method, like in the HydeServiceProvider. |
24 | * |
25 | * Remember that your overriding provider should be loaded after the HSP. |
26 | */ |
27 | trait RegistersFileLocations |
28 | { |
29 | /** |
30 | * Register the default source directories for the given page classes. |
31 | * Location string should be relative to the source root, which is |
32 | * usually the root of the project. |
33 | * |
34 | * @example registerSourceDirectories([HydePage::class => '_pages']) |
35 | * |
36 | * @param array<class-string<HydePage>, string> $directoryMapping |
37 | */ |
38 | protected function registerSourceDirectories(array $directoryMapping): void |
39 | { |
40 | /** @var class-string<HydePage> $class */ |
41 | foreach ($directoryMapping as $class => $location) { |
42 | $class::setSourceDirectory(unslash(Hyde::getSourceRoot().'/'.unslash($location))); |
43 | } |
44 | } |
45 | |
46 | /** |
47 | * Register the optional output directories. |
48 | * Some HTML pages, like Blade and Markdown pages are stored right in the _site/ directory. |
49 | * However, some pages, like docs and posts are in subdirectories of the _site/ directory. |
50 | * Location string should be relative to the root of the application. |
51 | * |
52 | * @example registerOutputDirectories([HydePage::class => 'docs']) |
53 | * |
54 | * @param array<class-string<HydePage>, string> $directoryMapping |
55 | */ |
56 | protected function registerOutputDirectories(array $directoryMapping): void |
57 | { |
58 | /** @var class-string<HydePage> $class */ |
59 | foreach ($directoryMapping as $class => $location) { |
60 | $class::setOutputDirectory(unslash($location)); |
61 | } |
62 | } |
63 | |
64 | /** |
65 | * If you are loading Blade views from a different directory, |
66 | * you need to add the path to the view.php config. This is |
67 | * here done automatically when registering the provider. |
68 | */ |
69 | protected function discoverBladeViewsIn(string $directory): void |
70 | { |
71 | Config::set(['view.paths' => array_unique(array_merge( |
72 | Config::getArray('view.paths', []), |
73 | [base_path($directory)] |
74 | ))]); |
75 | } |
76 | |
77 | /** |
78 | * @param string $directory The relative path to the directory when the compiled site is stored. |
79 | * |
80 | * Warning! This directory is emptied when compiling the site. |
81 | */ |
82 | protected function storeCompiledSiteIn(string $directory): void |
83 | { |
84 | Hyde::setOutputDirectory($directory); |
85 | } |
86 | |
87 | /** |
88 | * @param string $directory The relative path to the directory used for storing media files. |
89 | */ |
90 | protected function useMediaDirectory(string $directory): void |
91 | { |
92 | Hyde::setMediaDirectory($directory); |
93 | } |
94 | |
95 | protected function getSourceDirectoryConfiguration(string $class, string $default): string |
96 | { |
97 | return $this->getPageConfiguration('source_directories', $class, $default); |
98 | } |
99 | |
100 | protected function getOutputDirectoryConfiguration(string $class, string $default): string |
101 | { |
102 | return $this->getPageConfiguration('output_directories', $class, $default); |
103 | } |
104 | |
105 | private function getPageConfiguration(string $option, string $class, string $default): string |
106 | { |
107 | return Config::getNullableString("hyde.$option.".Str::kebab(class_basename($class))) /** @experimental Support for using kebab-case class names */ |
108 | ?? Config::getNullableString("hyde.$option.$class") |
109 | ?? $default; |
110 | } |
111 | } |