direct call to controller actions from frontcontroller

added UserController (to be fully implemented)
This commit is contained in:
Steffen Vogel 2010-06-07 21:48:49 +02:00
parent 8884656ea7
commit 91adac34a2
4 changed files with 65 additions and 36 deletions

View file

@ -20,29 +20,8 @@
*/
class ChannelController extends Controller {
public function __construct(View $view) {
parent::__construct($view);
}
public function execute() {
switch ($this->view->request->get['action']) {
case 'get':
$this->get();
break;
case 'log':
$this->log();
break;
default:
throw new InvalidArgumentException('Invalid action specified!');
}
}
private function get() {
public function get() {
if ($this->view->request->get['data'] == 'channels' || $this->view->request->get['data'] == 'pulses') {
$this->view->type = 'channels';
$this->view->channels = array();
if ($this->view->request->get['data'] == 'channels') { // get all channels assigned to user
@ -55,7 +34,7 @@ class ChannelController extends Controller {
$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;
$groupBy = (isset($this->view->request->get['groupby'])) ? $this->view->request->get['groupby'] : NULL; // get all readings by default
$this->view->from = $from; // TODO use min max timestamps from Channel::getData()
$this->view->to = $to;
@ -66,7 +45,6 @@ class ChannelController extends Controller {
$jsonChannel = $channel->toJson(); // TODO fix hardcoded json output
if ($this->view->request->get['data'] == 'pulses') {
$this->view->type = 'pulses';
$jsonChannel['pulses'] = array();
foreach ($channel->getPulses($from, $to, $groupBy) as $pulse) {
@ -81,7 +59,7 @@ class ChannelController extends Controller {
}
}
private function log() {
public function log() {
$ucid = $this->view->request->get['ucid'];
$channel = Channel::getByUcid($ucid);

View file

@ -19,14 +19,22 @@
* http://www.gnu.org/copyleft/gpl.html
*/
abstract class Controller {
class ControllerException extends Exception {};
interface ControllerInterface {
public function __construct(View $view);
}
abstract class Controller implements ControllerInterface {
protected $view;
public function __construct(View $view) {
$this->view = $view;
}
abstract public function execute();
public function __call($method, $param) {
throw new ControllerException('Undefined controller action!');
}
}
?>

View file

@ -28,22 +28,23 @@ final class FrontController {
$response = new HttpResponse();
// create view instance
$viewClass = new ReflectionClass($request->get['format'] . 'View');
if (!$viewClass->isSubclassOf('View')) {
throw new InvalidArgumentException('\'' . $viewClass->getName() . '\' is not a valid View');
$rc = new ReflectionClass($request->get['format'] . 'View');
if (!$rc->isSubclassOf('View')) {
throw new InvalidArgumentException('\'' . $rc->getName() . '\' is not a valid View');
}
$this->view = $viewClass->newInstanceArgs(array($request, $response));
$this->view = $rc->newInstanceArgs(array($request, $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');
$rc = new ReflectionClass($request->get['controller'] . 'Controller');
if (!$rc->isSubclassOf('Controller')) {
throw new InvalidArgumentException('\'' . $rc->getName() . '\' is not a valid Controller');
}
$this->controller = $controllerClass->newInstanceArgs(array($this->view));
$this->controller = $rc->newInstanceArgs(array($this->view));
}
public function run() {
$this->controller->execute(); // run controller
$action = $this->view->request->get['action'];
$this->controller->$action(); // run controllers actions (usually CRUD: http://de.wikipedia.org/wiki/CRUD)
$this->view->render(); // send response
}
}

View file

@ -0,0 +1,42 @@
<?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 UserController extends Controller {
public function add() {
}
public function delete() {
}
public function edit() {
}
public function login() {
}
public function logout() {
}
}