1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: |
|
15: |
|
16: | namespace LucidFrame\Core;
|
17: |
|
18: | |
19: | |
20: |
|
21: | class Middleware
|
22: | {
|
23: | const BEFORE = 'before';
|
24: | const AFTER = 'after';
|
25: |
|
26: | const FILTER_START_WITH = 'startWith';
|
27: | const FILTER_CONTAIN = 'contain';
|
28: | const FILTER_EQUAL = 'equal';
|
29: | const FILTER_EXCEPT = 'except';
|
30: |
|
31: |
|
32: | private static $before = array();
|
33: |
|
34: | private static $after = array();
|
35: |
|
36: | private static $id;
|
37: |
|
38: | private static $routeFilters = array();
|
39: |
|
40: | private static $orders = array();
|
41: |
|
42: | |
43: | |
44: | |
45: | |
46: | |
47: |
|
48: | public function register(\Closure $closure, $event = self::BEFORE)
|
49: | {
|
50: | self::$id = uniqid();
|
51: |
|
52: | if (in_array($event, array(self::BEFORE, self::AFTER))) {
|
53: | self::${$event}[self::$id] = $closure;
|
54: | $this->order(count(self::${$event}), $event);
|
55: | }
|
56: |
|
57: | return $this;
|
58: | }
|
59: |
|
60: | |
61: | |
62: | |
63: | |
64: | |
65: |
|
66: | public function on($key, $value)
|
67: | {
|
68: | if (self::$id) {
|
69: | self::$routeFilters[self::$id][$key][] = $value;
|
70: | }
|
71: |
|
72: | return $this;
|
73: | }
|
74: |
|
75: | |
76: | |
77: | |
78: | |
79: | |
80: |
|
81: | public function order($sort, $event = self::BEFORE)
|
82: | {
|
83: | if (self::$id) {
|
84: | self::$orders[$event][self::$id] = $sort;
|
85: | }
|
86: |
|
87: | return $this;
|
88: | }
|
89: |
|
90: | |
91: | |
92: |
|
93: | public static function runBefore()
|
94: | {
|
95: | asort(self::$orders[self::BEFORE]);
|
96: | self::invoke(self::BEFORE);
|
97: | }
|
98: |
|
99: | |
100: | |
101: |
|
102: | public static function runAfter()
|
103: | {
|
104: | asort(self::$orders[self::AFTER]);
|
105: | self::invoke(self::AFTER);
|
106: | }
|
107: |
|
108: | |
109: | |
110: | |
111: |
|
112: | private static function invoke($event)
|
113: | {
|
114: | $middlewares = $event == self::AFTER ? self::$after : self::$before;
|
115: |
|
116: | foreach (self::$orders[$event] as $id => $order) {
|
117: | $closure = $middlewares[$id];
|
118: |
|
119: | if (isset(self::$routeFilters[$id])) {
|
120: | $except = array();
|
121: | if (isset(self::$routeFilters[$id][self::FILTER_EXCEPT])) {
|
122: | foreach (self::$routeFilters[$id][self::FILTER_EXCEPT] as $exp) {
|
123: | $exp = is_array($exp) ? $exp : array($exp);
|
124: | $except = array_merge($except, $exp);
|
125: | }
|
126: | unset(self::$routeFilters[$id][self::FILTER_EXCEPT]);
|
127: | }
|
128: |
|
129: | if (count(self::$routeFilters[$id])) {
|
130: | foreach (self::$routeFilters[$id] as $filter => $value) {
|
131: | foreach ($value as $val) {
|
132: | switch($filter) {
|
133: | case self::FILTER_START_WITH:
|
134: | if (route_start($val, $except)) {
|
135: | $closure();
|
136: | }
|
137: | break;
|
138: |
|
139: | case self::FILTER_CONTAIN:
|
140: | $val = is_array($val) ? $val : array($val);
|
141: | if (route_contain($val, $except)) {
|
142: | $closure();
|
143: | }
|
144: | break;
|
145: |
|
146: | case self::FILTER_EQUAL:
|
147: | if (route_equal($val)) {
|
148: | $closure();
|
149: | }
|
150: | break;
|
151: | }
|
152: | }
|
153: | }
|
154: | } else {
|
155: | if (count($except) && call_user_func_array('route_except', $except)) {
|
156: | $closure();
|
157: | }
|
158: | }
|
159: | } else {
|
160: | $closure();
|
161: | }
|
162: | }
|
163: | }
|
164: | }
|
165: | |