From 71e8d9f9a1829418c907d3361883d71f544de695 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 7 Jun 2010 02:13:05 +0200 Subject: [PATCH] introduced view component of MVC pattern implemented JsonView --- lib/controller/channelcontroller.php | 63 +++++++++++----------------- lib/controller/controller.php | 11 +++-- lib/controller/frontcontroller.php | 36 ++++++++-------- lib/view/jsonview.php | 54 ++++++++++++++++++++++++ lib/view/{json.php => view.php} | 16 +++++-- 5 files changed, 115 insertions(+), 65 deletions(-) create mode 100644 lib/view/jsonview.php rename lib/view/{json.php => view.php} (69%) diff --git a/lib/controller/channelcontroller.php b/lib/controller/channelcontroller.php index 291bf6c..12df55e 100644 --- a/lib/controller/channelcontroller.php +++ b/lib/controller/channelcontroller.php @@ -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 diff --git a/lib/controller/controller.php b/lib/controller/controller.php index 3eb5b2e..11ce546 100644 --- a/lib/controller/controller.php +++ b/lib/controller/controller.php @@ -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(); } ?> \ No newline at end of file diff --git a/lib/controller/frontcontroller.php b/lib/controller/frontcontroller.php index 642d5b0..385678f 100644 --- a/lib/controller/frontcontroller.php +++ b/lib/controller/frontcontroller.php @@ -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 } } diff --git a/lib/view/jsonview.php b/lib/view/jsonview.php new file mode 100644 index 0000000..7cf9383 --- /dev/null +++ b/lib/view/jsonview.php @@ -0,0 +1,54 @@ + + * + * 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); + } +} + +?> \ No newline at end of file diff --git a/lib/view/json.php b/lib/view/view.php similarity index 69% rename from lib/view/json.php rename to lib/view/view.php index 1c2b00e..d89f598 100644 --- a/lib/view/json.php +++ b/lib/view/view.php @@ -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 -} - -?> \ No newline at end of file + public function __construct(HttpRequest $request, HttpResponse $response) { + $this->request = $request; + $this->response = $response; + $this->created = microtime(true); + } + + abstract public function render(); +} \ No newline at end of file