-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMRouteManager.php
149 lines (123 loc) · 3.19 KB
/
KMRouteManager.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
<?php
/**
* @author kofimokome
*/
if ( ! class_exists( 'KMRouteManager' ) ) {
class KMRouteManager {
public $routes = [];
public $allQueryVars = [];
public $names;
public $currentMiddleware = '';
public $currentGroup = '';
public $middlewares = [];
private $context;
public function __construct( string $context ) {
$this->context = $context;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function registerMiddleware( string $middleware, Closure $callback ): void {
$this->middlewares[ $middleware ] = $callback;
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function middleware( string $middleware, Closure $callback ): void {
$this->currentMiddleware = $middleware;
$callback();
$this->currentMiddleware = '';
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function registerRoutes(): void {
foreach ( $this->routes as $route ) {
$route->registerRoute();
}
}
/**
* @throws Exception
* @since 1.0.0
* @author kofimokome
*/
public function defaultMiddleware( $view ): string {
return $this->renderView( $view );
}
/**
* @author kofimokome
* Returns the path of a view in the views directory
*/
public function viewPath( $template = '' ) {
$template = str_replace( '.', '/', $template );
$env = ( new KMEnv( $this->context ) )->getEnv();
$views_dir = $env['VIEWS_DIR'];
// remove trailing / from $views_dir if any
$views_dir = rtrim( $views_dir, '/' );
$plugin_dir = $this->getPluginDir();
return $plugin_dir . $views_dir . '/' . $template . '.php';
}
/**
* @author kofimokome
* Display the content of a view in the views directory
*/
public function renderView( $template = '', $echo = true ) {
$template = str_replace( '.', '/', $template );
// Start output buffering.
ob_start();
ob_implicit_flush( 0 );
try {
$env = ( new KMEnv( $this->context ) )->getEnv();
$views_dir = $env['VIEWS_DIR'];
// remove trailing / from $views_dir if any
$views_dir = rtrim( $views_dir, '/' );
$plugin_dir = $this->getPluginDir();
include $plugin_dir . $views_dir . '/' . $template . '.php';
} catch ( Exception $e ) {
ob_end_clean();
throw $e;
}
if ( $echo ) {
echo ob_get_clean();
} else {
return ob_get_clean();
}
}
/**
* @throws Exception
*/
public function getPluginDir() {
$plugin_basename = plugin_basename( $this->context );
$chars = explode( '/', $plugin_basename );
if ( sizeof( $chars ) > 0 ) {
$plugin_basename = $chars[0];
return WP_PLUGIN_DIR . '/' . $plugin_basename;
}
throw new Exception( 'Could not get plugin directory' );
}
/**
* @author kofimokome
* Gets the link associated with a route
*/
public function route( string $name, array $params = [] ): string {
if ( $route = $this->getRoute( $name ) ) {
foreach ( $params as $key => $value ) {
$route = str_replace( ':' . $key, $value, $route );
}
return site_url( $route );
} else {
return 'bro';
}
}
/**
* @author kofimokome
* @since 1.0.0
*/
public function getRoute( string $name ) {
return $this->names[ $name ] ?? false;
}
}
}