This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathLoader.php
174 lines (145 loc) · 4.5 KB
/
Loader.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
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
<?php
namespace Sober\Controller;
use Sober\Controller\Utils;
use Brain\Hierarchy\Hierarchy;
class Loader
{
// Dep
private $hierarchy;
// User
private $namespace;
private $path;
// Internal
private $listOfFiles;
private $classesToRun = [];
/**
* Construct
*
* Initialise the Loader methods
*/
public function __construct(Hierarchy $hierarchy)
{
// Pass in WordPress hierarchy and set to param for reference
$this->hierarchy = $hierarchy;
// Set the default or custom namespace used for Controller files
$this->setNamespace();
// Set the path using $this->namespace assuming PSR4 autoloading
$this->setPath();
// Return if there are no Controller files
if (!file_exists($this->path)) {
return;
}
// Set the list of files from the Controller files namespace/path
$this->setListOfFiles();
// Set the classes to run from the list of files
$this->setClassesToRun();
// Set the aliases for static functions from the list of classes to run
$this->setClassesAlias();
// Add the -data body classes for the Blade filter
$this->addBodyDataClasses();
}
/**
* Set Namespace
*
* Set the namespace from the filter or use the default
*/
protected function setNamespace()
{
$this->namespace =
(has_filter('sober/controller/namespace')
? apply_filters('sober/controller/namespace', rtrim($this->namespace))
: 'App\Controllers');
}
/**
* Set Path
*
* Set the path assuming PSR4 autoloading from $this->namespace
*/
protected function setPath()
{
$reflection = new \ReflectionClass($this->namespace .'\App');
$this->path = dirname($reflection->getFileName());
}
/**
* Set File List
*
* Recursively get file list and place into array
*/
protected function setListOfFiles()
{
$this->listOfFiles = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path));
}
/**
* Set Class Instances
*
* Load each Class instance and store in $instances[]
*/
protected function setClassesToRun()
{
foreach ($this->listOfFiles as $filename => $file) {
// Exclude non-PHP files
if (!Utils::isFilePhp($filename)) {
continue;
}
// Exclude non-Controller classes
if (!Utils::doesFileContain($filename, 'extends Controller')) {
continue;
}
// Set the classes to run
$this->classesToRun[] = $this->namespace . '\\' . pathinfo($filename, PATHINFO_FILENAME);
}
}
/**
* Set Class Alias
*
* Remove namespace from static functions
*/
public function setClassesAlias()
{
// Alias each class from $this->classesToRun
foreach ($this->classesToRun as $class) {
class_alias($class, (new \ReflectionClass($class))->getShortName());
}
}
/**
* Set Document Classes
*
* Set the classes required for the blade filter to pass on data
* @return array
*/
protected function addBodyDataClasses()
{
add_filter('body_class', function ($body) {
global $wp_query;
// Get the template hierarchy from WordPress
$templates = $this->hierarchy->getTemplates($wp_query);
// Reverse the templates returned from $this->hierarchy
$templates = array_reverse($templates);
// Add app-data to classes array
$classes[] = 'app-data';
foreach ($templates as $template) {
// Exclude .blade.php and index.php
if (strpos($template, '.blade.php') || $template === 'index.php') {
continue;
}
// Exclude index as we use app
if ($template === 'index') {
$template = 'index.php';
}
// Replace .php with -data and add to the classes array
$classes[] = basename(str_replace('.php', '-data', $template));
}
// Return the new body class list for WordPress
return array_merge($body, $classes);
});
}
/**
* Get Classes To Run
*
* @return array
*/
public function getClassesToRun()
{
return $this->classesToRun;
}
}