freenet-router |
Subversion Repositories: |
Compare with Previous - Blame - Download
<?php
namespace Phem\Controllers;
use Phem\Core\Object;
use Phem\Environment\ApplicationException;
use Phem\Environment\EnvironmentManager;
use ReflectionMethod;
abstract class Controller extends Object
{
protected $defaultTask = DEFAULT_TASK;
protected $entityManager;
protected $redirectToController;
protected $redirectToTask;
protected $redirectHiddenArgs;
protected $redirectArgs;
protected $loggedUser;
function __construct()
{
$this->loggedUser = EnvironmentManager::getSession()->getVar(ROOT_NAMESPACE . ".loggedUser");
}
function execute($task)
{
$args = EnvironmentManager::getSession()->getVar(ROOT_NAMESPACE . "_redirectedArgs");
if (($task != null) && (method_exists($this, $task)))
{
$taskToExecute = $task;
}
else if (method_exists($this, $this->defaultTask))
{
$taskToExecute = $this->defaultTask;
}
else
{
throw new ApplicationException("neither custom or default task exists for given controller");
}
$reflector = new ReflectionMethod($this, $taskToExecute);
$params = array();
$reflectedArgs = $reflector->getParameters();
foreach ($reflectedArgs as $arg)
{
if (EnvironmentManager::getRequestVar($arg->name) != null)
{
$params[] = EnvironmentManager::getRequestVar($arg->name);
}
elseif ((is_array($args) && array_key_exists($arg->name, $args)))
{
$params[] = $args[$arg->name];
}
else if ($arg->isDefaultValueAvailable())
{
$params[] = $arg->getDefaultValue();
}
else
{
$params[] = null;
}
}
call_user_func_array(array($this, $taskToExecute), $params);
}
function redirect()
{
if ($this->redirectToController == null)
return;
/** Perform the Request task */
$task = DEFAULT_TASK;
if ($this->redirectToTask != null)
$task = $this->redirectToTask;
$args['controller'] = $this->redirectToController;
$args['task'] = $task;
EnvironmentManager::getSession()->setVar(ROOT_NAMESPACE . "_redirectedArgs", $this->redirectHiddenArgs);
$linkArgs = ($this->redirectArgs == null) ? $args : array_merge($args, $this->redirectArgs);
$link = EnvironmentManager::getLinkBuilder()
->link($linkArgs);
header('Location:' . $link);
}
}