Skip to content

Commit c313f5b

Browse files
committed
Release Version 1.0
This is release version 1.0 Amongst many bugfixes, this update was primarily focused on refactoring. - Spelling Fixes - modifications to adhere to PSR standards - naming modifications for clarity - improvements to functionality - many bugfixes
1 parent 6dad0c9 commit c313f5b

33 files changed

+2842
-1419
lines changed

App.php

Lines changed: 137 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
/**
33
* App.php
44
*
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.
99
*
10-
* @version 0.9
10+
* @version 1.0
1111
*
12-
* @author Joey Kimsey <joeyk4816@gmail.com>
12+
* @author Joey Kimsey <JoeyKimsey@thetempusproject.com>
1313
*
14-
* @link https://github.com/JoeyK4816/tempus-project-core
14+
* @link https://TheTempusProject.com/Core
1515
*
1616
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
1717
*/
@@ -20,79 +20,155 @@
2020

2121
use TempusProjectCore\Classes\Debug as Debug;
2222
use TempusProjectCore\Classes\Config as Config;
23-
use TempusProjectCore\Classes\CustomException as CustomException;
23+
use TempusProjectCore\Functions\Docroot as Docroot;
2424
use TempusProjectCore\Classes\Input as Input;
2525

2626
class App
2727
{
2828
//Default Controller
29-
protected $controller = 'home';
29+
protected $controllerName = 'home';
30+
3031
//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;
3238
protected $params = [];
39+
protected $url = null;
3340

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)
3551
{
36-
Debug::group('Main Application');
52+
Debug::group('TPC Application');
3753
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();
5365
}
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;
56103
}
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);
74108
}
109+
Debug::info('Method not found: ' . $this->url[0] . ', loading default.');
110+
array_shift($this->url);
111+
return $this->methodName;
112+
}
75113

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);
78145
}
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;
85150
}
86151

87152
/**
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.
91155
*/
92-
public function parseUrl()
156+
private function loadController()
93157
{
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();
97173
}
98174
}

CODE_OF_CONDUCT.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at webmaster@thetempusproject.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WIP

0 commit comments

Comments
 (0)