|
2 | 2 | /**
|
3 | 3 | * App.php
|
4 | 4 | *
|
5 |
| - * This file parses any given url and separates it into controller, |
6 |
| - * method, and data. This allows the application to direct the user |
7 |
| - * to the desired location and provide the controller any additional |
8 |
| - * information it may require. |
| 5 | + * This file parses any given url and separates it into controller, |
| 6 | + * method, and data. This allows the application to direct the user |
| 7 | + * to the desired location and provide the controller any additional |
| 8 | + * information it may require to run. |
9 | 9 | *
|
10 |
| - * @version 0.9 |
| 10 | + * @version 1.0 |
11 | 11 | *
|
12 |
| - * @author Joey Kimsey <joeyk4816@gmail.com> |
| 12 | + * @author Joey Kimsey <JoeyKimsey@thetempusproject.com> |
13 | 13 | *
|
14 |
| - * @link https://github.com/JoeyK4816/tempus-project-core |
| 14 | + * @link https://TheTempusProject.com/Core |
15 | 15 | *
|
16 | 16 | * @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
17 | 17 | */
|
|
20 | 20 |
|
21 | 21 | use TempusProjectCore\Classes\Debug as Debug;
|
22 | 22 | use TempusProjectCore\Classes\Config as Config;
|
23 |
| -use TempusProjectCore\Classes\CustomException as CustomException; |
| 23 | +use TempusProjectCore\Functions\Docroot as Docroot; |
24 | 24 | use TempusProjectCore\Classes\Input as Input;
|
25 | 25 |
|
26 | 26 | class App
|
27 | 27 | {
|
28 | 28 | //Default Controller
|
29 |
| - protected $controller = 'home'; |
| 29 | + protected $controllerName = 'home'; |
| 30 | + |
30 | 31 | //Default Method
|
31 |
| - protected $method = 'index'; |
| 32 | + protected $methodName = 'index'; |
| 33 | + |
| 34 | + protected $controllerPath = null; |
| 35 | + protected $indexPath = null; |
| 36 | + protected $path = null; |
| 37 | + protected $directed = false; |
32 | 38 | protected $params = [];
|
| 39 | + protected $url = null; |
33 | 40 |
|
34 |
| - public function __construct() |
| 41 | + /** |
| 42 | + * The constructor handles the entire process of parsing the url, |
| 43 | + * finding the controller/method, and calling the appropriate |
| 44 | + * class/function for the application. |
| 45 | + * |
| 46 | + * @param string $urlDirected - A custom URL to be parsed to determine |
| 47 | + * controller/method. (GET) url is used by |
| 48 | + * default if none is provided |
| 49 | + */ |
| 50 | + public function __construct($urlDirected = null) |
35 | 51 | {
|
36 |
| - Debug::group('Main Application'); |
| 52 | + Debug::group('TPC Application'); |
37 | 53 | Debug::log("Class Initiated: " . __CLASS__);
|
38 |
| - $path = Config::get('main/location'); |
39 |
| - $url = $this->parseUrl(); |
40 |
| - |
41 |
| - //////////////// |
42 |
| - // controller // |
43 |
| - //////////////// |
44 |
| - if (isset($url[0])) { |
45 |
| - if (file_exists($path.'Controllers/' . $url[0] . '.php')) { |
46 |
| - Debug::log("Modifying the controller from $this->controller to $url[0]"); |
47 |
| - $this->controller = strtolower($url[0]); |
48 |
| - unset($url[0]); |
49 |
| - } else { |
50 |
| - new CustomException('controller', $url[0]); |
51 |
| - unset($url[0]); |
52 |
| - } |
| 54 | + |
| 55 | + // Set Default Controller Locations |
| 56 | + $this->controllerPath = Docroot::getFull() . 'Controllers/'; |
| 57 | + $this->indexPath = Docroot::getFull(); |
| 58 | + |
| 59 | + // Set the application url to be used |
| 60 | + if (!empty($urlDirected)) { |
| 61 | + $this->directed = true; |
| 62 | + $this->url = Docroot::parseUrl($urlDirected); |
| 63 | + } else { |
| 64 | + $this->url = Docroot::parseUrl(); |
53 | 65 | }
|
54 |
| - if (!is_file($path.'Controllers/'.$this->controller.'.php')) { |
55 |
| - new CustomException('default_controller'); |
| 66 | + |
| 67 | + // Find the Controller |
| 68 | + $this->controllerName = $this->getController(); |
| 69 | + define('CORE_CONTROLLER', $this->controllerName); |
| 70 | + $this->controllerNameFull = (string) APP_SPACE . '\\Controllers\\' . $this->controllerName; |
| 71 | + |
| 72 | + // Ensure the controller is required |
| 73 | + Debug::log("Requiring Controller: $this->controllerName"); |
| 74 | + require $this->path . $this->controllerName . '.php'; // docroot |
| 75 | + |
| 76 | + // Find the Method |
| 77 | + $this->methodName = $this->getMethod(); |
| 78 | + define('CORE_METHOD', $this->methodName); |
| 79 | + |
| 80 | + ///////////////////////////////////////////////////////////////// |
| 81 | + // Load the appropriate Controller and Method which initiates // |
| 82 | + // the dynamic part of the application. // |
| 83 | + ///////////////////////////////////////////////////////////////// |
| 84 | + $this->loadController(); |
| 85 | + $this->loadMethod(); |
| 86 | + Debug::gend(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * This is used to determine the method to be called in the controller class. |
| 91 | + * |
| 92 | + * NOTE: If $url is set, this function will automatically remove the first |
| 93 | + * segment of the array regardless of whether or not it found the specified |
| 94 | + * method. |
| 95 | + * |
| 96 | + * @return string - The method name to be used by the application. |
| 97 | + */ |
| 98 | + private function getMethod() |
| 99 | + { |
| 100 | + if (empty($this->url[0])) { |
| 101 | + Debug::info('No Method Specified'); |
| 102 | + return $this->methodName; |
56 | 103 | }
|
57 |
| - define('CORE_CONTROLLER', $this->controller); |
58 |
| - Debug::log("Requiring Controller: $this->controller"); |
59 |
| - require_once $path.'Controllers/'. $this->controller . '.php'; |
60 |
| - $newController = APP_SPACE . "\\Controllers\\" . $this->controller; |
61 |
| - |
62 |
| - ///////////// |
63 |
| - // Method: // |
64 |
| - ///////////// |
65 |
| - if (isset($url[1])) { |
66 |
| - if (method_exists($newController, $url[1])) { |
67 |
| - Debug::log("Modifying the method from $this->method to $url[1]"); |
68 |
| - $this->method = strtolower($url[1]); |
69 |
| - unset($url[1]); |
70 |
| - } else { |
71 |
| - new CustomException('method', $url[1]); |
72 |
| - unset($url[1]); |
73 |
| - } |
| 104 | + if (method_exists($this->controllerNameFull, $this->url[0])) { |
| 105 | + Debug::log("Modifying the method from $this->methodName to " . $this->url[0]); |
| 106 | + $out = array_shift($this->url); |
| 107 | + return strtolower($out); |
74 | 108 | }
|
| 109 | + Debug::info('Method not found: ' . $this->url[0] . ', loading default.'); |
| 110 | + array_shift($this->url); |
| 111 | + return $this->methodName; |
| 112 | + } |
75 | 113 |
|
76 |
| - if (!method_exists($newController, $this->method)) { |
77 |
| - new CustomException('default_method', $this->controller . "::" . $this->method); |
| 114 | + /** |
| 115 | + * Using the $url array, this function will define the controller |
| 116 | + * name and path to be used. If the $urlDirected flag was used, |
| 117 | + * the first location checked is the indexPath. If this does |
| 118 | + * not exist, it will default back to the Controllers folder to search |
| 119 | + * for the specified controller. |
| 120 | + * |
| 121 | + * NOTE: If $url is set, this function will automatically remove the first |
| 122 | + * segment of the array regardless of whether or not it found the specified |
| 123 | + * controller. |
| 124 | + * |
| 125 | + * @return string - The controller name to be used by the application. |
| 126 | + */ |
| 127 | + private function getController() |
| 128 | + { |
| 129 | + if (empty($this->url[0])) { |
| 130 | + Debug::info('No Controller Specified.'); |
| 131 | + $this->path = $this->controllerPath; // docroot |
| 132 | + return $this->controllerName; |
| 133 | + } |
| 134 | + if ($this->directed && file_exists($this->indexPath . $this->url[0] . '.php')) { |
| 135 | + Debug::log("Modifying the controller from $this->controllerName to " . $this->url[0]); |
| 136 | + $out = array_shift($this->url); |
| 137 | + $this->path = $this->indexPath; // docroot |
| 138 | + return strtolower($out); |
| 139 | + } |
| 140 | + if (!$this->directed && file_exists($this->controllerPath . $this->url[0] . '.php')) { |
| 141 | + Debug::log("Modifying the controller from $this->controllerName to " . $this->url[0]); |
| 142 | + $out = array_shift($this->url); |
| 143 | + $this->path = $this->controllerPath; // docroot |
| 144 | + return strtolower($out); |
78 | 145 | }
|
79 |
| - define('CORE_METHOD', $this->method); |
80 |
| - $this->params = $url ? array_values($url) : []; |
81 |
| - Debug::log("Initiating controller: $this->controller"); |
82 |
| - $this->controller = new $newController(); |
83 |
| - Debug::log("Calling method : $this->method"); |
84 |
| - call_user_func_array([$this->controller, $this->method], $this->params); |
| 146 | + Debug::info('Could not locate specified controller: ' . $this->url[0]); |
| 147 | + $this->path = $this->controllerPath; // docroot |
| 148 | + array_shift($this->url); |
| 149 | + return $this->controllerName; |
85 | 150 | }
|
86 | 151 |
|
87 | 152 | /**
|
88 |
| - * This takes the url provided and returns it as an array. |
89 |
| - * |
90 |
| - * @return array - The exploded $_GET URL. |
| 153 | + * This function Initiates the specified controller and |
| 154 | + * stores it as an object in controllerObject. |
91 | 155 | */
|
92 |
| - public function parseUrl() |
| 156 | + private function loadController() |
93 | 157 | {
|
94 |
| - if (Input::get('url')) { |
95 |
| - return $url = explode('/', filter_var(rtrim(Input::get('url'), '/'), FILTER_SANITIZE_URL)); |
96 |
| - } |
| 158 | + Debug::group("Initiating controller: $this->controllerName", 1); |
| 159 | + $this->controllerObject = new $this->controllerNameFull; |
| 160 | + Debug::gend(); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * This function calls the application method/function from the |
| 165 | + * controllerObject. |
| 166 | + */ |
| 167 | + private function loadMethod() |
| 168 | + { |
| 169 | + $this->params = $this->url ? array_values($this->url) : []; |
| 170 | + Debug::group("Initiating method : $this->methodName", 1); |
| 171 | + call_user_func_array([$this->controllerObject, $this->methodName], $this->params); |
| 172 | + Debug::gend(); |
97 | 173 | }
|
98 | 174 | }
|
0 commit comments