Passed
Push — master ( eb8cff...c3a046 )
by Caen
03:04 queued 14s
created

HydeKernel::getModelSourcePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework;
4
5
use Composer\InstalledVersions;
6
use Hyde\Framework\Concerns\JsonSerializesArrayable;
7
use Hyde\Framework\Contracts\HydeKernelContract;
8
use Hyde\Framework\Contracts\RouteContract;
9
use Hyde\Framework\Foundation\FileCollection;
10
use Hyde\Framework\Foundation\Filesystem;
11
use Hyde\Framework\Foundation\Hyperlinks;
12
use Hyde\Framework\Foundation\PageCollection;
13
use Hyde\Framework\Foundation\RouteCollection;
14
use Hyde\Framework\Helpers\Features;
15
use Illuminate\Contracts\Support\Arrayable;
16
use Illuminate\Support\Facades\View;
17
use Illuminate\Support\Str;
18
use Illuminate\Support\Traits\Macroable;
19
20
/**
21
 * Encapsulates a HydePHP project, providing helpful methods for interacting with it.
22
 *
23
 * @see \Hyde\Framework\Hyde for the facade commonly used to access this class.
24
 *
25
 * @author  Caen De Silva <[email protected]>
26
 * @copyright 2022 Caen De Silva
27
 * @license MIT License
28
 *
29
 * @link https://hydephp.com/
30
 */
31
class HydeKernel implements HydeKernelContract, Arrayable, \JsonSerializable
32
{
33
    use Macroable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Hyde\Framework\HydeKernel.
Loading history...
34
    use JsonSerializesArrayable;
35
36
    protected static HydeKernelContract $instance;
37
38
    protected string $basePath;
39
40
    protected Filesystem $filesystem;
41
    protected Hyperlinks $hyperlinks;
42
43
    protected FileCollection $files;
44
    protected PageCollection $pages;
45
    protected RouteCollection $routes;
46
47
    protected bool $booted = false;
48
49
    public function __construct(?string $basePath = null)
50
    {
51
        $this->setBasePath($basePath ?? getcwd());
52
        $this->filesystem = new Filesystem($this);
53
        $this->hyperlinks = new Hyperlinks($this);
54
    }
55
56
    public function boot(): void
57
    {
58
        $this->booted = true;
59
60
        $this->files = FileCollection::boot($this);
61
        $this->pages = PageCollection::boot($this);
62
        $this->routes = RouteCollection::boot($this);
63
    }
64
65
    public static function setInstance(HydeKernelContract $instance): void
66
    {
67
        static::$instance = $instance;
68
    }
69
70
    public static function getInstance(): HydeKernelContract
71
    {
72
        return static::$instance;
73
    }
74
75
    public static function version(): string
76
    {
77
        return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased';
78
    }
79
80
    public function getBasePath(): string
81
    {
82
        return $this->basePath;
83
    }
84
85
    public function setBasePath(string $basePath): void
86
    {
87
        $this->basePath = rtrim($basePath, '/\\');
88
    }
89
90
    public function features(): Features
91
    {
92
        return new Features;
93
    }
94
95
    public function hasFeature(string $feature): bool
96
    {
97
        return Features::enabled($feature);
98
    }
99
100
    public function currentPage(): string
101
    {
102
        return View::shared('currentPage', '');
103
    }
104
105
    public function currentRoute(): ?RouteContract
106
    {
107
        return View::shared('currentRoute');
108
    }
109
110
    public function files(): FileCollection
111
    {
112
        if (! $this->booted) {
113
            $this->boot();
114
        }
115
116
        return $this->files;
117
    }
118
119
    public function pages(): PageCollection
120
    {
121
        if (! $this->booted) {
122
            $this->boot();
123
        }
124
125
        return $this->pages;
126
    }
127
128
    public function routes(): RouteCollection
129
    {
130
        if (! $this->booted) {
131
            $this->boot();
132
        }
133
134
        return $this->routes;
135
    }
136
137
    public function makeTitle(string $slug): string
138
    {
139
        $alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but'];
140
141
        return ucfirst(str_ireplace(
0 ignored issues
show
Bug introduced by
It seems like str_ireplace($alwaysLowe...t\Str::headline($slug)) can also be of type array; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        return ucfirst(/** @scrutinizer ignore-type */ str_ireplace(
Loading history...
142
            $alwaysLowercase,
143
            $alwaysLowercase,
144
            Str::headline($slug)
145
        ));
146
    }
147
148
    public function formatHtmlPath(string $destination): string
149
    {
150
        return $this->hyperlinks->formatHtmlPath($destination);
151
    }
152
153
    public function relativeLink(string $destination): string
154
    {
155
        return $this->hyperlinks->relativeLink($destination);
156
    }
157
158
    public function image(string $name, bool $preferQualifiedUrl = false): string
159
    {
160
        return $this->hyperlinks->image($name, $preferQualifiedUrl);
161
    }
162
163
    public function hasSiteUrl(): bool
164
    {
165
        return $this->hyperlinks->hasSiteUrl();
166
    }
167
168
    public function url(string $path = '', ?string $default = null): string
169
    {
170
        return $this->hyperlinks->url($path, $default);
171
    }
172
173
    public function path(string $path = ''): string
174
    {
175
        return $this->filesystem->path($path);
176
    }
177
178
    public function vendorPath(string $path = ''): string
179
    {
180
        return $this->filesystem->vendorPath($path);
181
    }
182
183
    public function copy(string $from, string $to): bool
184
    {
185
        return $this->filesystem->copy($from, $to);
186
    }
187
188
    public function touch(string|array $path): bool
189
    {
190
        return $this->filesystem->touch($path);
191
    }
192
193
    public function unlink(string|array $path): bool
194
    {
195
        return $this->filesystem->unlink($path);
196
    }
197
198
    public function getModelSourcePath(string $model, string $path = ''): string
199
    {
200
        return $this->filesystem->getModelSourcePath($model, $path);
201
    }
202
203
    public function getBladePagePath(string $path = ''): string
204
    {
205
        return $this->filesystem->getBladePagePath($path);
206
    }
207
208
    public function getMarkdownPagePath(string $path = ''): string
209
    {
210
        return $this->filesystem->getMarkdownPagePath($path);
211
    }
212
213
    public function getMarkdownPostPath(string $path = ''): string
214
    {
215
        return $this->filesystem->getMarkdownPostPath($path);
216
    }
217
218
    public function getDocumentationPagePath(string $path = ''): string
219
    {
220
        return $this->filesystem->getDocumentationPagePath($path);
221
    }
222
223
    public function getSiteOutputPath(string $path = ''): string
224
    {
225
        return $this->filesystem->getSiteOutputPath($path);
226
    }
227
228
    public function pathToRelative(string $path): string
229
    {
230
        return $this->filesystem->pathToRelative($path);
231
    }
232
233
    /**
234
     * @inheritDoc
235
     *
236
     * @return array{basePath: string, features: \Hyde\Framework\Helpers\Features, pages: \Hyde\Framework\Foundation\PageCollection, routes: \Hyde\Framework\Foundation\RouteCollection}
237
     */
238
    public function toArray(): array
239
    {
240
        return [
241
            'basePath' => $this->basePath,
242
            'features' => $this->features(),
243
            'files' => $this->files(),
244
            'pages' => $this->pages(),
245
            'routes' => $this->routes(),
246
        ];
247
    }
248
}
249