Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.67% |
33 / 36 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
FileHelpers | |
91.67% |
33 / 36 |
|
88.89% |
8 / 9 |
22.28 | |
0.00% |
0 / 1 |
docsDirectory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
docsIndexPath | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
path | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
vendorPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
pageLink | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
relativeLink | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
image | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
uriPath | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
copy | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Concerns\Internal; |
4 | |
5 | /** |
6 | * Offloads file helper methods for the Hyde Facade. |
7 | * |
8 | * If a method uses the name `path` it refers to an internal file path. |
9 | * if a method uses the name `link` it refers to a web link used in Blade templates. |
10 | * |
11 | * @see \Hyde\Framework\Hyde |
12 | */ |
13 | trait FileHelpers |
14 | { |
15 | /** |
16 | * Get the subdirectory compiled documentation files are stored in. |
17 | * |
18 | * @deprecated will be renamed to be more distinct from other path helpers. |
19 | * Naming suggestion is `getDocumentationOutputPath()`. |
20 | * The configuration option has been renamed. |
21 | * |
22 | * @return string |
23 | */ |
24 | public static function docsDirectory(): string |
25 | { |
26 | return trim(config('docs.output_directory', 'docs'), '/\\'); |
27 | } |
28 | |
29 | /** |
30 | * Get the path to the frontpage for the documentation. |
31 | * |
32 | * @return string|false returns false if no frontpage is found |
33 | */ |
34 | public static function docsIndexPath(): string|false |
35 | { |
36 | if (file_exists(static::path('_docs/index.md'))) { |
37 | return trim(static::pageLink(static::docsDirectory().'/index.html'), '/'); |
38 | } |
39 | |
40 | if (file_exists(static::path('_docs/readme.md'))) { |
41 | return trim(static::pageLink(static::docsDirectory().'/readme.html'), '/'); |
42 | } |
43 | |
44 | return false; |
45 | } |
46 | |
47 | /** |
48 | * Get an absolute file path from a supplied relative path. |
49 | * |
50 | * The function returns the fully qualified path to your site's root directory. |
51 | * |
52 | * You may also use the function to generate a fully qualified path to a given file |
53 | * relative to the project root directory when supplying the path argument. |
54 | * |
55 | * @param string $path |
56 | * @return string |
57 | */ |
58 | public static function path(string $path = ''): string |
59 | { |
60 | if (empty($path)) { |
61 | return static::getBasePath(); |
62 | } |
63 | |
64 | $path = trim($path, '/\\'); |
65 | |
66 | return static::getBasePath().DIRECTORY_SEPARATOR.$path; |
67 | } |
68 | |
69 | /** |
70 | * Works similarly to the path() function, but returns a file in the Framework package. |
71 | * |
72 | * @param string $path |
73 | * @return string |
74 | */ |
75 | public static function vendorPath(string $path = ''): string |
76 | { |
77 | return static::path('vendor/hyde/framework/'.trim($path, '/\\')); |
78 | } |
79 | |
80 | /** |
81 | * Format a link to an HTML file, allowing for pretty URLs, if enabled. |
82 | * |
83 | * @see \Tests\Unit\FileHelperPageLinkPrettyUrlTest |
84 | */ |
85 | public static function pageLink(string $destination): string |
86 | { |
87 | if (config('hyde.pretty_urls', false) === true) { |
88 | if (str_ends_with($destination, '.html')) { |
89 | if ($destination === 'index.html') { |
90 | return '/'; |
91 | } |
92 | if ($destination === static::docsDirectory().'/index.html') { |
93 | return static::docsDirectory().'/'; |
94 | } |
95 | |
96 | return substr($destination, 0, -5); |
97 | } |
98 | } |
99 | |
100 | return $destination; |
101 | } |
102 | |
103 | /** |
104 | * Inject the proper number of `../` before the links in Blade templates. |
105 | * |
106 | * @see \Tests\Unit\FileHelperRelativeLinkTest |
107 | * |
108 | * @param string $destination relative to output directory on compiled site |
109 | * @param string $current the current URI path relative to the site root |
110 | * @return string |
111 | */ |
112 | public static function relativeLink(string $destination, string $current = ''): string |
113 | { |
114 | $nestCount = substr_count($current, '/'); |
115 | $route = ''; |
116 | if ($nestCount > 0) { |
117 | $route .= str_repeat('../', $nestCount); |
118 | } |
119 | $route .= static::pageLink($destination); |
120 | |
121 | return str_replace('//', '/', $route); |
122 | } |
123 | |
124 | /** |
125 | * Gets a relative link to the given image stored in the _site/media folder. |
126 | */ |
127 | public static function image(string $name, string $current = ''): string |
128 | { |
129 | if (str_starts_with($name, 'http')) { |
130 | return $name; |
131 | } |
132 | |
133 | return static::relativeLink('media/'.basename($name), $current); |
134 | } |
135 | |
136 | /** |
137 | * Return a qualified URI path, if SITE_URL is set in .env, else return false. |
138 | * |
139 | * @param string|null $path optional relative path suffix. Omit to return base url. |
140 | * @return string|false |
141 | */ |
142 | public static function uriPath(?string $path = ''): string|false |
143 | { |
144 | if (config('hyde.site_url', false)) { |
145 | return rtrim(config('hyde.site_url'), '/').'/'.(trim($path, '/') ?? ''); |
146 | } |
147 | |
148 | return false; |
149 | } |
150 | |
151 | /** |
152 | * Wrapper for the copy function, but allows choosing if files may be overwritten. |
153 | * |
154 | * @param string $from The source file path. |
155 | * @param string $to The destination file path. |
156 | * @param bool $force If true, existing files will be overwritten. |
157 | * @return bool|int Returns true|false on copy() success|failure, or an error code on failure |
158 | */ |
159 | public static function copy(string $from, string $to, bool $force = false): bool|int |
160 | { |
161 | if (! file_exists($from)) { |
162 | return 404; |
163 | } |
164 | |
165 | if (file_exists($to) && ! $force) { |
166 | return 409; |
167 | } |
168 | |
169 | return copy($from, $to); |
170 | } |
171 | } |