. */ namespace Volkszaehler\View; use Volkszaehler\Model; use Volkszaehler\View\HTTP; use Volkszaehler\Util; /** * Interface for all View classes * * @author Steffen Vogel * @package default */ interface ViewInterface { public function addChannel(Model\Channel $channel, array $data = NULL); function addAggregator(Model\Aggregator $aggregator); function addDebug(Util\Debug $debug); } /** * Superclass for all view classes * * @package default * @author Steffen Vogel * */ abstract class View implements ViewInterface { /** * @var integer round all values to x decimals */ const PRECISSION = 5; /** * @var HTTP\Request * @todo do we need this? * @todo why public? not via getter? */ public $request; /** * @var HTTP\Response */ protected $response; /** * Constructor * * @param HTTP\Request $request * @param HTTP\Response $response */ public function __construct(HTTP\Request $request, HTTP\Response $response) { $this->request = $request; $this->response = $response; // error & exception handling by view set_exception_handler(array($this, 'exceptionHandler')); set_error_handler(array($this, 'errorHandler')); } /** * error & exception handling */ final public function errorHandler($errno, $errstr, $errfile, $errline) { $this->exceptionHandler(new \ErrorException($errstr, 0, $errno, $errfile, $errline)); } final public function exceptionHandler(\Exception $exception) { $this->addException($exception, Util\Debug::isActivated()); $code = ($exception->getCode() == 0 && HTTP\Response::getCodeDescription($exception->getCode())) ? 400 : $exception->getCode(); $this->response->setCode($code); $this->sendResponse(); die(); } public function sendResponse() { if (Util\Debug::isActivated()) { $this->addDebug(Util\Debug::getInstance()); } $this->renderResponse(); $this->response->send(); } protected abstract function renderResponse(); protected abstract function addException(\Exception $exception); } ?>