Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
HydeKernel
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 version
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 features
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasFeature
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 toArray
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Foundation;
6
7use Hyde\Enums\Feature;
8use Hyde\Facades\Features;
9use Hyde\Support\BuildWarnings;
10use Hyde\Foundation\Kernel\Filesystem;
11use Hyde\Foundation\Kernel\Hyperlinks;
12use Hyde\Foundation\Kernel\FileCollection;
13use Hyde\Foundation\Kernel\PageCollection;
14use Hyde\Foundation\Kernel\RouteCollection;
15use Hyde\Support\Contracts\SerializableContract;
16use Hyde\Support\Concerns\Serializable;
17use Illuminate\Support\Traits\Macroable;
18use Illuminate\Support\Str;
19
20use function getcwd;
21use function sprintf;
22use function is_string;
23use function var_export;
24use function debug_backtrace;
25use function trigger_deprecation;
26
27/**
28 * Encapsulates a HydePHP project, providing helpful methods for interacting with it.
29 *
30 * @see \Hyde\Hyde for the facade commonly used to access this class.
31 *
32 * @author  Caen De Silva <caen@desilva.se>
33 * @copyright 2022 Caen De Silva
34 * @license MIT License
35 *
36 * @link https://hydephp.com/
37 *
38 * @extra Usage information:
39 *
40 * The HydeKernel It is stored as a singleton in this class, and is bound into the
41 * Laravel Application Service Container, and can be accessed in a few ways.
42 *
43 * Commonly, you'll use the Hyde facade to access it, but you can also use Dependency Injection
44 * by type-hinting the HydeKernel::class, or use the hyde() function to get the Kernel.
45 * The Kernel instance is constructed and bound in the app/bootstrap.php script.
46 */
47class HydeKernel implements SerializableContract
48{
49    use Concerns\HandlesFoundationCollections;
50    use Concerns\ImplementsStringHelpers;
51    use Concerns\ForwardsHyperlinks;
52    use Concerns\ForwardsFilesystem;
53    use Concerns\ManagesHydeKernel;
54    use Concerns\ManagesExtensions;
55    use Concerns\ManagesViewData;
56    use Concerns\BootsHydeKernel;
57
58    use Serializable;
59    use Macroable;
60
61    final public const VERSION = '1.7.3';
62
63    protected static self $instance;
64
65    protected string $basePath;
66    protected string $sourceRoot = '';
67    protected string $outputDirectory = '_site';
68    protected string $mediaDirectory = '_media';
69
70    protected Filesystem $filesystem;
71    protected Hyperlinks $hyperlinks;
72
73    protected FileCollection $files;
74    protected PageCollection $pages;
75    protected RouteCollection $routes;
76
77    protected bool $booted = false;
78
79    /** @var array<class-string<\Hyde\Foundation\Concerns\HydeExtension>, \Hyde\Foundation\Concerns\HydeExtension> */
80    protected array $extensions = [];
81
82    public function __construct(?string $basePath = null)
83    {
84        $this->setBasePath($basePath ?? getcwd());
85        $this->filesystem = new Filesystem($this);
86        $this->hyperlinks = new Hyperlinks($this);
87
88        $this->registerExtension(HydeCoreExtension::class);
89    }
90
91    public static function version(): string
92    {
93        return self::VERSION;
94    }
95
96    public function features(): Features
97    {
98        return new Features;
99    }
100
101    public function hasFeature(Feature|string $feature): bool
102    {
103        if (is_string($feature)) {
104            /** @see https://github.com/hydephp/develop/pull/1650 */
105
106            // @codeCoverageIgnoreStart
107
108            $message = 'Passing a string to HydeKernel::hasFeature() is deprecated. Use a Feature enum case instead.';
109            trigger_deprecation('hydephp/hyde', '1.5.0', $message);
110
111            BuildWarnings::report(sprintf("$message\n    <fg=gray>Replace </><fg=default>`%s`</><fg=gray> with </><fg=default>`%s`</><fg=gray> \n    in file %s:%s</>",
112                sprintf('HydeKernel::hasFeature(%s)', var_export($feature, true)),
113                sprintf('HydeKernel::hasFeature(Feature::%s)', Str::studly($feature)),
114                debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['file'],
115                debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['line']
116            ));
117
118            $feature = match ($feature) {
119                'html-pages' => Feature::HtmlPages,
120                'markdown-posts' => Feature::MarkdownPosts,
121                'blade-pages' => Feature::BladePages,
122                'markdown-pages' => Feature::MarkdownPages,
123                'documentation-pages' => Feature::DocumentationPages,
124                'darkmode' => Feature::Darkmode,
125                'documentation-search' => Feature::DocumentationSearch,
126                'torchlight' => Feature::Torchlight,
127            };
128
129            // @codeCoverageIgnoreEnd
130        }
131
132        return Features::enabled($feature);
133    }
134
135    /** @inheritDoc */
136    public function toArray(): array
137    {
138        return [
139            'version' => self::VERSION,
140            'basePath' => $this->basePath,
141            'sourceRoot' => $this->sourceRoot,
142            'outputDirectory' => $this->outputDirectory,
143            'mediaDirectory' => $this->mediaDirectory,
144            'extensions' => $this->getRegisteredExtensions(),
145            'features' => $this->features(),
146            'files' => $this->files(),
147            'pages' => $this->pages(),
148            'routes' => $this->routes(),
149        ];
150    }
151}