forked from soberwp/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
97 lines (75 loc) · 2.39 KB
/
controller.php
File metadata and controls
97 lines (75 loc) · 2.39 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
<?php
namespace Sober\Controller;
use Brain\Hierarchy\Hierarchy;
use function App\sage;
/**
* Loader
*/
function loader()
{
if (!function_exists('\App\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()
{
if (!function_exists('\App\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');
}