change api urls to prettier version

This commit is contained in:
Steffen Vogel 2010-09-04 01:25:52 +02:00
parent 0fcd84e161
commit acbe108273
6 changed files with 254 additions and 94 deletions

View file

@ -1,6 +1,8 @@
Order Allow,Deny
Deny from all
<IfModule rewrite_module>
RewriteEngine On
<Files "index.php">
Allow from all
</Files>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1 [L]
</IfModule>

View file

@ -23,6 +23,7 @@
namespace Volkszaehler\Controller;
use Volkszaehler\Util;
use Volkszaehler\Model;
/**
@ -31,71 +32,25 @@ use Volkszaehler\Model;
* @author Steffen Vogel <info@steffenvogel.de>
* @package default
*/
class ChannelController extends Controller {
/**
* Get channels by filter
*
* @todo authentification/indentification
* @todo implement filters
*/
public function get() {
$dql = 'SELECT c, p FROM Volkszaehler\Model\Channel c LEFT JOIN c.properties p';
if ($uuid = $this->view->request->getParameter('uuid')) {
$dql .= ' WHERE c.uuid = \'' . $uuid . '\'';
}
$q = $this->em->createQuery($dql);
$channels = $q->getResult();
foreach ($channels as $channel) {
$this->view->addChannel($channel);
}
}
class ChannelController extends EntityController {
/**
* Add channel
*/
public function add() {
$properties = array();
$channel = new Model\Channel($this->view->request->getParameter('type'));
foreach ($this->view->request->getParameters() as $parameter => $value) {
if (Model\PropertyDefinition::exists($parameter)) {
$properties[] = new Model\Property($parameter, $value);
$property = new Model\Property($channel, $parameter, $value);
$channel->setProperty($property);
}
}
$channel = new Model\Channel($this->view->request->getParameter('type'), $properties);
$this->em->persist($channel);
$this->em->flush();
$this->view->addChannel($channel);
}
/**
* Delete channel by uuid
*
* @todo authentification/indentification
*/
public function delete() {
$ucid = $this->view->request->getParameter('ucid');
$channel = $this->em->getRepository('Volkszaehler\Model\Channel')->findOneBy(array('uuid' => $ucid));
$this->em->remove($channel);
$this->em->flush();
}
/**
* Edit channel properties
*
* @todo authentification/indentification
* @todo to be implemented
*/
public function edit() {
}
}
?>

View file

@ -0,0 +1,101 @@
<?php
/**
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @package default
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org 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 volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler\Controller;
use Volkszaehler\Util;
use Volkszaehler\Model;
/**
* Entity controller
*
* @author Steffen Vogel <info@steffenvogel.de>
* @package default
*/
class EntityController extends Controller {
/**
* Get channels by filter
*
* @todo authentification/indentification
* @todo implement filters
*/
public function get() {
$dql = 'SELECT c, p FROM Volkszaehler\Model\Channel c LEFT JOIN c.properties p';
if ($uuid = $this->view->request->getParameter('uuid')) {
$dql .= ' WHERE c.uuid = \'' . $uuid . '\'';
}
$q = $this->em->createQuery($dql);
$channels = $q->getResult();
foreach ($channels as $channel) {
$this->view->addChannel($channel);
}
}
/**
* Add channel
*/
public function add() {
$channel = new Model\Channel($this->view->request->getParameter('type'));
foreach ($this->view->request->getParameters() as $parameter => $value) {
if (Model\PropertyDefinition::exists($parameter)) {
$property = new Model\Property($channel, $parameter, $value);
$channel->setProperty($property);
}
}
$this->em->persist($channel);
$this->em->flush();
$this->view->addChannel($channel);
}
/**
* Delete channel by uuid
*
* @todo authentification/indentification
*/
public function delete() {
$ucid = $this->view->request->getParameter('ucid');
$channel = $this->em->getRepository('Volkszaehler\Model\Channel')->findOneBy(array('uuid' => $ucid));
$this->em->remove($channel);
$this->em->flush();
}
/**
* Edit channel properties
*
* @todo authentification/indentification
* @todo to be implemented
*/
public function edit() {
}
}
?>

View file

@ -39,21 +39,13 @@ class GroupController extends Controller {
* @todo filter to root groups when using recursion
*/
public function get() {
$dql = 'SELECT g, c, d FROM Volkszaehler\Model\Aggregator g LEFT JOIN g.children c LEFT JOIN g.channels d';
$dql = 'SELECT g, c, d, p FROM Volkszaehler\Model\Aggregator g LEFT JOIN g.children c LEFT JOIN g.channels d LEFT JOIN g.properties p';
// TODO fix this (depending on DDC-719)
if ($recursion = $this->view->request->getParameter('recursive')) {
//$dql .= ' WHERE g.parents IS EMPTY';
}
if ($uuid = $this->view->request->getParameter('uuid')) {
// TODO add conditions
}
if ($ugid = $this->view->request->getParameter('ugid')) {
// TODO add conditions
}
$q = $this->em->createQuery($dql);
$groups = $q->getResult();

View file

@ -52,6 +52,11 @@ class Dispatcher {
*/
protected $controller;
/**
* @var Router
*/
protected $router;
/**
* @var Util\Debug optional debugging instance
*/
@ -75,32 +80,27 @@ class Dispatcher {
$request = new HTTP\Request();
$response = new HTTP\Response();
if ($format = $request->getParameter('format')) {
$format = strtolower($format);
}
else {
$format = 'json'; // default view
}
if ($controller = $request->getParameter('controller')) {
$controller = strtolower($controller);
}
else {
throw new \Exception('no controller specified');
}
// initialize entity manager
$this->em = Dispatcher::createEntityManager();
// starting debugging
if (($debug = $request->getParameter('debug')) != NULL || $debug = Util\Configuration::read('debug')) {
if ($debug > 0) {
$this->debug = new Util\Debug($debug);
$this->em->getConnection()->getConfiguration()->setSQLLogger($this->debug);
if (($debugLevel = $request->getParameter('debug')) != NULL || $debugLevel = Util\Configuration::read('debug')) {
if ($debugLevel > 0) {
$this->debug = new Util\Debug($debugLevel, $this->em);
}
}
// initialize router
$this->router = new Router($this->em);
// initialize view
if ($this->router->getFormat()) {
$format = $this->router->getFormat();
}
else {
$format = 'json';
}
switch ($format) {
case 'png':
case 'jpeg':
@ -129,9 +129,8 @@ class Dispatcher {
}
// initialize controller
$controllerClassName = 'Volkszaehler\Controller\\' . ucfirst($controller) . 'Controller';
if (!(Util\ClassLoader::classExists($controllerClassName)) || !is_subclass_of($controllerClassName, '\Volkszaehler\Controller\Controller')) {
throw new \Exception('\'' . $controllerClassName . '\' is not a valid controller');
if (!($controllerClassName = $this->router->getController())) {
throw new \Exception('no controller specified');
}
$this->controller = new $controllerClassName($this->view, $this->em);
}
@ -140,8 +139,8 @@ class Dispatcher {
* Execute application
*/
public function run() {
if ($this->view->request->getParameter('action')) {
$action = $this->view->request->getParameter('action');
if ($this->router->getAction()) {
$action = $this->router->getAction();
}
elseif (self::$actionMapping[strtolower($this->view->request->getMethod())]) {
$action = self::$actionMapping[strtolower($this->view->request->getMethod())];
@ -151,18 +150,12 @@ class Dispatcher {
}
$this->controller->run($action); // run controllers actions (usually CRUD: http://de.wikipedia.org/wiki/CRUD)
if (Util\Debug::isActivated()) {
$this->addDebug($this->debug);
}
$this->view->sendResponse(); // render view & send http response
$this->view->sendResponse(); // render view & send http response
}
/**
* Factory for doctrines entitymanager
*
* @todo create extra singleton class?
* @todo add other caching drivers (memcache, xcache)
*/
public static function createEntityManager() {

117
backend/lib/Router.php Normal file
View file

@ -0,0 +1,117 @@
<?php
/**
* @package default
* @copyright Copyright (c) 2010, The volkszaehler.org project
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org 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 volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler;
/**
* @package default
* @author Steffen Vogel <info@steffenvogel.de>
*/
use Volkszaehler\Util;
use Doctrine\ORM;
class Router {
protected $format;
protected $controller;
protected $identifier;
protected $action;
protected static $controllerMapping = array(
'channels' => 'Volkszaehler\Controller\ChannelController',
'groups' => 'Volkszaehler\Controller\GroupController',
'tokens' => 'Volkszaehler\Controller\TokenController',
'capabilities' => 'Volkszaehler\Controller\CapabilitiesController'
);
/**
* Constructor
*
* @param ORM\EntityManager $em
*/
public function __construct(ORM\EntityManager $em) {
$this->parsePathInfo();
}
/**
* @todo add alternative url schema without PATH_INFO
*/
protected function parsePathInfo() {
// Request: http://sub.domain.local/vz/backend/channel/550e8400-e29b-11d4-a716-446655440000/edit.json?title=New Title
// PATH_INFO: /channel/550e8400-e29b-11d4-a716-446655440000/edit.json
$pi = $this->getPathInfo();
if ($pi) {
$pi = substr($pi, 1);
$pie = explode('/', $pi);
$i = 0;
if (isset($pie[$i]) && array_key_exists($pie[$i], self::$controllerMapping)) {
$this->controller = self::$controllerMapping[$pie[$i]];
$i++;
}
if (isset($pie[$i]) && preg_match('/[a-f0-9\-]{3,36}/', $pie[$i])) {
$this->identifier = $pie[$i];
}
$i++;
if (isset($pie[$i]) && strpos($pie[$i], '.') !== FALSE) {
list($this->action, $this->format) = explode('.', $pie[$i]);
}
elseif (isset($pie[$i])) {
$this->action = $pie[$i];
}
}
else {
throw new \Exception('no PATH_INFO found');
}
}
/**
* Get CGI environmental var PATH_INFO from webserver
*
* @return string
*/
protected static function getPathInfo() {
if (isset($_SERVER['PATH_INFO'])) {
return $_SERVER['PATH_INFO'];
}
elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
return $_SERVER['ORIG_PATH_INFO'];
}
else {
return FALSE;
}
}
/*
* Getter & setter
*/
public function getFormat() { return $this->format; }
public function getController() { return $this->controller; }
public function getIdentifier() { return $this->identifier; }
public function getAction() { return $this->action; }
}
?>