Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
App::uses('CakeRoute', 'Routing/Route');
App::uses('CakeEvent', 'Event');
App::uses('CakeEventManager', 'Event');
App::uses('DebugKitDebugger', 'DebugKit.Lib');
 
/**
 * Possible Middleware Implementation
 *
 * Note that this thing SUCKS when using multiple routes.
 *
 * App::uses('MiddlewareRouter', 'Routing');
 * MiddlewareRouter::setup(array('Auth'));
 * Router::defaultRouteClass('MiddlewareRouter');
 *
 * app/Lib/Middleware/Middleware.php
 *
 *  <?php
 *  App::uses('CakeRoute', 'Routing/Route');
 *  App::uses('CakeEventListener', 'Event');
 *  class Middleware extends CakeRoute implements CakeEventListener {
 *      public function implementedEvents() {
 *          return array('Middleware.match' => 'process');
 *      }
 *  }
 *
 * app/Lib/Middleware/AuthMiddleware.php
 *
 *  <?php
 *  App::uses('Middleware', 'Middleware');
 *  class AuthMiddleware extends Middleware {
 *  
 *      public function process($event) {
 *          // Code to process your request
 *          // and potentially modify the url
 *          $event->stopPropagation();
 *      }
 *  }
 *
 * @package middleware
 * @author Jose Diaz-Gonzalez
 */
class MiddlewareRouter extends CakeRoute {
 
    public static $cakeRoute = null;
 
    public static $request = null;
 
    public static $classes = null;
 
    public static $oldClasses = null;
 
    protected static $middleware = array();
 
    protected static $_eventManager = null;
 
/**
 * Constructor for a Route
 *
 * @param string $template Template string with parameter placeholders
 * @param array $defaults Array of defaults for the route.
 * @param string $params Array of parameters and additional options for the Route
 * @return void
 */
    public function __construct($template, $defaults = array(), $options = array()) {
        $options = array_merge($this->options, (array) $options);
        if (isset($options['classes'])) {
            static::classes($options['classes']);
        } elseif (static::$oldClasses !== null) {
            static::$classes = static::$oldClasses;
            static::$oldClasses = null;
            static::attachMiddleware();
        }
 
        parent::__construct($template, $defaults, $options);
    }
 
/**
 * Setup the Middleware
 *
 * @param array $classes
 */
    public static function setup($classes = array()) {
        static::classes($classes);
 
        if (static::$request === null) {
            static::$request = clone Router::getRequest();
        }
 
        $count = count(Router::$routes);
        require CAKE . 'Config' . DS . 'routes.php';
        $newCount = count(Router::$routes);
 
        $params = Router::parse(static::$request->url);
        static::$request->addParams($params);
 
        Router::$routes = array();
        static::$cakeRoute = static::$request->params;
        static::$request = clone Router::getRequest();
    }
 
/**
 * Swaps middleware classes
 *
 * @param array $classes
 */
    public static function classes($classes = array()) {
        static::$oldClasses = static::$classes;
        static::$classes = static::normalizeObjectArray($classes);
        static::attachMiddleware();
    }
 
/**
 * Attaches middleware event listeners
 *
 * @return void
 */
    public static function attachMiddleware() {
        foreach (static::$middleware as $listener)  {
            $this->getEventManager()->detach($listener, 'Middleware.match');
        }
 
        foreach (static::$classes as $name => $settings) {
            if ($settings['plugin']) {
                $settings['plugin'] = "{$settings['plugin']}.";
            }
 
            $middlewareClass = $settings['name'] . 'Middleware';
            App::uses($middlewareClass, $settings['plugin'] . 'Middleware');
            static::getEventManager()->attach(new $middlewareClass($settings['settings']));
        }
    }
 
/**
 * Processes a request through middleware
 *
 * @param string $url The url to parse
 * @return mixed false on failure, or an array of request parameters
 */
    public function parse($url) {
        $cakeRoute = static::$cakeRoute;
        if (empty(static::$classes)) {
            return $cakeRoute;
        }
 
        if (class_exists('DebugKitDebugger')) {
            DebugKitDebugger::startTimer('MiddlewareRouter::dispatch()');
        }
 
        $event = new CakeEvent('Middleware.match', static::$request, compact('url', 'cakeRoute'));
        static::getEventManager()->dispatch($event);
 
        if (class_exists('DebugKitDebugger')) {
            DebugKitDebugger::stopTimer('MiddlewareRouter::dispatch()');
        }
 
        if (isset($event->data['useCakeRoute'])) {
            return $cakeRoute;
        }
 
        if (isset($event->data['route'])) {
            return $event->data['route'];
        }
 
        if (isset($event->data['output'])) {
            if ($event->data['output'] === false) {
                return false;
            }
 
            echo $event->data['output'];
        }
 
        if (!empty($event->data['exit'])) {
            exit;
        }
 
        return parent::parse($event->data['url']);
    }
 
/**
 * Returns the CakeEventManager manager instance that is handling any callbacks.
 *
 * @return CakeEventManager
 */
    public static function getEventManager() {
        if (empty(static::$_eventManager)) {
            static::$_eventManager = new CakeEventManager();
        }
        return static::$_eventManager;
    }
 
/**
 * Normalizes an object array, creates an array that makes lazy loading
 * easier
 *
 * @param array $objects Array of child objects to normalize.
 * @return array Array of normalized objects.
 */
    public static function normalizeObjectArray($objects) {
        $normal = array();
        foreach ($objects as $i => $class) {
            $settings = array();
            if (!is_int($i)) {
                $settings = (array)$class;
                $class = $i;
            }
 
            list($plugin, $name) = pluginSplit($class);
            $normal[$name] = compact('plugin', 'name', 'class', 'settings');
        }
 
        return $normal;
    }
 
}