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 0.2.9
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Routing;
16:
17: use Cake\Event\EventDispatcherTrait;
18: use Cake\Event\EventListenerInterface;
19: use Cake\Http\ActionDispatcher;
20: use Cake\Http\Response;
21: use Cake\Http\ServerRequest;
22:
23: /**
24: * Dispatcher converts Requests into controller actions. It uses the dispatched Request
25: * to locate and load the correct controller. If found, the requested action is called on
26: * the controller
27: *
28: * @deprecated 3.6.0 Dispatcher is deprecated. You should update your application to use
29: * the Http\Server implementation instead.
30: */
31: class Dispatcher
32: {
33: use EventDispatcherTrait;
34:
35: /**
36: * Connected filter objects
37: *
38: * @var \Cake\Event\EventListenerInterface[]
39: */
40: protected $_filters = [];
41:
42: /**
43: * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
44: * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
45: *
46: * Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you
47: * want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
48: * For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods
49: * are also not accessible via URL.
50: *
51: * If no controller of given name can be found, invoke() will throw an exception.
52: * If the controller is found, and the action is not found an exception will be thrown.
53: *
54: * @param \Cake\Http\ServerRequest $request Request object to dispatch.
55: * @param \Cake\Http\Response $response Response object to put the results of the dispatch into.
56: * @return string|null if `$request['return']` is set then it returns response body, null otherwise
57: * @throws \LogicException When the controller did not get created in the Dispatcher.beforeDispatch event.
58: */
59: public function dispatch(ServerRequest $request, Response $response)
60: {
61: deprecationWarning(
62: 'Dispatcher is deprecated. You should update your application to use ' .
63: 'the Http\Server implementation instead.'
64: );
65: $actionDispatcher = new ActionDispatcher(null, $this->getEventManager(), $this->_filters);
66: $response = $actionDispatcher->dispatch($request, $response);
67: if ($request->getParam('return', null) !== null) {
68: return $response->body();
69: }
70:
71: return $response->send();
72: }
73:
74: /**
75: * Add a filter to this dispatcher.
76: *
77: * The added filter will be attached to the event manager used
78: * by this dispatcher.
79: *
80: * @param \Cake\Event\EventListenerInterface $filter The filter to connect. Can be
81: * any EventListenerInterface. Typically an instance of \Cake\Routing\DispatcherFilter.
82: * @return void
83: */
84: public function addFilter(EventListenerInterface $filter)
85: {
86: $this->_filters[] = $filter;
87: }
88:
89: /**
90: * Get the list of connected filters.
91: *
92: * @return \Cake\Event\EventListenerInterface[]
93: */
94: public function filters()
95: {
96: return $this->_filters;
97: }
98: }
99: