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 pathcontroller.php
120 lines (94 loc) · 2.84 KB
/
controller.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
<?php
namespace Sober\Controller;
use Brain\Hierarchy\Hierarchy;
/**
* Sage Namespace
*/
function sage()
{
// Determine if project namespace has been changed
$sage = apply_filters('sober/controller/sage/namespace', 'App') . '\sage';
// Return the function if it exists
if (function_exists($sage)) {
return $sage;
}
// Return false if function does not exist
return false;
}
/**
* Loader
*/
function loader()
{
// Get Sage function
$sage = sage();
// Return if function does not exist
if (!$sage) {
return;
}
// Run WordPress hierarchy class
$hierarchy = new Hierarchy();
// Run Loader class and pass on WordPress hierarchy class
$loader = new Loader($hierarchy);
// Use the Sage DI container
$container = $sage();
// Loop over each class
foreach ($loader->getClassesToRun() as $class) {
// Create the class on the DI container
$controller = $container->make($class);
// Set the params required for template param
$controller->__setParams();
// Determine template location to expose data
$location = "sage/template/{$controller->__getTemplateParam()}-data/data";
// Pass data to filter
add_filter($location, function ($data) use ($container, $class) {
// Recreate the class so that $post is included
$controller = $container->make($class);
// Params
$controller->__setParams();
// Lifecycle
$controller->__before();
// Data
$controller->__setData($data);
// Lifecycle
$controller->__after();
// Return
return $controller->__getData();
}, 10, 2);
}
}
/**
* Blade
*/
function blade()
{
// Get Sage function
$sage = sage();
// Return if function does not exist
if (!$sage) {
return;
}
// Debugger
$sage('blade')->compiler()->directive('debug', function () {
return '<?php (new \Sober\Controller\Blade\Debugger(get_defined_vars())); ?>';
});
$sage('blade')->compiler()->directive('dump', function ($param) {
return "<?php (new Illuminate\Support\Debug\Dumper)->dump({$param}); ?>";
});
// Coder
$sage('blade')->compiler()->directive('code', function ($param) {
$param = ($param) ? $param : 'false';
return "<?php (new \Sober\Controller\Blade\Coder(get_defined_vars(), {$param})); ?>";
});
$sage('blade')->compiler()->directive('codeif', function ($param) {
$param = ($param) ? $param : 'false';
return "<?php (new \Sober\Controller\Blade\Coder(get_defined_vars(), {$param}, true)); ?>";
});
}
/**
* Hooks
*/
if (function_exists('add_action')) {
add_action('init', __NAMESPACE__ . '\loader');
add_action('init', __NAMESPACE__ . '\blade');
}