Skip to content

Commit

Permalink
Updated Application.php
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Jul 22, 2015
1 parent 8c83e93 commit 8dd2b02
Showing 1 changed file with 76 additions and 36 deletions.
112 changes: 76 additions & 36 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,31 @@
*
* @package Slytherin
*/
class Application {
class Application
{

protected $_constructorArguments = array();

protected $_controller = NULL;
protected $_methods = array();
protected $_router = NULL;
protected $arguments = array();
protected $controller = NULL;
protected $methods = array();
protected $router = NULL;

/**
* Load the necessary configurations
*/
public function __construct(Mux $router)
{
$this->_defineUrls();

$this->_router = $router;
$this->defineUrls();
$this->getController();

$this->router = $router;
}

/**
* Run the application
*/
public function run()
{
$this->_getController();

$route = $this->_router;
$route = $this->router;

/**
* Include the user's specified routes
Expand All @@ -42,8 +41,10 @@ public function run()
include 'app/config/routes.php';

$hasIndex = FALSE;
$index = array();
$routes = array();

foreach ($this->_methods as $method => $parameters) {
foreach ($this->methods as $method => $parameters) {
$options = array();
$regex = array();
$segments = NULL;
Expand All @@ -56,14 +57,14 @@ public function run()
$hasIndex = TRUE;
}

if ( ! empty($this->_constructorArguments)) {
$options['constructor_args'] = $this->_constructorArguments;
if ( ! empty($this->arguments)) {
$options['constructor_args'] = $this->arguments;
}

/**
* Implode the parameters and create a regex pattern
*/

if (is_array($parameters)) {
foreach ($parameters as $parameter => $defaultValue) {
$segments .= '/:' . $parameter;
Expand All @@ -84,17 +85,13 @@ public function run()
* Add an additional pattern for 'create' and 'edit' methods
*/

$pattern = '/' . $this->_controller . '/' . $method . $segments;
$pattern = '/' . $this->controller . '/' . $method . $segments;

/**
* Define the specified route
*/

$source = 'Controllers\\' . ucfirst($this->_controller) . ':' . $method;

/**
* Set the HTTP verb for the specified method
*/
$source = 'Controllers\\' . ucfirst($this->controller) . ':' . $method;

/**
* Add a new route if the method is index
Expand All @@ -103,8 +100,18 @@ public function run()
if ($hasIndex) {
$route->get(str_replace('/index', '', $pattern), $source, $options);
$hasIndex = FALSE;

$routes[] = array(
'pattern' => str_replace('/index', '', $pattern),
'source' => $source,
'options' => $options
);
}

/**
* Set the HTTP verb for the specified method
*/

switch ($method) {
case 'delete':
$route->delete($pattern, $source, $options);
Expand All @@ -121,15 +128,23 @@ public function run()
$route->get($pattern, $source, $options);
break;
}

$routes[] = array(
'pattern' => $pattern,
'source' => $source,
'options' => $options
);
}

// echo '<pre>';
// print_r($routes);
// echo '</pre>';

/**
* Set the URL to be dispatch
*/

$url = str_replace(BASE_URL, '', CURRENT_URL);
$url = (substr($url, -1) == '/') ? substr($url, 0, strlen($url) - 1) : $url;
$url = '/' . strtok($url, '?');
$url = $this->cleanUrl(BASE_URL, CURRENT_URL);

/**
* Dispatch and execute the route
Expand All @@ -138,12 +153,37 @@ public function run()
echo Executor::execute($route->dispatch($url));
}

/**
* Clean the specified URL
*
* @param string $baseUrl
* @param string $currentUrl
* @return string
*/
protected function cleanUrl($baseUrl, $currentUrl)
{
$url = str_replace($baseUrl, '', $currentUrl);

if (substr($url, -1) == '/') {
$url = substr($url, 0, strlen($url) - 1);
}

if (strpos($url, '?') !== FALSE) {
$questionMark = strpos($url, '?');

return '/' . substr($url, 0, $questionMark);
}

return '/' . $url;
}

/**
* Define the base and current urls
*/
protected function _defineUrls()
protected function defineUrls()
{
$baseUrl = 'http://localhost/';
$currentUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$isHttps = (isset($_SERVER['HTTPS'])) ? 's' : '';
$scriptName = substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME'])));

Expand All @@ -169,15 +209,15 @@ protected function _defineUrls()
*/

define('BASE_URL', $baseUrl);
define('CURRENT_URL', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
define('CURRENT_URL', $currentUrl);
}

/**
* Check the controller and get its contents
*
* @return boolean
*/
protected function _getController()
protected function getController()
{
/**
* Seperate the links from the string difference of BASE_URL and CURRENT_URL
Expand All @@ -193,13 +233,13 @@ protected function _getController()
return 0;
}

$controller = '\\Controllers\\' . ucfirst(strtok($segments[0], '?'));
$this->_controller = strtok($segments[0], '?');
$this->controller = strtok($segments[0], '?');
$controller = '\\Controllers\\' . ucfirst($this->controller);

try {
$class = new \ReflectionClass($controller);
} catch (\ReflectionException $exception) {
return 0;
return $exception;
}

$constructor = $class->getConstructor();
Expand All @@ -209,12 +249,12 @@ protected function _getController()
/**
* Get the class name without needing the class to be loaded
*/

preg_match('/\[\s\<\w+?>\s([\w]+)/s', $parameter->__toString(), $matches);
$object = isset($matches[1]) ? $matches[1] : NULL;

if ($object) {
$this->_constructorArguments[] = new $object();
$this->arguments[] = new $object();
}
}
}
Expand All @@ -228,17 +268,17 @@ protected function _getController()
* Add the curent method to the list of methods
*/

$this->_methods[$method->name] = NULL;
$this->methods[$method->name] = NULL;

/**
* Get the parameters for the each specified method
*/

foreach ($method->getParameters() as $parameter) {
$this->_methods[$method->name][$parameter->name] = NULL;
$this->methods[$method->name][$parameter->name] = NULL;

if ($parameter->isOptional()) {
$this->_methods[$method->name][$parameter->name] = $parameter->getDefaultValue();
$this->methods[$method->name][$parameter->name] = $parameter->getDefaultValue();
}
}
}
Expand Down

0 comments on commit 8dd2b02

Please sign in to comment.