From ce07e3d3f4cd90bedd0107d8b4b1bf07d29d0afd Mon Sep 17 00:00:00 2001 From: Romain d'Alverny Date: Mon, 12 Nov 2012 11:36:31 +0000 Subject: reorg code, license headers --- lib/pinq/App.php | 8 ++++++- lib/pinq/Cache.php | 55 ++++++++++++++++++++++++++++++++++++++++++++ lib/pinq/Controller.php | 61 +++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 lib/pinq/Cache.php (limited to 'lib/pinq') diff --git a/lib/pinq/App.php b/lib/pinq/App.php index 2f2ebd88a..587b47a86 100644 --- a/lib/pinq/App.php +++ b/lib/pinq/App.php @@ -6,11 +6,17 @@ * @category Mageia * @package Mageia\Web\www\Pinq * @author rda - * @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+ * @link http://www.mageia.org/ * + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2+ + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License aspublished by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. */ + /** */ abstract class Pinq_App diff --git a/lib/pinq/Cache.php b/lib/pinq/Cache.php new file mode 100644 index 000000000..8a5a3d4d1 --- /dev/null +++ b/lib/pinq/Cache.php @@ -0,0 +1,55 @@ + + * @link http://www.mageia.org/ + * + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2+ + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License aspublished by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. +*/ + +abstract class Pinq_Cache +{ + abstract public function __construct(); + + /** + * @param string $key + * + * @return mixed + */ + abstract public function get($key); + + /** + * @param mixed $value + * @param string $key + * @param integer $timeout + */ + abstract public function set($value, $key, $timeout = 0); +} + +class Pinq_Memcache_Cache extends Pinq_Cache +{ + + public function __construct($timeout = 0) + { + $this->timeout = $timeout; + } + + public function get($key) + { + + } + + public function set($value, $key, $timeout = 0) + { + $timeout = $timeout > 0 ? $timeout : $this->timeout; + } +} \ No newline at end of file diff --git a/lib/pinq/Controller.php b/lib/pinq/Controller.php index 6043cc0e1..8ceb26ade 100644 --- a/lib/pinq/Controller.php +++ b/lib/pinq/Controller.php @@ -6,9 +6,14 @@ * @category Mageia * @package Mageia\Web\www\Pinq * @author rda - * @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2+ * @link http://www.mageia.org/ * + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2+ + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License aspublished by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. */ /** @@ -26,11 +31,36 @@ class Pinq_Controller $this->_server = $server; } + /** + */ + public static function run($app_root, $routes, $_server, $cache) + { + $pc = new self($app_root, $_server); + $pc->init(); + + if (isset($cache) + && $res = $cache->get($pc->get_cache_key())) { + + // + } else { + + $res = $pc->_run($routes); + + if (isset($cache)) { + $cache->set($res, $pc->cache_key()); + } + } + + $pc->publish($res); + unset($pc); + } + /** */ public function init() { - $this->uri = $this->_server['REQUEST_URI']; + $this->method = $this->_server['REQUEST_METHOD']; + $this->uri = $this->_server['REQUEST_URI']; if (strpos('?', $this->uri) !== false) { $uri = explode('?', $this->uri); @@ -39,19 +69,40 @@ class Pinq_Controller } } + /** + */ + public function get_cache_key() + { + return sha1(implode('#', array($this->method, $this->uri))); + } + + /** + */ + public function publish($res) + { + + foreach ($res['Headers'] as $h) { + header($h); + } + echo $res['Body']; + + } + /** * @param array $routes * * @return boolean */ - public function run($routes = null) + private function _run($routes = null) { // static, image files are expected to be served directly by the server. // detect path language; if not set, redirect to best fallback language (English for now), end $this->lang = $this->get_request_language($this->uri); + if (!$this->lang_is_managed($this->lang)) { - // TODO + // TODO - ignore, with a special code or redirect? + $this->lang = 'en'; } // delegate to declared routes/apps @@ -144,7 +195,7 @@ class Pinq_Controller function fallback_to_previous_mode($uri, $lang) { $alt_uri = sprintf( - '/%s/%s', + '/%s/%s', 'en', substr($uri, strlen($lang) + 2) ); -- cgit v1.2.1