Created
December 7, 2015 16:07
-
-
Save DailyMatters/9533e2005cb12e2bc6e8 to your computer and use it in GitHub Desktop.
SundayMVC Bitches!
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) 2015 | |
| Cláudio Ribeiro <claudio.santos.ribeiro@gmail.com> | |
| Permission is hereby granted, free of charge, to any person | |
| obtaining a copy of this software and associated documentation | |
| files (the "Software"), to deal in the Software without | |
| restriction, including without limitation the rights to use, | |
| copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the | |
| Software is furnished to do so, subject to the following | |
| conditions: | |
| The above copyright notice and this permission notice shall be | |
| included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
| OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
| OTHER DEALINGS IN THE SOFTWARE. | |
| *****************************************************************/ | |
| //=============================================================== | |
| // Controller | |
| // Parses the HTTP request and routes to the appropriate function | |
| //=============================================================== | |
| //ATTENTION: this router is not Exception handled yet | |
| class SimpleController{ | |
| public function route( $r, callable $callback ){ | |
| $this->r[$r] = $callback; | |
| } | |
| public function execute(){ | |
| $server = $_SERVER; | |
| $path = 'PATH_INFO'; | |
| $p = isset( $server[$path] ) ? $server[$path] : '/'; | |
| if( isset($this->r[$p]) ){ | |
| $this->r[$p](); | |
| }else{ | |
| header("HTTP/1.0 404 Not Found"); | |
| die('<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p><p>Please go <a href="javascript: history.back(1)">back</a> and try again.</p><hr /></body></html>'); | |
| } | |
| } | |
| } | |
| // =============================================================== | |
| // View | |
| // For html templates stored on the /templates folder | |
| // =============================================================== | |
| class SimpleView { | |
| protected $template_dir = 'templates/'; | |
| protected $vars = array(); | |
| public function __construct($template_dir = null) { | |
| if ($template_dir !== null) { | |
| // Check here whether this directory really exists | |
| $this->template_dir = $template_dir; | |
| } | |
| } | |
| public function render($template_file) { | |
| if (file_exists($this->template_dir.$template_file)) { | |
| include $this->template_dir.$template_file; | |
| } else { | |
| throw new Exception('no template file ' . $template_file . ' present in directory ' . $this->template_dir); | |
| } | |
| } | |
| public function __set($name, $value) { | |
| $this->vars[$name] = $value; | |
| } | |
| public function __get($name) { | |
| return $this->vars[$name]; | |
| } | |
| } | |
| //=============================================================== | |
| // Model/ORM | |
| // Database Handling | |
| //=============================================================== | |
| class SimpleDatabase extends PDO { | |
| // Define configuration | |
| /*define("DB_HOST", "localhost"); | |
| define("DB_USER", "DBUSER"); | |
| define("DB_PASS", "DBPWD"); | |
| define("DB_NAME", "DBNAME");*/ | |
| private $host = DB_HOST; | |
| private $user = DB_USER; | |
| private $pass = DB_PASS; | |
| private $dbname = DB_NAME; | |
| private $dbh; | |
| private $error; | |
| private $stmt; | |
| public function __construct(){ | |
| // Set DSN | |
| $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; | |
| // Set options | |
| $options = array( | |
| PDO::ATTR_PERSISTENT => true, | |
| PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION | |
| ); | |
| // Create a new PDO instanace | |
| try{ | |
| $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); | |
| } | |
| // Catch any errors | |
| catch(PDOException $e){ | |
| $this->error = $e->getMessage(); | |
| } | |
| } | |
| public function __destuct(){ | |
| // Adding null connection | |
| $this->dbh = null; | |
| $this->isConnected = false; | |
| } | |
| // The query, prepare, bind and execute methods allows us to use prepared statements in our DB interactions | |
| // Prepare | |
| public function query($query){ | |
| $this->stmt = $this->dbh->prepare($query); | |
| } | |
| // Bind | |
| public function bind($param, $value, $type = null){ | |
| if (is_null($type)) { | |
| switch (true) { | |
| case is_int($value): | |
| $type = PDO::PARAM_INT; | |
| break; | |
| case is_bool($value): | |
| $type = PDO::PARAM_BOOL; | |
| break; | |
| case is_null($value): | |
| $type = PDO::PARAM_NULL; | |
| break; | |
| default: | |
| $type = PDO::PARAM_STR; | |
| } | |
| } | |
| $this->stmt->bindValue($param, $value, $type); | |
| } | |
| // Execute | |
| public function execute(){ | |
| return $this->stmt->execute(); | |
| } | |
| // Methods to fetch query results | |
| // Result Set | |
| public function resultset(){ | |
| $this->execute(); | |
| return $this->stmt->fetchAll(PDO::FETCH_ASSOC); | |
| } | |
| // Single | |
| public function single(){ | |
| $this->execute(); | |
| return $this->stmt->fetch(PDO::FETCH_ASSOC); | |
| } | |
| // Row Count | |
| public function rowCount(){ | |
| return $this->stmt->rowCount(); | |
| } | |
| // Last Insert Id | |
| public function lastInsertId(){ | |
| return $this->dbh->lastInsertId(); | |
| } | |
| // Handling transactions | |
| // Transactions | |
| public function beginTransaction(){ | |
| return $this->dbh->beginTransaction(); | |
| } | |
| public function endTransaction(){ | |
| return $this->dbh->commit(); | |
| } | |
| public function cancelTransaction(){ | |
| return $this->dbh->rollBack(); | |
| } | |
| // Debug Dump Parameters | |
| public function debugDumpParams(){ | |
| return $this->stmt->debugDumpParams(); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment