introduced view component of MVC pattern
implemented JsonView
This commit is contained in:
parent
dc7d96206a
commit
71e8d9f9a1
5 changed files with 115 additions and 65 deletions
|
@ -20,22 +20,13 @@
|
|||
*/
|
||||
|
||||
class ChannelController extends Controller {
|
||||
private $jsonHeader = array();
|
||||
|
||||
public function __construct(HttpRequest $request, HttpResponse $response) {
|
||||
parent::__construct($request, $response);
|
||||
|
||||
$config = Registry::get('config');
|
||||
|
||||
$this->jsonHeader = array('source' => 'volkszaehler.org',
|
||||
'version' => '0.1',
|
||||
'storage' => $config['db']['backend']);
|
||||
|
||||
//$this->response->headers['Content-type'] = 'application/json'; // TODO uncomment in production use (just for debug)
|
||||
public function __construct(View $view) {
|
||||
parent::__construct($view);
|
||||
}
|
||||
|
||||
public function process() {
|
||||
switch ($this->request->get['action']) {
|
||||
public function execute() {
|
||||
switch ($this->view->request->get['action']) {
|
||||
case 'get':
|
||||
$this->get();
|
||||
break;
|
||||
|
@ -50,52 +41,48 @@ class ChannelController extends Controller {
|
|||
}
|
||||
|
||||
private function get() {
|
||||
$json = $this->jsonHeader;
|
||||
|
||||
if ($this->request->get['data'] == 'channels' || $this->request->get['data'] == 'pulses') {
|
||||
$json['type'] = 'channels';
|
||||
if ($this->view->request->get['data'] == 'channels' || $this->view->request->get['data'] == 'pulses') {
|
||||
$this->view->type = 'channels';
|
||||
$this->view->channels = array();
|
||||
|
||||
if ($this->request->get['data'] == 'channels') { // get all channels assigned to user
|
||||
$user = current(User::getByFilter(array('id' => 1)));
|
||||
if ($this->view->request->get['data'] == 'channels') { // get all channels assigned to user
|
||||
$user = current(User::getByFilter(array('id' => 1))); // TODO replace by authentication or session handling
|
||||
$channels = $user->getChannels();
|
||||
}
|
||||
else {
|
||||
$ids = explode(',', trim($this->request->get['ids']));
|
||||
$ids = explode(',', trim($this->view->request->get['ids']));
|
||||
$channels = Channel::getByFilter(array('id' => $ids), true, false); // get all channels with id in $ids as an array
|
||||
|
||||
$from = (isset($this->request->get['from'])) ? (int) $this->request->get['from'] : NULL;
|
||||
$to = (isset($this->request->get['to'])) ? (int) $this->request->get['to'] : NULL;
|
||||
$groupBy = (isset($this->request->get['groupby'])) ? $this->request->get['groupby'] : 400;
|
||||
$from = (isset($this->view->request->get['from'])) ? (int) $this->view->request->get['from'] : NULL;
|
||||
$to = (isset($this->view->request->get['to'])) ? (int) $this->view->request->get['to'] : NULL;
|
||||
$groupBy = (isset($this->view->request->get['groupby'])) ? $this->view->request->get['groupby'] : 400;
|
||||
|
||||
$json['from'] = $from; // TODO use min max tiestamps from Channel::getData()
|
||||
$json['to'] = $to;
|
||||
$this->view->from = $from; // TODO use min max timestamps from Channel::getData()
|
||||
$this->view->to = $to;
|
||||
}
|
||||
|
||||
$jsonChannels = array();
|
||||
foreach ($channels as $channel) {
|
||||
$jsonChannel = array('id' => (int) $channel->id,
|
||||
'resolution' => (int) $channel->resolution,
|
||||
'description' => $channel->description,
|
||||
'type' => $channel->type,
|
||||
'costs' => $channel->cost);
|
||||
$jsonChannel = $channel->toJson(); // TODO fix hardcoded json output
|
||||
|
||||
if ($this->request->get['data'] == 'pulses') {
|
||||
$json['type'] = 'pulses';
|
||||
if ($this->view->request->get['data'] == 'pulses') {
|
||||
$this->view->type = 'pulses';
|
||||
$jsonChannel['pulses'] = array();
|
||||
|
||||
foreach ($channel->getPulses($from, $to, $groupBy) as $pulse) {
|
||||
$jsonChannel['pulses'][] = array($pulse['timestamp'], $pulse['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$json['channels'][] = $jsonChannel;
|
||||
$jsonChannels[] = $jsonChannel;
|
||||
}
|
||||
|
||||
$this->view->channels = $jsonChannels;
|
||||
}
|
||||
|
||||
echo json_encode($json);
|
||||
}
|
||||
|
||||
private function log() {
|
||||
$ucid = $this->request->get['ucid'];
|
||||
$ucid = $this->view->request->get['ucid'];
|
||||
|
||||
$channel = Channel::getByUcid($ucid);
|
||||
|
||||
|
@ -103,7 +90,7 @@ class ChannelController extends Controller {
|
|||
$channel = Channel::addChannel($ucid);
|
||||
}
|
||||
|
||||
$channel->addData($this->request->get);
|
||||
$channel->addData($this->view->request->get);
|
||||
}
|
||||
|
||||
public function add($ucid) { // TODO rework
|
||||
|
|
|
@ -20,14 +20,13 @@
|
|||
*/
|
||||
|
||||
abstract class Controller {
|
||||
protected $request;
|
||||
protected $response;
|
||||
protected $view;
|
||||
|
||||
public function __construct(HttpRequest $request, HttpResponse $response) {
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
public function __construct(View $view) {
|
||||
$this->view = $view;
|
||||
}
|
||||
abstract public function process();
|
||||
|
||||
abstract public function execute();
|
||||
}
|
||||
|
||||
?>
|
|
@ -19,30 +19,32 @@
|
|||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*/
|
||||
|
||||
class FrontController {
|
||||
private $request;
|
||||
private $response;
|
||||
final class FrontController {
|
||||
private $controller;
|
||||
private $view;
|
||||
|
||||
public function __construct() {
|
||||
$this->request = new HttpRequest();
|
||||
$this->response = new HttpResponse();
|
||||
$request = new HttpRequest();
|
||||
$response = new HttpResponse();
|
||||
|
||||
$controller = $this->request->get['controller'] . 'Controller';
|
||||
|
||||
$rc = new ReflectionClass($controller);
|
||||
if (!$rc->isSubclassOf('Controller')) {
|
||||
throw new InvalidArgumentException('\'' . $controller . '\' is not a valid Controller');
|
||||
// create view instance
|
||||
$viewClass = new ReflectionClass($request->get['format'] . 'View');
|
||||
if (!$viewClass->isSubclassOf('View')) {
|
||||
throw new InvalidArgumentException('\'' . $viewClass->getName() . '\' is not a valid View');
|
||||
}
|
||||
$this->view = $viewClass->newInstanceArgs(array($request, $response));
|
||||
|
||||
$this->controller = $rc->newInstanceArgs(array($this->request, $this->response));
|
||||
// create controller instance
|
||||
$controllerClass = new ReflectionClass($request->get['controller'] . 'Controller');
|
||||
if (!$controllerClass->isSubclassOf('Controller')) {
|
||||
throw new InvalidArgumentException('\'' . $controllerClass->getName() . '\' is not a valid Controller');
|
||||
}
|
||||
$this->controller = $controllerClass->newInstanceArgs(array($this->view));
|
||||
}
|
||||
|
||||
public function handleRequest() {
|
||||
$this->controller->process();
|
||||
}
|
||||
|
||||
public function sendResponse() {
|
||||
$this->response->send();
|
||||
public function run() {
|
||||
$this->controller->execute(); // run controller
|
||||
$this->view->render(); // send response
|
||||
}
|
||||
}
|
||||
|
||||
|
|
54
lib/view/jsonview.php
Normal file
54
lib/view/jsonview.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?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
|
||||
*/
|
||||
|
||||
class JsonView extends View {
|
||||
private $data = array();
|
||||
|
||||
public function __construct(HttpRequest $request, HttpResponse $response) {
|
||||
parent::__construct($request, $response);
|
||||
|
||||
$config = Registry::get('config');
|
||||
|
||||
$this->source = 'volkszaehler.org';
|
||||
$this->version = VZ_VERSION;
|
||||
$this->storage = $config['db']['backend'];
|
||||
//$this->response->headers['Content-type'] = 'application/json'; // TODO uncomment in production use (just for debugging)
|
||||
}
|
||||
|
||||
public function __set($key, $value) {
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
public function __get($key) {
|
||||
return $this->data[$key];
|
||||
}
|
||||
|
||||
public function __isset($key) {
|
||||
return isset($this->data[$key]);
|
||||
}
|
||||
|
||||
public function render() {
|
||||
$this->time = round(microtime(true) - $this->created, 4);
|
||||
echo json_encode($this->data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -19,8 +19,16 @@
|
|||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*/
|
||||
|
||||
class Json {
|
||||
abstract class View {
|
||||
public $request;
|
||||
protected $response;
|
||||
protected $created; // holds timestamp of creation, used later to show time of execution
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
public function __construct(HttpRequest $request, HttpResponse $response) {
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
$this->created = microtime(true);
|
||||
}
|
||||
|
||||
abstract public function render();
|
||||
}
|
Loading…
Add table
Reference in a new issue