2010-06-06 16:05:46 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 by Justin Otherguy <justin@justinotherguy.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License (either version 2 or
|
|
|
|
* version 3) as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* For more information on the GPL, please go to:
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
2010-07-18 17:12:00 +02:00
|
|
|
namespace Volkszaehler\View;
|
2010-06-07 21:47:51 +02:00
|
|
|
|
2010-07-20 21:27:53 +02:00
|
|
|
use Volkszaehler\Util;
|
|
|
|
|
2010-07-20 19:10:45 +02:00
|
|
|
abstract class View {
|
2010-06-07 02:13:05 +02:00
|
|
|
public $request;
|
|
|
|
protected $response;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
|
|
|
public function __construct(Http\Request $request, Http\Response $response) {
|
|
|
|
$this->request = $request;
|
|
|
|
$this->response = $response;
|
2010-07-09 23:50:20 +02:00
|
|
|
|
2010-06-07 21:47:51 +02:00
|
|
|
// error & exception handling by view
|
|
|
|
set_exception_handler(array($this, 'exceptionHandler'));
|
2010-06-08 15:39:31 +02:00
|
|
|
set_error_handler(array($this, 'errorHandler'));
|
2010-06-07 02:13:05 +02:00
|
|
|
}
|
|
|
|
|
2010-07-18 17:12:00 +02:00
|
|
|
/*
|
|
|
|
* error & exception handling
|
|
|
|
*/
|
2010-06-07 21:47:51 +02:00
|
|
|
final public function errorHandler($errno, $errstr, $errfile, $errline) {
|
2010-07-18 17:12:00 +02:00
|
|
|
$this->exceptionHandler(new \ErrorException($errstr, 0, $errno, $errfile, $errline));
|
2010-06-08 15:39:31 +02:00
|
|
|
}
|
|
|
|
|
2010-07-18 17:12:00 +02:00
|
|
|
final public function exceptionHandler(\Exception $exception) {
|
2010-06-11 23:17:05 +02:00
|
|
|
$this->addException($exception);
|
|
|
|
|
|
|
|
//$this->status = STATUS_EXCEPTION; // TODO add status reporting to API
|
2010-07-18 17:12:00 +02:00
|
|
|
|
|
|
|
$code = ($exception->getCode() == 0 && Http\Response::getCodeDescription($exception->getCode())) ? 400 : $exception->getCode();
|
|
|
|
$this->response->setCode($code);
|
2010-06-11 23:17:05 +02:00
|
|
|
|
|
|
|
$this->render();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2010-07-18 17:12:00 +02:00
|
|
|
public function render() {
|
|
|
|
$this->response->send();
|
2010-06-07 21:47:51 +02:00
|
|
|
}
|
2010-07-20 19:10:45 +02:00
|
|
|
|
|
|
|
public function addException(\Exception $e) {
|
|
|
|
echo $e;
|
|
|
|
}
|
|
|
|
|
2010-07-20 21:27:53 +02:00
|
|
|
public function addDebug(Util\Debug $debug) {
|
2010-07-20 19:10:45 +02:00
|
|
|
|
|
|
|
}
|
2010-06-07 02:13:05 +02:00
|
|
|
}
|