Codice PHP:
/**
* You must set up a (app_)controllers_Index class
* Revise for factory controller.
*/
class w_FrontController {
/**
* @var w_http_Request
*/
protected $request= null;
/**
* @var string defaultController
* @default controllers_Index
*/
protected $defaultController= 'controllers_Index';
/**
* @var string defaultCommand
* @default commands_Read
*/
protected $defaultCommand= 'commands_Read';
/**
* @var reflection w_controller
*/
protected static $reflectionController= null;
/**
* @var reflection defaultController
*/
protected static $reflectionDefaultController= null;
public function __construct(w_http_Request $request){
$this->request= $request;
if(!self::$reflectionDefaultController){
try{
self::$reflectionController= new ReflectionClass('w_Controller');
/* I'm looking for troubles for the controllers_Index class only*/
self::$reflectionDefaultController= new ReflectionClass('controllers_Index');
//self::$reflectionDefaultController->getProperty('allowCommand');
//var_dump(self::$reflectionDefaultController->getProperty('allowCommand'));
//var_dump(self::$reflectionDefaultController->getStaticPropertyValue('allowCommand'));
}
catch(ReflectionException $e){
throw new Exception($e->getMessage().' you must set up your controllers_Index');
}
}
}
public function run(){
$routeParts= $this->request->parseQueryString();
$controller= $this->getController($routeParts);
$this->checkController($controller);
$controllerName= strtolower($controller->getName());
$defaultCommand= $controller->getDefaultCommand();
$allowCommands= $controller->getAllowCommands();
$comand= $this->getCommand($routeParts,$controllerName,$defaultCommand,$allowCommands);
$comand->execute();
$controller->run();
//var_dump();
}
protected function getController(array $routeParts){
if(!isset($routeParts[0]) || empty($routeParts[0])){
/* Controller have parameters ? Factory ?*/
return self::$reflectionDefaultController->newInstance();
}
try{
$controllerName= 'controllers_'.ucfirst($routeParts[0]);
$reflectionClass = new ReflectionClass($controllerName);
if($reflectionClass->isSubclassOf(self::$reflectionController)){
return $reflectionClass->newInstance();
}
return self::$reflectionDefaultController->newInstance();
}
catch(ReflectionException $e){
return self::$reflectionDefaultController->newInstance();
}
}
protected function getCommand(array $routeParts,$controllerName,$defaultCommand,array $allowedCommands){
$defaultCommand= 'commands_'.$controllerName.'_'.$defaultCommand;
if(!isset($routeParts[1]) || empty($routeParts[1])){
return new $defaultCommand();
}
if(in_array($routeParts[1],$allowedCommands)){
$command= 'commands_'.$controllerName.'_'.$routeParts[1];
return new $command();
}
return new $defaultCommand();
}
protected function checkController(w_Controller $controller){
$allowedCommands= $controller->getAllowCommands();
if(empty($allowedCommands)){
throw new Exception($controller->getName().' must have a allowed command at least');
}
}
}
// IL MODELLO PER IL CONTROLLER
abstract class w_Controller
{
protected $allowCommands= array();
abstract public function run();
abstract public function getDefaultCommand();
abstract public function getName();
public function getAllowCommands(){
return $this->allowCommands;
}
}
ancora un po pasticciato occorre
refactoring w_FrontController::getCommand
probabilmente metterò come parametro
il controller.