1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Application
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Class to define an abstract Web application router.
14 *
15 * @since 12.2
16 * @deprecated 3.7.0 Use the `joomla/router` package via Composer instead
17 */
18 abstract class JApplicationWebRouter
19 {
20 /**
21 * @var JApplicationWeb The web application on whose behalf we are routing the request.
22 * @since 12.2
23 */
24 protected $app;
25
26 /**
27 * @var string The default page controller name for an empty route.
28 * @since 12.2
29 */
30 protected $default;
31
32 /**
33 * @var string Controller class name prefix for creating controller objects by name.
34 * @since 12.2
35 */
36 protected $controllerPrefix;
37
38 /**
39 * @var JInput An input object from which to derive the route.
40 * @since 12.2
41 */
42 protected $input;
43
44 /**
45 * Constructor.
46 *
47 * @param JApplicationWeb $app The web application on whose behalf we are routing the request.
48 * @param JInput $input An optional input object from which to derive the route. If none
49 * is given than the input from the application object will be used.
50 *
51 * @since 12.2
52 */
53 public function __construct(JApplicationWeb $app, JInput $input = null)
54 {
55 $this->app = $app;
56 $this->input = ($input === null) ? $this->app->input : $input;
57 }
58
59 /**
60 * Find and execute the appropriate controller based on a given route.
61 *
62 * @param string $route The route string for which to find and execute a controller.
63 *
64 * @return mixed The return value of the controller executed
65 *
66 * @since 12.2
67 * @throws InvalidArgumentException
68 * @throws RuntimeException
69 */
70 public function execute($route)
71 {
72 // Get the controller name based on the route patterns and requested route.
73 $name = $this->parseRoute($route);
74
75 // Get the controller object by name.
76 $controller = $this->fetchController($name);
77
78 // Execute the controller.
79 return $controller->execute();
80 }
81
82 /**
83 * Set the controller name prefix.
84 *
85 * @param string $prefix Controller class name prefix for creating controller objects by name.
86 *
87 * @return JApplicationWebRouter This object for method chaining.
88 *
89 * @since 12.2
90 */
91 public function setControllerPrefix($prefix)
92 {
93 $this->controllerPrefix = (string) $prefix;
94
95 return $this;
96 }
97
98 /**
99 * Set the default controller name.
100 *
101 * @param string $name The default page controller name for an empty route.
102 *
103 * @return JApplicationWebRouter This object for method chaining.
104 *
105 * @since 12.2
106 */
107 public function setDefaultController($name)
108 {
109 $this->default = (string) $name;
110
111 return $this;
112 }
113
114 /**
115 * Parse the given route and return the name of a controller mapped to the given route.
116 *
117 * @param string $route The route string for which to find and execute a controller.
118 *
119 * @return string The controller name for the given route excluding prefix.
120 *
121 * @since 12.2
122 * @throws InvalidArgumentException
123 */
124 abstract protected function parseRoute($route);
125
126 /**
127 * Get a JController object for a given name.
128 *
129 * @param string $name The controller name (excluding prefix) for which to fetch and instance.
130 *
131 * @return JController
132 *
133 * @since 12.2
134 * @throws RuntimeException
135 */
136 protected function fetchController($name)
137 {
138 // Derive the controller class name.
139 $class = $this->controllerPrefix . ucfirst($name);
140
141 // If the controller class does not exist panic.
142 if (!class_exists($class) || !is_subclass_of($class, 'JController'))
143 {
144 throw new RuntimeException(sprintf('Unable to locate controller `%s`.', $class), 404);
145 }
146
147 // Instantiate the controller.
148 $controller = new $class($this->input, $this->app);
149
150 return $controller;
151 }
152 }
153