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