forked from andrewdavis-dev/phpapprentice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
111 lines (95 loc) · 2.32 KB
/
Copy pathfunctions.php
File metadata and controls
111 lines (95 loc) · 2.32 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
<?php
/**
* Runs a scoped require on a partial
*
* The filename must start with an underscore
* and end with .php or .phtml
*
* @param string $path Name of partial
* @param array $vars Variables to use for the partial
* @return void
*/
function partial(string $path, array $vars = []) {
$dir = config('templates_dir');
if (file_exists($dir. '/' . "_$path.php")) {
$file = $dir. '/' . "_$path.php";
} elseif (file_exists($dir . '/' . "_$path.phtml")) {
$file = $dir. '/' . "_$path.phtml";
} else {
throw new Exception('Partial could not be found: ' . $dir . '/' . "_$path.php");
}
extract($vars);
require $file;
}
/**
* Gets url path to page
*
* @param string $page Name of page
* @return string
*/
function page_path(string $page): string {
return '/' . $page . '.html';
}
/**
* Safely escapes text for display in html
*
* @param string $text
* @return string
*/
function escape(string $text): string {
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8', true);
}
/**
* Loads content of svg icon from assets folder
*
* @param string $name Name of icon without extension
* @return string Contents of file
*/
function icon(string $name): string {
$dir = config('icon_dir');
$path = $dir . '/' . $name . '.svg';
if (file_exists($path)) {
return file_get_contents($path);
}
return '';
}
/**
* Loads config file into a global
*
* @param string $path
* @return void
*/
function load_config(string $path) {
$config = require $path;
if (!is_array($config)) {
throw new \Exception('Config file does not return an array.');
}
$GLOBALS['APPRENTICE_CONFIG'] = $config;
}
/**
* Returns config value
*
* @param string $key
* @return mixed
*/
function config(string $key) {
$config = $GLOBALS['APPRENTICE_CONFIG'] ?? [];
return $config[$key] ?? null;
}
/**
* Returns path to asset based on manifes.json file
*
* @param string $name
* @return string
*/
function asset(string $name): string {
$outputDir = config('output_dir');
if (file_exists($outputDir . '/manifest.json')) {
$text = file_get_contents($outputDir . '/manifest.json');
$paths = json_decode($text, true);
if (isset($paths[$name])) {
return $paths[$name];
}
}
return '/' . $name;
}