<?php
/***************************************************************
* ARASPHP WEB DEVELOPING FRAMEWORK
*
* Website: www.arasphp.org
* Author: Arturo López Pérez
* hide@address.com
* Version: 0.02
***************************************************************
*
* This file it's part of ArasPhp Web developing framework.
*
* This project is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Dispatcher
* This is the heart of the framework
* His duty is to split the url received into controller, action and arguments
* Makes sure they are valid before executing them
*/
class Dispatcher {
// Received array
var $argq;
// Arguments passed to the controller
var $args = array();
// Instance of the controller to be created
var $myController;
/**
* Gets current path and saves it into $argq
*/
function __construct($handUri = NULL)
{
// If no uri was sent, we take current navigator url
if($handUri == NULL)
{
// We delete the stuff right after the & if any
$temp = explode("&", $_SERVER['QUERY_STRING']);
// We split each arg of the url into an array
$this->argq = explode('/', $temp[0]);
} else {
// If was sent, we use the sent one.
$this->argq = explode("/", $handUri);
}
// If controller not passed, then is index
if(Validator::isEmpty($this->argq[0])) { $this->argq[0] = "index"; }
// If action not passed, then is index
if(Validator::isEmpty($this->argq[1])) { $this->argq[1] = "index"; }
// Count arguments passed in url
$argqlenght = count($this->argq);
// Count lenght of url
$lenght = strlen($_SERVER['QUERY_STRING']);
// If last character of url is a slash, last argument does not count
if(substr($_SERVER['QUERY_STRING'], $lenght-1, $lenght) == "/")
{
$argqlenght -= 1;
}
// Finally, we store the arguments
for($it = 2; $it < $argqlenght; $it++)
{
$this->args[] = $this->argq[$it];
}
// We create the controller
self::validControllerAction();
}
/**
* Checks if controller exists
* @return boolean
*/
public function validController()
{
if(file_exists(self::getController())) { return true; } else { return false; }
}
/**
* Checks if the requested action exist inside the controller
* @return boolean
*/
public function validControllerAction()
{
if(self::validController())
{
require_once(self::getController());
$myControllerName = self::getControllerClass();
// If first char of controller is _ is not valid
if(substr(self::getActionName(), 0,1) == "_") return false;
// We check existance
$this->myController = new $myControllerName();
if(method_exists($this->myController, $this->getActionName()))
{
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Checks if view exists
* @return boolean
*/
public function validView()
{
if(file_exists(self::getView())) { return true; } else { return false; }
}
/**
* @return string controllers path
*/
public function getController()
{
return(CONTROLLER . $this->argq[0] . ".php");
}
/**
* @return string view path
*/
private function getViewPath()
{
return(VIEW . $this->argq[0] . "/");
}
/**
* @return string controller file
*/
public function getControllerAction()
{
return $this->argq[1];
}
/**
* @return string view file
*/
public function getView()
{
return(self::getViewPath() . $this->argq[1] . ".php");
}
/**
* @return array of arguments passed to the controller
*/
public function getArgs()
{
return $this->args;
}
/**
* @return string name of controller
*/
public function getControllerName()
{
return $this->argq[0];
}
/**
* @return string name of action
*/
public function getActionName()
{
return $this->argq[1];
}
/**
* @return string name of class
*/
public function getControllerClass()
{
return(ucfirst($this->argq[0]) . "Controller");
}
/**
* getErrors
* Returns an array of dispatcher errors or NULL
*/
public function getErrors()
{
if(!self::validController()) {
$errors[] = "The controller does not exist";
}
if(!self::validControllerAction()) {
$errors[] = "The action does not exist in the controller";
}
if(!self::validView()) {
$errors[] = "The view does not exist";
}
if(count($this->args) > 10) {
$errors[] = "More than 10 arguments have been passed.";
}
if(isset($errors)) { return $errors; } else { return NULL; }
}
/**
* process
* Creates, executes and returns the results of the controller.
*/
public function process()
{
// We save the action to be performed
$Action = self::getActionName();
// We execute the action and store the results
$Legacy = $this->myController->execute($Action, self::getArgs());
return $Legacy;
}
/**
* Prints out all of it's info
*/
public function debug()
{
echo "<ul>";
echo "<li><b>Controller: </b> " . self::getController();
if(self::validController() == false) { echo" (not exists)"; }
echo "</li>";
echo "<li><b>Action requested: </b> " . self::getControllerAction();
if(self::validControllerAction() == false) { echo" (not exists)"; }
echo "</li>";
echo "<li><b>View: </b> " . self::getView();
if(self::validView() == false) { echo" (not exists)"; }
echo "</li>";
echo "<li><b>Arguments: </b> ";
if(count($this->args) == 0) { echo "No arguments passed"; } else {
echo "<br><ol>";
for($it = 0; $it < count($this->args); $it++)
{
echo "<li>Arg". $it ." -> ". $this->args[$it] ."</li>";
}
echo "</ol>";
}
echo "</li>";
echo "</ul>";
}
}
?>