From cb5ed16cd541799455398e7265ef5908f633c85e Mon Sep 17 00:00:00 2001 From: Fariz Luqman Date: Sat, 21 Jan 2017 00:55:31 +0800 Subject: [PATCH] Version 0.3.0 - Added Profiler to Debugger - Routes now are defined by a config file stored in the directory /routes - Added the Service class --- README.md | 0 composer.json | 0 src/Alias.php | 0 src/App.php | 0 src/Cache.php | 11 -------- src/Config.php | 0 src/Database.php | 0 src/Debugger.php | 59 ++++++++++++++++++++++++++++++++++++++-- src/Response.php | 0 src/Router.php | 47 ++++++++++++++++++++------------ src/Service.php | 58 +++++++++++++++++++++++++++++++++++++++ src/Sharer.php | 0 src/Singleton.php | 0 src/Viewer.php | 0 src/errorpage/full.php | 0 src/errorpage/simple.php | 0 src/errorpage/style.css | 0 tests/AppTest.php | 0 tests/bootstrap.php | 0 19 files changed, 143 insertions(+), 32 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 composer.json mode change 100644 => 100755 src/Alias.php mode change 100644 => 100755 src/App.php mode change 100644 => 100755 src/Cache.php mode change 100644 => 100755 src/Config.php mode change 100644 => 100755 src/Database.php mode change 100644 => 100755 src/Debugger.php mode change 100644 => 100755 src/Response.php mode change 100644 => 100755 src/Router.php create mode 100644 src/Service.php mode change 100644 => 100755 src/Sharer.php mode change 100644 => 100755 src/Singleton.php mode change 100644 => 100755 src/Viewer.php mode change 100644 => 100755 src/errorpage/full.php mode change 100644 => 100755 src/errorpage/simple.php mode change 100644 => 100755 src/errorpage/style.css mode change 100644 => 100755 tests/AppTest.php mode change 100644 => 100755 tests/bootstrap.php diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 diff --git a/src/Alias.php b/src/Alias.php old mode 100644 new mode 100755 diff --git a/src/App.php b/src/App.php old mode 100644 new mode 100755 diff --git a/src/Cache.php b/src/Cache.php old mode 100644 new mode 100755 index f349a7b..bb45498 --- a/src/Cache.php +++ b/src/Cache.php @@ -87,15 +87,4 @@ static function init(){ } } - /** - * Clear all cached data - * - * @static - * @access public - * @since Method available since Release 0.1.0 - */ - static function clean(){ - CacheManager::clean(); - } - } \ No newline at end of file diff --git a/src/Config.php b/src/Config.php old mode 100644 new mode 100755 diff --git a/src/Database.php b/src/Database.php old mode 100644 new mode 100755 diff --git a/src/Debugger.php b/src/Debugger.php old mode 100644 new mode 100755 index 7cf6a97..612857c --- a/src/Debugger.php +++ b/src/Debugger.php @@ -40,9 +40,13 @@ * */ class Debugger { - /** + private static $profiles = []; + private static $time_start = 0; + private static $profilerStartTime = 0; + + /** * Registering the debugger to log exceptions locally or transfer them to - * external services + * external services. * * Depends on the settings in config/env.php: * @@ -163,7 +167,7 @@ static function display($name, $message = ''){ * @access public * @since Method available since Release 0.1.0 */ - static function microtime_diff($start) + static private function microtime_diff($start) { $duration = microtime(true) - $start; $hours = (int)($duration/60/60); @@ -185,4 +189,53 @@ static function microtime_diff($start) static function exec_time(){ echo ('Request takes '.(self::microtime_diff(DSS_START) * 1000 ) . ' milliseconds'); } + + static function startProfiling(){ + if(self::$profilerStartTime == 0){ + self::$profilerStartTime = microtime(true); + } + + self::$time_start = microtime(true); + } + + static function addProfilingData($point_name = '', $point_type = 'others'){ + $profileData = + [ + 'name' => $point_name, + 'time' => ( self::microtime_diff(self::$time_start) * 1000 ), + 'unit' => 'ms', + 'type' => $point_type + ]; + + array_push(self::$profiles, $profileData); + + self::$time_start = microtime(true); + + return $profileData; + } + + static function endProfiling(){ + $timeIncludingAutoloader = self::microtime_diff(DSS_START) * 1000; + $timeProfiled = self::microtime_diff(self::$profilerStartTime) * 1000; + $timeMinusAutoloader = $timeIncludingAutoloader - $timeProfiled; + + $profileData = + [ + 'name' => 'Starting Autoloader', + 'time' => ($timeMinusAutoloader), + 'unit' => 'ms', + 'type' => 'system' + ]; + + array_unshift(self::$profiles, $profileData); + self::$time_start = 0; + self::$profilerStartTime = 0; + + return + [ + 'Total Time' => ( $timeIncludingAutoloader ), + 'unit' => 'ms', + 'profiles' => self::$profiles, + ]; + } } \ No newline at end of file diff --git a/src/Response.php b/src/Response.php old mode 100644 new mode 100755 diff --git a/src/Router.php b/src/Router.php old mode 100644 new mode 100755 index e2400f9..7171e13 --- a/src/Router.php +++ b/src/Router.php @@ -26,24 +26,19 @@ * @author Studio Nexus * @copyright 2016 Studio Nexus * @license MIT - * @version Release: 0.2.0 + * @version Release: 0.3.0 * @link https://www.studionexus.co/php/damnstupidsimple */ namespace Core; -/** - * Routing System - * ----------------------------------------------------------------------- - * - * @method static Macaw get(string $route, Callable $callback) - * @method static Macaw post(string $route, Callable $callback) - * @method static Macaw put(string $route, Callable $callback) - * @method static Macaw delete(string $route, Callable $callback) - * @method static Macaw options(string $route, Callable $callback) - * @method static Macaw head(string $route, Callable $callback) - * - */ class Router { + /** + * The configurations + * @var array + * @access private + * @static + */ + static private $config = null; public static $halts = false; public static $routes = array(); public static $methods = array(); @@ -64,6 +59,20 @@ public static function __callstatic($method, $params) { array_push(self::$methods, strtoupper($method)); array_push(self::$callbacks, $callback); } + + /** + * Load the configuration file + */ + public static function start(){ + if(self::$config === null){ + self::$config = Config::get('routes'); + } + + foreach(self::$config['routes'] as $route){ + include(self::$config['path'] . $route . '.php'); + } + } + /** * Defines callback if route is not found */ @@ -83,7 +92,7 @@ public static function dispatch(){ $searches = array_keys(static::$patterns); $replaces = array_values(static::$patterns); $found_route = false; - + self::$routes = str_replace('//', '/', self::$routes); // Check if route is defined without regex if (in_array($uri, self::$routes)) { @@ -136,7 +145,7 @@ public static function dispatch(){ // Fix multi parameters if(!method_exists($controller, $segments[1])) { //"controller and action not found" - Debugger::report(500); + Debugger::report(500); } else { call_user_func_array(array($controller, $segments[1]), $matched); } @@ -145,7 +154,9 @@ public static function dispatch(){ call_user_func_array(self::$callbacks[$pos], $matched); if (self::$halts) return; } - } + }else{ + + } } $pos++; } @@ -167,11 +178,11 @@ public static function dispatch(){ call_user_func(self::$error_callback); } } - + static function redirect($url, $permanent = false){ if (headers_sent() === false){ header('Location: ' . $url, true, ($permanent === true) ? 301 : 302); } exit(); } -} \ No newline at end of file +} diff --git a/src/Service.php b/src/Service.php new file mode 100644 index 0000000..ed31111 --- /dev/null +++ b/src/Service.php @@ -0,0 +1,58 @@ +prepare(); + + return self::$serviceObject; + } + + /** + * Object preparer + */ + private function prepare(){ + if(isset($config) === false){ + $this->config = Config::get('services'); + } + + foreach($this->config as $className => $varName){ + $this->$varName = new $className; + } + } + + /** + * Dump function + */ + public function dump(){ + return($this); + } + +} \ No newline at end of file diff --git a/src/Sharer.php b/src/Sharer.php old mode 100644 new mode 100755 diff --git a/src/Singleton.php b/src/Singleton.php old mode 100644 new mode 100755 diff --git a/src/Viewer.php b/src/Viewer.php old mode 100644 new mode 100755 diff --git a/src/errorpage/full.php b/src/errorpage/full.php old mode 100644 new mode 100755 diff --git a/src/errorpage/simple.php b/src/errorpage/simple.php old mode 100644 new mode 100755 diff --git a/src/errorpage/style.css b/src/errorpage/style.css old mode 100644 new mode 100755 diff --git a/tests/AppTest.php b/tests/AppTest.php old mode 100644 new mode 100755 diff --git a/tests/bootstrap.php b/tests/bootstrap.php old mode 100644 new mode 100755