/** * Constructor * create a new instance of the database helper * @param string $tableName stores a table name * @return void */ public function __construct($tableName) { //connect to PDO here. $this->db = Database::getInstance(); $this->tableName = strtolower(Configurations::getConf("db")["tablePrefix"] . $tableName); $this->modelName = $tableName; }
/** * getInstance * * Static method get * * @static * @return instance of database */ public static function getInstance() { if (!isset(self::$instance)) { // database configuration information $type = Configurations::getConf("db")["type"]; $host = Configurations::getConf("db")["host"]; $name = Configurations::getConf("db")["name"]; $user = Configurations::getConf("db")["user"]; $pass = Configurations::getConf("db")["pass"]; $port = Configurations::getConf("db")["port"]; $charset = Configurations::getConf("db")["charset"]; //through an error and die try { $options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; self::$instance = new Database("{$type}:host={$host};port={$port};dbname={$name};charset={$charset}", $user, $pass, $options); } catch (PDOException $e) { die("Cannot connect to Database"); } } //return instance return self::$instance; }
/** * route * maps the url to controller and action * @param string $url * @return void */ public function route($url) { // seperate controller, action and parameters $urlSegments = explode("/", $url); if (!empty($urlSegments[0]) && strtolower($urlSegments[0]) != 'index') { $this->controller = ucfirst($urlSegments[0]); } if (!empty($urlSegments[1]) && strtolower($urlSegments[1]) != 'index') { $this->action = $urlSegments[1]; } $params = count($urlSegments) > 2 ? array_slice($urlSegments, 2) : array(); //todo: modules/components/plugins routes $filePath = Configurations::getConf("app")["basePath"] . "/" . $this->path . '/Controller/' . $this->controller . 'Controller.php'; if (is_readable($filePath)) { include $filePath; $class = $this->controller . "Controller"; $controller = new $class($this->controller, $this->action); //is_callable function checks if the class and method is present and we can call it. if (is_callable(array($controller, $this->action))) { $action = $this->action; //load its model class $modelClass = Configurations::getConf("app")["basePath"] . "/" . $this->path . '/Model/' . ucfirst($this->controller) . '.php'; if (is_readable($modelClass)) { include_once $modelClass; } } else { $controller = new Error(); $action = 'index'; $params['error'] = 'Method not found: <strong>' . $this->controller . "Controller/" . $this->action . '</strong>'; } } else { $controller = new Error(); $action = 'index'; $params['error'] = 'Invalid request : <strong>' . $this->controller . "Controller/" . $this->action . '</strong>'; } //invoke the controller $controller->{$action}($params); }
/** * renders the view - parses the HTML */ function render() { //extracts all the parameters extract($this->params); $basePath = Configurations::getConf("app")["basePath"]; $path = "templates/views/" . $this->controller . "/" . $this->action . ".php"; $content = ''; //overide global view file if a local view is present in app/templates if (file_exists($basePath . "app/" . $path)) { $content = $this->renderContent($basePath . "app/" . $path, $params); } else { if (file_exists($basePath . $path)) { $content = $this->renderContent($basePath . $path, $params); } else { $content = 'Error: View not found: ' . $path; } } //set the page title parameter if (!isset($params["page_title"])) { $params["page_title"] = Configurations::getConf("app")["name"]; } $path = "templates/layouts/" . $this->layout . ".php"; if (file_exists($basePath . "app/" . $path)) { include $basePath . "app/" . $path; } else { if (file_exists($basePath . $path)) { include $basePath . $path; } else { die('Error: Cannot find the Layout: ' . $basePath . $path); } } }
<?php /* * index.php - invoke the framework * * @author Abdul Wahid - awahid@gmail.com * @version 1.0.0 * @date June 15th, 2015 */ $loader = dirname(__FILE__) . '/../vendor/autoload.php'; if (file_exists($loader)) { require $loader; } else { echo "<p>Unable to start the framework </p><br/>"; echo "<p>Please run the command composer install from your working directory using your command promt</p>"; echo "<p>For more help about composer, please visit http://getcomposer.org</p>"; exit; } //load configurations use Mist\Helpers\Configurations; $confPath = dirname(__FILE__) . '/../config/config.php'; Configurations::init($confPath); //initialize session use Mist\Helpers\Session; Session::init(); //router use Mist\Core\Routing\Router; $router = new Router(); //define routes $url = isset($_GET['url']) ? $_GET['url'] : ''; $router->route($url);
/** * empties and destroys the session * * @return void */ public static function destroy($key = '') { if (self::$sessionStarted == true) { if (empty($key)) { session_unset(); session_destroy(); } else { unset($_SESSION[Configurations::getConf('session')['sessionPrefix'] . "_" . $key]); } } }