Example #1
0
 /**
  * Runs the callback for the given request
  */
 public static function dispatch()
 {
     $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
     $method = $_SERVER['REQUEST_METHOD'];
     $searches = array_keys(static::$patterns);
     $replaces = array_values(static::$patterns);
     self::$routes = str_replace('//', '/', self::$routes);
     $found_route = false;
     // parse query parameters
     $query = '';
     $q_arr = array();
     if (strpos($uri, '&') > 0) {
         $query = substr($uri, strpos($uri, '&') + 1);
         $uri = substr($uri, 0, strpos($uri, '&'));
         $q_arr = explode('&', $query);
         foreach ($q_arr as $q) {
             $qobj = explode('=', $q);
             $q_arr[] = array($qobj[0] => $qobj[1]);
             if (!isset($_GET[$qobj[0]])) {
                 $_GET[$qobj[0]] = $qobj[1];
             }
         }
     }
     // check if route is defined without regex
     if (in_array($uri, self::$routes)) {
         $route_pos = array_keys(self::$routes, $uri);
         // foreach route position
         foreach ($route_pos as $route) {
             if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') {
                 $found_route = true;
                 //if route is not an object
                 if (!is_object(self::$callbacks[$route])) {
                     //call object controller and method
                     self::invokeObject(self::$callbacks[$route]);
                     if (self::$halts) {
                         return;
                     }
                 } else {
                     //call closure
                     call_user_func(self::$callbacks[$route]);
                     if (self::$halts) {
                         return;
                     }
                 }
             }
         }
         // end foreach
     } else {
         // check if defined with regex
         $pos = 0;
         // foreach routes
         foreach (self::$routes as $route) {
             $route = str_replace('//', '/', $route);
             if (strpos($route, ':') !== false) {
                 $route = str_replace($searches, $replaces, $route);
             }
             if (preg_match('#^' . $route . '$#', $uri, $matched)) {
                 if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY') {
                     $found_route = true;
                     //remove $matched[0] as [1] is the first parameter.
                     array_shift($matched);
                     if (!is_object(self::$callbacks[$pos])) {
                         //call object controller and method
                         self::invokeObject(self::$callbacks[$pos], $matched);
                         if (self::$halts) {
                             return;
                         }
                     } else {
                         //call closure
                         call_user_func_array(self::$callbacks[$pos], $matched);
                         if (self::$halts) {
                             return;
                         }
                     }
                 }
             }
             $pos++;
         }
         // end foreach
     }
     if (self::$fallback) {
         //call the auto dispatch method
         $found_route = self::autoDispatch();
     }
     // run the error callback if the route was not found
     if (!$found_route) {
         //first check if we have a satisfiable static route.
         if (count(self::$static_routes) > 0) {
             $test_sr = str_replace(Config::SITE_PATH() . '/', '', $uri);
             foreach (self::$static_routes as $sr) {
                 if (file_exists($sr . $test_sr)) {
                     $ext = \HMC\Document\Document::getExtension($test_sr);
                     switch ($ext) {
                         case 'html':
                         case 'htm':
                             require $sr . $test_sr;
                             break;
                         default:
                             header("Location: {$sr}{$test_sr}");
                     }
                     die;
                 } else {
                     //for security you cannot access php files directly so
                     //if there's no extension we test for a php file with that name.
                     if (file_exists($sr . $test_sr . '.php')) {
                         require $sr . $test_sr . '.php';
                         die;
                     }
                 }
             }
         }
         if (!self::$errorCallback) {
             self::$errorCallback = function () {
                 Error::showError(404);
             };
         }
         if (!is_object(self::$errorCallback)) {
             //call object controller and method
             self::invokeObject(self::$errorCallback, null, 'No routes found.');
             if (self::$halts) {
                 return;
             }
         } else {
             call_user_func(self::$errorCallback);
             if (self::$halts) {
                 return;
             }
         }
     }
 }
Example #2
0
 /**
  * Output optimized stylesheet.
  * This function will combine multiple stylesheets into a single sheet.
  * It will also perform some basic optimizations to reduce download size.
  * @param $files - array of files to include
  * @param $outputdir - where to store generated file(s), default is typically adequate.
  */
 public static function combine_css($files, $outputdir = 'static/generated/')
 {
     if (\HMC\Config::SITE_ENVIRONMENT() == 'development') {
         Assets::css($files);
         return;
     }
     $ofiles = is_array($files) ? $files : array($files);
     $hashFileName = md5(join($ofiles));
     $dirty = false;
     if (file_exists($outputdir . $hashFileName . '.css')) {
         $hfntime = filemtime($outputdir . $hashFileName . '.css');
         foreach ($ofiles as $vfile) {
             $file = str_replace(\HMC\Config::SITE_URL(), \HMC\Config::SITE_PATH(), $vfile);
             if (!$dirty) {
                 $fmtime = filemtime($file);
                 if ($fmtime > $hfntime) {
                     $dirty = true;
                 }
             }
         }
     } else {
         $dirty = true;
     }
     if ($dirty) {
         $buffer = "";
         foreach ($ofiles as $vfile) {
             $cssFile = str_replace(\HMC\Config::SITE_URL(), \HMC\Config::SITE_PATH(), $vfile);
             $buffer .= "\n" . file_get_contents($cssFile);
         }
         // Remove comments
         $buffer = preg_replace('!/\\*[^*]*\\*+([^/][^*]*\\*+)*/!', '', $buffer);
         // Remove space after colons
         $buffer = str_replace(': ', ':', $buffer);
         // Remove whitespace
         $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
         ob_start();
         // Write everything out
         echo $buffer;
         $fc = ob_get_clean();
         file_put_contents(\HMC\Config::SITE_PATH() . $outputdir . $hashFileName . '.css', $fc);
     }
     static::resource(str_replace(':||', '://', str_replace('//', '/', str_replace('://', ':||', \HMC\Config::SITE_URL() . $outputdir . $hashFileName . '.css'))), 'css');
 }