-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
181 lines (148 loc) · 4.06 KB
/
helpers.php
File metadata and controls
181 lines (148 loc) · 4.06 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
use RootStudio\Base;
use RootStudio\BaseLayout;
/**
* Return path to document root
*
* @param string $path
*
* @return string
*/
if (!function_exists('base_public_path')) {
function base_public_path(string $path = ''): string
{
if (!defined('BASE_PUBLIC_PATH')) {
define('BASE_PUBLIC_PATH', '/../../../../');
}
$Base = new Base(realpath(__DIR__ . BASE_PUBLIC_PATH));
$userPath = $Base->getPublicPath() . ($path ? '/' . trim($path, '/') : $path);
return $userPath;
}
}
/**
* Parse manifest and output versioned asset path
*
* @param string $path
* @param string $manifestDirectory
*
* @return string
* @throws Exception
*/
if (!function_exists('base_asset')) {
function base_asset(string $path, string $manifestDirectory = ''): string
{
static $manifest;
$path = '/' . ltrim($path, '/');
if ($manifestDirectory) {
$manifestDirectory = '/' . ltrim($manifestDirectory, '/');
}
if (!$manifest) {
if (!file_exists($manifestPath = base_public_path($manifestDirectory . '/mix-manifest.json'))) {
throw new Exception('The Mix manifest does not exist.');
}
$manifest = json_decode(file_get_contents($manifestPath), true);
}
if (!array_key_exists($path, $manifest)) {
throw new Exception(
"Unable to locate Mix file: {$path}. Please check your " .
'webpack.mix.js output paths and try again.'
);
}
if (file_exists(base_public_path($manifestDirectory . '/hot'))) {
return "http://localhost:8080{$manifest[$path]}";
}
return $manifestDirectory . $manifest[$path];
}
}
/**
* Include a layout file
*
* @param string $file
* @param array $data
* @param bool $return
*
* @return string|void
*
* @throws Exception
*/
if (!function_exists('base_layout')) {
function base_layout(string $file, array $data = [], bool $return = false)
{
$Base = new Base(realpath(__DIR__ . '/../../../../'));
$BaseLayout = BaseLayout::fetch();
$BaseLayout->setLayoutVars($data);
$path = $Base->getLayoutPath() . DIRECTORY_SEPARATOR . ltrim($file, '/') . '.php';
if (!file_exists($path)) {
throw new Exception("Unable to locate layout file: {$path}. Please check the layout exists");
}
if ($return) {
flush();
ob_start();
}
$BaseLayout->incrementLayoutDepth();
include $path;
$BaseLayout->decrementLayoutDepth();
if ($return) {
return ob_get_clean();
}
flush();
}
}
/**
* Echo or return a layout variable
*
* @param $key
* @param bool $return
*
* @return string
*/
if (!function_exists('base_layout_var')) {
function base_layout_var(string $key, bool $return = false)
{
$BaseLayout = BaseLayout::fetch();
$value = $BaseLayout->getLayoutVar($key);
if ($return) return $value;
echo htmlentities($value);
}
}
/**
* Check whether layout has access to variable
*
* @param string $key
*
* @return bool
*/
if (!function_exists('base_layout_has')) {
function base_layout_has(string $key): bool
{
$BaseLayout = BaseLayout::fetch();
$value = $BaseLayout->getLayoutVar($key);
if ($value) return true;
return false;
}
}
/**
* Returns a new faker instance for mocking content
*
* @return Faker\Generator
*/
if (!function_exists('base_faker_factory')) {
function base_faker_factory()
{
if (!class_exists('Faker\Factory')) {
throw new Exception('Faker library is not available.');
}
return Faker\Factory::create();
}
}
/**
* Returns the full HTTP host of the current request
*
* @return string
*/
if (!function_exists('base_http_host')) {
function base_http_host(): string
{
return 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 's' : '') . '://' . $_SERVER['HTTP_HOST'];
}
}