-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBuildService.php
96 lines (84 loc) · 2.59 KB
/
BuildService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Hyde\Framework\Services;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\DocumentationPage;
use Hyde\Framework\Models\MarkdownPage;
use Hyde\Framework\Models\MarkdownPost;
/**
* Static service helpers for building static pages.
* @deprecated may be renamed to DiscoveryService
*
* @package HydeAutoDiscovery
*/
class BuildService
{
public static function getParserClassForModel(string $model): string
{
return $model::$parserClass;
}
/**
* Create and get a constructed instance of a Model's Parser class.
*
* @param string $model Class constant of the Model to get the Parser for.
* @param string $slug The slug of the source file to parse.
*
* @example getParserForModel(MarkdownPost::class, 'hello-world')
*
* @return object The constructed Parser instance.
*/
public static function getParserInstanceForModel(string $model, string $slug): object
{
return new $model::$parserClass($slug);
}
/**
* Get the file extension for a models source files.
*/
public static function getFileExtensionForModelFiles(string $model): string
{
return $model::$fileExtension;
}
/**
* Get the source directory path of a model.
*/
public static function getFilePathForModelClassFiles(string $model): string
{
return $model::$sourceDirectory;
}
/**
* Determine the Page Model to use for a given file path.
*
* @todo refactor to use the source directories instead of hard-coded paths
*
* @return string The model class constant, or false if none was found.
*/
public static function findModelFromFilePath(string $filepath): string|false
{
if (str_starts_with($filepath, '_posts')) {
return MarkdownPost::class;
}
if (str_starts_with($filepath, '_docs')) {
return DocumentationPage::class;
}
if (str_starts_with($filepath, '_pages') && str_ends_with($filepath, '.md')) {
return MarkdownPage::class;
}
if (str_starts_with($filepath, '_pages') && str_ends_with($filepath, '.blade.php')) {
return BladePage::class;
}
return false;
}
/**
* Create a filepath that can be opened in the browser from a terminal.
*
* @param string $filepath
* @return string
*/
public static function createClickableFilepath(string $filepath): string
{
return 'file://'.str_replace(
'\\',
'/',
realpath($filepath)
);
}
}