Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Lightbulb {
- private static $instance;
- private static $model;
- private static $controller;
- public $mobile;
- public $module_check;
- public $module_class;
- public $date = array();
- public $title;
- private function __construct(){
- include './system/config/constants.php';
- $this->check_mobile();
- }
- /*
- * This is called to load in all the assets
- * We call this function to avoid having duplicate
- * classes when calling the "get_instance" function
- *
- */
- public function loadAssets(){
- $config = $this->loadConfig();
- $this->routes();
- }
- /*
- * Checks for a default module to load.
- * If there is one, then see if that was the path
- * specified.
- *
- *
- */
- public function check_modules(){
- require CORE_PATH.'Module.php';
- $this->module_class = new Module();
- $module = $this->module_class;
- $check = $module->mod_scan();
- if($check == false){
- $this->module_check = false;
- return $this->module_check;
- }else if($check != false){
- /*
- * If there are modules, check to see if there is a default
- * specified to load.
- * This returns the name of the module that needs to be load
- */
- $db = $this->load_db();
- $module = $db->where('value','setting', "name='default module");
- //check to see if the path was what was specified.
- $page = $_GET['page'];
- if($page == $module){
- return $module;
- }
- }
- }
- /*
- * This is the singleton function that
- * allows the instance of Lightbulb to
- * be called anywhere in the framework.
- *
- */
- public static function get_instance(){
- if(!isset(self::$instance)){
- $c = __CLASS__;
- self::$instance = new $c;
- }
- return self::$instance;
- }
- /*
- * Loads core config file
- */
- public function loadConfig(){
- if(file_exists(CONFIG_PATH.'/config.php')){
- require_once CONFIG_PATH.'/config.php';
- }else{
- error_log("Couldn't find core config file!\n",3,"system/errors/framework-errors.log");
- }
- }
- /*
- * Takes care of routing.
- * - First finds routes file(should be in app/config/routes.php)
- * - if found, build out the other classes following routing rules,
- * if not, then build out the other classes like normal.
- */
- public function routes(){
- /*
- * checking to see if theres a routes file
- */
- if(file_exists(CONFIG_PATH.'/routes.php')){
- require_once CONFIG_PATH.'/routes.php';
- /*
- * If we're using routes
- */
- if($useRoutes == True){
- $this->construct(true);
- }else if($useRoutes == false){
- $this->construct(false);
- }
- }else{
- echo "fail";// require_once './site/system/errors/404.php';
- // error_log("Couldn't find core config file!\n",3,"./site/system/errors/framework-errors.log");
- }
- }
- /*
- * Loads and constructs all the classes,
- * functions, etc...
- *
- */
- public function construct($routes){
- /*
- * start the chief Controller and Moduel classes.
- */
- require_once 'Controller.php';
- $controller = new Controller();
- //load all the files from the controller folder
- $controllers = scandir(CONTROLLER_PATH,1);
- //load route file
- include CONFIG_PATH.'routes.php';
- //do we use routes?($useRoutes is defined in routes.php)
- if($useRoutes == false){
- if(isset($_GET['page'])){
- $page = $_GET['page'];
- $loader = $controller->load($page);
- }else{
- $page = "home";
- $loader = $controller->load($page);
- }
- }else if($useRoutes == true){
- //first we need to find out how many rules there are
- $ruleCount = count($routes);
- //load the route rules into a useable variable
- foreach($routes as $input => $output){
- /*
- * Switch statement to take care of all the possible routing cases.
- *
- * If we only have one rule, its only the rule specifying the
- * default controller.
- *
- * So we handle that interaction, as well as any other URL perameters
- * that might have been passed in.
- *
- * Otherwise, we need to then account for all the rules and route
- * accordingly
- * --------------------------------------------------------------------
- * URL format:
- * (root)/index.php?page=(page name)&method=(method name)&var=(variable name)
- *
- * URL is re-written to look like
- * (root)/(page name)/(method name)/(variable name)
- *
- * $_GET is still useable if necessary.
- *
- *
- */
- switch ($ruleCount){
- case ($ruleCount == 1):
- //first see if page variable is set
- if((!isset($_GET['page']))){
- //if not just load default controller
- $controller->load($routes['default']);
- }else{
- //otherwise start checking for the other variables too
- if((!isset($_GET['method']))&&(!isset($_GET['var']))){
- //if the method or var variables aren't set, then load the requested page
- $page = $_GET['page'];
- $controller->load($page);
- }else if((isset($_GET['method']))||(isset($_GET['var']))){
- //if they are set, load page and method, also do last check for var
- $page = $_GET['page'];
- $method = $_GET['method'];
- if(isset($_GET['var'])){
- $var = $_GET['var'];
- }
- //load everything
- $controller->load($page,$method,$var);
- }
- }
- break;
- case($ruleCount>1):
- $page = $_GET['page'];
- $method = $_GET['method'];
- $var = $_GET['var'];
- /*
- * If we have a match with a rule,
- * and there isn't a method, load the rule its replacing.
- */
- if(($page == $input)&&($method == "")){
- $controller->load($output);
- }else if($method != ""){
- //first check for a variable
- if($var != ""){
- $controller->load($output,$method,$var);
- }else{
- $controller->load($ouput,$method);
- }
- }else{
- error_log("Potential rule mismatch in routing\n",3,"./system/errors/framework-errors.log");
- }
- break;
- }
- }
- }
- }//end of function
- /*
- * Loads libraries
- */
- public function load_library($library){
- require LIBRARY_PATH.$library.'.php';
- $class = $this->startClass(new $library());
- return $class;
- }
- public function load_module($name){
- $path = "./system/app/modules/$name/$name.php";
- if(file_exists($path)){
- require $path;
- $class = $this->startClass(new $name());
- return $class;
- }else{
- echo "module doesn't exist";
- }
- }
- /*
- * Function helps to automatically create new instance of
- * libraries called by the "loadClass" function.
- */
- public function startClass($class){
- return $class;
- }
- /*
- * Load models
- */
- public function load_model($name){
- require 'Model.php';
- $object;
- $path = MODEL_PATH.$name.'.php';
- if((file_exists($path))&&(!class_exists($name))){
- require $path;
- $object = new $name();
- return $object;
- }else{
- error_log("Sorry, could not locate model $name");
- }
- }
- /*
- * Database interaction function
- * Again, MySQL is the assumed default.
- * Loads default values for host, username and password.
- *
- * For flexibility in selecting multiple db's,
- * it loads default database specified in Config, but if you pass in a name,
- * it'll load that database instead
- */
- public function load_db($dbName=null){
- global $database;
- include CONFIG_PATH.'config.php';
- $dbUser = $config['database']['dbUser'];
- $dbHost = $config['database']['dbHost'];
- $dbPass = $config['database']['dbPass'];
- //if there isn't a DB name name stated, run with the default name.
- if($dbName == ""){
- $db = $config['database']['dbName'];
- }else{
- $db = $dbName;
- }
- $database = new DataFetch($dbUser, $dbPass, $dbHost, $db);
- if(get_class ($database) != ""){
- return $database;
- echo "instance exists";
- }else{
- $database = new DataFetch($dbUser, $dbPass, $dbHost, $db);
- return $database;
- echo "needed to start a new instance";
- }
- }
- /*
- * Mobile device check:
- * gonna see if user is using a mobile device
- */
- public function check_mobile(){
- require './system/config/user-agents.php';
- $user_agent = $_SERVER['HTTP_USER_AGENT'];
- $count;
- for($i = 0;$i= 1){
- $this->mobile = "yes";
- }else{
- $this->mobile = "no";
- }
- }
- public function test(){
- echo "testing";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement