. */ namespace Volkszaehler; use Volkszaehler\View\HTTP; use Volkszaehler\View; use Volkszaehler\Controller; use Volkszaehler\Util; /** * Backend dispatcher * * This class acts as a frontcontroller to route incomming requests * * @package default * @author Steffen Vogel */ class Dispatcher { /** * @var \Doctrine\ORM\EntityManager Doctrine EntityManager */ protected $em; /** * @var View\View */ protected $view; /** * @var Controller\Controller */ protected $controller; /** * @var Util\Debug optional debugging instance */ protected $debug = NULL; /** * @var array HTTP method => action mapping */ protected static $actionMapping = array( 'post' => 'add', 'delete' => 'delete', 'get' => 'get', 'pull' => 'edit' ); /** * Constructor */ public function __construct() { // create HTTP request & response (needed to initialize view & controller) $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')) !== FALSE || $debug = Util\Configuration::read('debug')) { if ($debug > 0) { $this->debug = new Util\Debug($debug); $this->em->getConnection()->getConfiguration()->setSQLLogger($this->debug); } } // TODO debug controll via configuration file // initialize view switch ($format) { case 'png': case 'jpeg': case 'gif': $this->view = new View\JpGraph($request, $response, $format); break; case 'json': case 'xml': case 'csv': $viewClassName = 'Volkszaehler\View\\' . strtoupper($format); if (!(Util\ClassLoader::classExists($viewClassName)) || !is_subclass_of($viewClassName, '\Volkszaehler\View\View')) { throw new \Exception('\'' . $viewClassName . '\' is not a valid View'); } $this->view = new $viewClassName($request, $response); break; case 'txt': $this->view = new View\PlainText($request, $response); break; default: throw new \Exception('unknown format: ' . $format); break; } // 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'); } $this->controller = new $controllerClassName($this->view, $this->em); } /** * Execute application */ public function run() { if ($this->view->request->getParameter('action')) { $action = $this->view->request->getParameter('action'); } elseif (self::$actionMapping[strtolower($this->view->request->getMethod())]) { $action = self::$actionMapping[strtolower($this->view->request->getMethod())]; } else { throw new \Exception('can\'t determine action'); } $this->controller->run($action); // run controllers actions (usually CRUD: http://de.wikipedia.org/wiki/CRUD) if (Util\Debug::isActivated()) { $this->view->addDebug($this->debug); } $this->view->sendResponse(); // render view & send http response } /** * Factory for doctrines entitymanager * * @todo create extra singleton class? */ public static function createEntityManager() { $config = new \Doctrine\ORM\Configuration; if (extension_loaded('apc')) { $cache = new \Doctrine\Common\Cache\ApcCache; $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); } $driverImpl = $config->newDefaultAnnotationDriver(BACKEND_DIR . '/lib/Model'); $config->setMetadataDriverImpl($driverImpl); $config->setProxyDir(BACKEND_DIR . '/lib/Model/Proxies'); $config->setProxyNamespace('Volkszaehler\Model\Proxies'); $config->setAutoGenerateProxyClasses(DEV_ENV == TRUE); return \Doctrine\ORM\EntityManager::create(Util\Configuration::read('db'), $config); } } ?>