Skip to content

Commit

Permalink
Version 0.3.0
Browse files Browse the repository at this point in the history
- Added Profiler to Debugger
- Routes now are defined by a config file stored in the directory
/routes
- Added the Service class
  • Loading branch information
Fariz Luqman committed Jan 20, 2017
1 parent 35d2aeb commit cb5ed16
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 32 deletions.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
Empty file modified src/Alias.php
100644 → 100755
Empty file.
Empty file modified src/App.php
100644 → 100755
Empty file.
11 changes: 0 additions & 11 deletions src/Cache.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Empty file modified src/Config.php
100644 → 100755
Empty file.
Empty file modified src/Database.php
100644 → 100755
Empty file.
59 changes: 56 additions & 3 deletions src/Debugger.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down Expand Up @@ -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);
Expand All @@ -185,4 +189,53 @@ static function microtime_diff($start)
static function exec_time(){
echo ('<span style="display: table; margin: 0 auto;">Request takes '.(self::microtime_diff(DSS_START) * 1000 ) . ' milliseconds</span>');
}

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,
];
}
}
Empty file modified src/Response.php
100644 → 100755
Empty file.
47 changes: 29 additions & 18 deletions src/Router.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,19 @@
* @author Studio Nexus <[email protected]>
* @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();
Expand All @@ -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
*/
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -145,7 +154,9 @@ public static function dispatch(){
call_user_func_array(self::$callbacks[$pos], $matched);
if (self::$halts) return;
}
}
}else{

}
}
$pos++;
}
Expand All @@ -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();
}
}
}
58 changes: 58 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Core;

/**
* Class Service
* Service is where all the applications written by user resides, for both
* input and output processing
* @package Core
*/
class Service {
/**
* Contains the object of instantiation of the Service class
* @static
* @var object
*/
private static $serviceObject;

/**
* @var array
*/
private $config = '';

/**
* @static
* @return Service|object
*/
public static function loadServices()
{
if(isset(self::$serviceObject) === false){
self::$serviceObject = new self;
}

self::$serviceObject->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);
}

}
Empty file modified src/Sharer.php
100644 → 100755
Empty file.
Empty file modified src/Singleton.php
100644 → 100755
Empty file.
Empty file modified src/Viewer.php
100644 → 100755
Empty file.
Empty file modified src/errorpage/full.php
100644 → 100755
Empty file.
Empty file modified src/errorpage/simple.php
100644 → 100755
Empty file.
Empty file modified src/errorpage/style.css
100644 → 100755
Empty file.
Empty file modified tests/AppTest.php
100644 → 100755
Empty file.
Empty file modified tests/bootstrap.php
100644 → 100755
Empty file.

0 comments on commit cb5ed16

Please sign in to comment.