Created
July 1, 2011 20:34
-
-
Save jdp/1059338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once "lib/compra.php"; | |
| require_once "lib/sheen.php"; | |
| require_once "lib/beggar.php"; | |
| use compra\Compra; | |
| use sheen\View; | |
| use beggar\Request; | |
| function templatize($req, $res) { | |
| $title = isset($res->title) ? $res->title : "PBBG"; | |
| $player = isset($req->player) ? $req->player : NULL; | |
| $layout = new View("layout", compact("title", "player")); | |
| $layout->render(array(), $res->view->render(compact("player"), NULL, TRUE)); | |
| } | |
| Compra::except("beggar\Exception", function($req, $res, $e) { | |
| $error = "Our web service seems to be malfunctioning. Please bear with us."; | |
| $res->view = new View("error", compact("error")); | |
| $res->title = "Error"; | |
| templatize($req, $res); | |
| }); | |
| get("/", function($req, $res) { | |
| if (isset($req->player)) { | |
| $res->title = "Headquarters"; | |
| $res->view = new View("home"); | |
| } | |
| else { | |
| $res->title = "Welcome"; | |
| $res->view = new View("index"); | |
| } | |
| }); | |
| get("/login", function($req, $res) { | |
| if (isset($req->player)) { | |
| $res->redirect("/"); | |
| } | |
| $res->title = "Login"; | |
| $res->view = new View("login"); | |
| }); | |
| get("/logout", function($req, $res) { | |
| session_destroy(); | |
| $res->redirect("/"); | |
| }); | |
| post("/login", function($req, $res) { | |
| $username = trim($_POST['username']); | |
| $password = trim($_POST['password']); | |
| $api = new Request($username, $password); | |
| try { | |
| $api->players->self->_get(); | |
| $_SESSION['credentials'] = "{$username}:{$password}"; | |
| $res->redirect("/"); | |
| } | |
| catch (beggar\ForbiddenException $e) { | |
| $error = $e->getMessage(); | |
| } | |
| $res->title = "Login"; | |
| $res->view = new View("login", compact("error")); | |
| }); | |
| get("/battle", function($req, $res) { | |
| $players = $req->ws->players->_get(); | |
| $res->title = "Battle"; | |
| $res->view = new View("battle", compact("players")); | |
| }); | |
| Compra::before(function($req, $res) { | |
| session_start(); | |
| if (isset($_SESSION['credentials'])) { | |
| // TODO: Store session data in Redis or memcached | |
| list($username, $password) = explode(":", $_SESSION['credentials']); | |
| $req->ws = new Request($username, $password); | |
| $req->player = (object)$req->ws->players->self->_get(); | |
| } | |
| }); | |
| Compra::after(function($req, $res) { | |
| templatize($req, $res); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Copyright (c) 2011 Justin Poliey <[email protected]> | |
| * | |
| * Permission to use, copy, modify, and/or distribute this software for any | |
| * purpose with or without fee is hereby granted, provided that the above | |
| * copyright notice and this permission notice appear in all copies. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
| * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
| * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
| * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
| * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| */ | |
| namespace sheen; | |
| class RenderException extends \Exception { | |
| } | |
| class View { | |
| /** | |
| * The filename of the view | |
| * @var string | |
| * @access private | |
| */ | |
| private $filename; | |
| /** | |
| * The directory in which view files are stored | |
| * @var string | |
| * @access protected | |
| */ | |
| public $viewDirectory = "../views"; | |
| /** | |
| * The file extension that view files are saved with | |
| * @var string | |
| * @access protected | |
| */ | |
| public $viewExtension = ".html.php"; | |
| /** | |
| * Locals are an array of values that will be treated as variables inside of the view. | |
| * @var array | |
| * @access private | |
| */ | |
| private $locals = array(); | |
| /** | |
| * Flag indicating whether or not `render()` method has been called. | |
| * @var boolean | |
| * @access public | |
| */ | |
| public $rendered = FALSE; | |
| /** | |
| * The `$content` parameter of `render()` is passed into this | |
| * @var string | |
| * @access public | |
| */ | |
| public $content; | |
| function __construct($name, $locals = array()) { | |
| $rel_fname = (is_array($name)? join($name, DIRECTORY_SEPARATOR): $name); | |
| $this->filename = join(array(dirname(__FILE__), $this->viewDirectory, $rel_fname.$this->viewExtension), DIRECTORY_SEPARATOR); | |
| $this->locals = $locals; | |
| } | |
| function render($locals = array(), $content = NULL, $return = FALSE) { | |
| if (!is_readable($this->filename)) { | |
| throw new RenderException("Could not load view {$this->filename}"); | |
| } | |
| $this->locals = array_merge($this->locals, $locals); | |
| if ($return) { | |
| ob_start(); | |
| } | |
| $this->content = $content; | |
| extract($this->locals); | |
| include $this->filename; | |
| $this->rendered = TRUE; | |
| if ($return) { | |
| return ob_get_clean(); | |
| } | |
| return TRUE; | |
| } | |
| function __toString() { | |
| return $this->render(array(), NULL, TRUE); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment