1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Routing\Filter;
16:
17: use Cake\Event\Event;
18: use Cake\Http\ControllerFactory;
19: use Cake\Routing\DispatcherFilter;
20:
21: /**
22: * A dispatcher filter that builds the controller to dispatch
23: * in the request.
24: *
25: * This filter resolves the request parameters into a controller
26: * instance and attaches it to the event object.
27: */
28: class ControllerFactoryFilter extends DispatcherFilter
29: {
30: /**
31: * Priority is set high to allow other filters to be called first.
32: *
33: * @var int
34: */
35: protected $_priority = 50;
36:
37: /**
38: * Resolve the request parameters into a controller and attach the controller
39: * to the event object.
40: *
41: * @param \Cake\Event\Event $event The event instance.
42: * @return void
43: */
44: public function beforeDispatch(Event $event)
45: {
46: $request = $event->getData('request');
47: $response = $event->getData('response');
48: $event->setData('controller', $this->_getController($request, $response));
49: }
50:
51: /**
52: * Gets controller to use, either plugin or application controller.
53: *
54: * @param \Cake\Http\ServerRequest $request Request object
55: * @param \Cake\Http\Response $response Response for the controller.
56: * @return \Cake\Controller\Controller
57: * @throws \ReflectionException
58: */
59: protected function _getController($request, $response)
60: {
61: $factory = new ControllerFactory();
62:
63: return $factory->create($request, $response);
64: }
65: }
66: