php-censor/src/Controller.php

110 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor;
2018-03-04 11:22:14 +01:00
use PHPCensor\Http\Request;
use PHPCensor\Http\Response;
2018-03-13 14:09:54 +01:00
abstract class Controller
{
/**
2018-03-04 10:15:21 +01:00
* @var Request
*/
protected $request;
/**
* @var Config
*/
protected $config;
/**
2018-03-13 14:09:54 +01:00
* @param Config $config
* @param Request $request
*/
2018-03-13 14:09:54 +01:00
public function __construct(Config $config, Request $request)
2013-10-10 02:01:06 +02:00
{
2018-03-04 10:15:21 +01:00
$this->config = $config;
$this->request = $request;
2018-02-17 05:59:02 +01:00
}
/**
2018-03-13 14:09:54 +01:00
* Initialise the controller.
2018-02-17 05:59:02 +01:00
*/
2018-03-13 14:09:54 +01:00
abstract public function init();
2018-03-04 10:15:21 +01:00
/**
* @param string $name
*
* @return boolean
*/
public function hasAction($name)
{
if (method_exists($this, $name)) {
return true;
}
if (method_exists($this, '__call')) {
return true;
}
return false;
}
2018-03-13 14:09:54 +01:00
/**
* Handles an action on this controller and returns a Response object.
*
* @param string $action
* @param array $actionParams
*
* @return Response
*/
public function handleAction($action, $actionParams)
{
return call_user_func_array([$this, $action], $actionParams);
}
2018-03-04 10:15:21 +01:00
/**
* Get a hash of incoming request parameters ($_GET, $_POST)
*
* @return array
*/
public function getParams()
{
return $this->request->getParams();
}
/**
* Get a specific incoming request parameter.
*
* @param string $key
* @param mixed $default Default return value (if key does not exist)
*
* @return mixed
*/
public function getParam($key, $default = null)
{
return $this->request->getParam($key, $default);
}
/**
* Change the value of an incoming request parameter.
*
* @param string $key
* @param mixed $value
*/
public function setParam($key, $value)
{
$this->request->setParam($key, $value);
}
/**
* Remove an incoming request parameter.
*
* @param string $key
*/
public function unsetParam($key)
{
$this->request->unsetParam($key);
}
2013-10-10 02:01:06 +02:00
}