diff --git a/backend/.htaccess b/backend/.htaccess index 20b76cb..3459c6a 100644 --- a/backend/.htaccess +++ b/backend/.htaccess @@ -1,6 +1,8 @@ -Order Allow,Deny -Deny from all + + RewriteEngine On - - Allow from all - + RewriteCond %{REQUEST_FILENAME} !-f + RewriteCond %{REQUEST_FILENAME} !-d + + RewriteRule (.*) index.php/$1 [L] + diff --git a/backend/lib/Controller/ChannelController.php b/backend/lib/Controller/ChannelController.php index a095c8d..386379c 100644 --- a/backend/lib/Controller/ChannelController.php +++ b/backend/lib/Controller/ChannelController.php @@ -23,6 +23,7 @@ namespace Volkszaehler\Controller; +use Volkszaehler\Util; use Volkszaehler\Model; /** @@ -31,71 +32,25 @@ use Volkszaehler\Model; * @author Steffen Vogel * @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() { - - } } ?> \ No newline at end of file diff --git a/backend/lib/Controller/EntityController.php b/backend/lib/Controller/EntityController.php new file mode 100644 index 0000000..0a26de9 --- /dev/null +++ b/backend/lib/Controller/EntityController.php @@ -0,0 +1,101 @@ +. + */ + +namespace Volkszaehler\Controller; + +use Volkszaehler\Util; +use Volkszaehler\Model; + +/** + * Entity controller + * + * @author Steffen Vogel + * @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() { + + } +} + +?> \ No newline at end of file diff --git a/backend/lib/Controller/GroupController.php b/backend/lib/Controller/GroupController.php index 7f024ba..cae35fc 100644 --- a/backend/lib/Controller/GroupController.php +++ b/backend/lib/Controller/GroupController.php @@ -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(); diff --git a/backend/lib/Dispatcher.php b/backend/lib/Dispatcher.php index 246cfe2..b1938ec 100644 --- a/backend/lib/Dispatcher.php +++ b/backend/lib/Dispatcher.php @@ -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() { diff --git a/backend/lib/Router.php b/backend/lib/Router.php new file mode 100644 index 0000000..202c487 --- /dev/null +++ b/backend/lib/Router.php @@ -0,0 +1,117 @@ +. + */ + +namespace Volkszaehler; + +/** + * @package default + * @author Steffen Vogel + */ +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; } +} + +?> \ No newline at end of file