diff --git a/composer.json b/composer.json index 283ef0e..a2e7ed6 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "email": "ritsuka.sunny@gmail.com" }], "require": { - "php": ">=5.3.0", + "php": ">=5.4.0", "catfan/Medoo": "^1.0", "wyrihaximus/html-compress": "^1.2", "scrivo/highlight.php": "8.*" diff --git a/example/httpd.conf b/example/.htaccess similarity index 100% rename from example/httpd.conf rename to example/.htaccess diff --git a/example/nginx.conf b/example/nginx.conf index 0bcb2b1..d210271 100644 --- a/example/nginx.conf +++ b/example/nginx.conf @@ -2,7 +2,20 @@ location / { try_files $uri @kotori; - } +} + +location ~ /vendor { + return 403; +} + +location ~ /composer.(json|lock) { + return 403; +} + +location ~ /app { + return 403; +} + location @kotori { set $static 0; if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) { @@ -11,4 +24,4 @@ location @kotori { if ($static = 0) { rewrite ^/(.*)$ /index.php?_i=$1; } - } \ No newline at end of file +} \ No newline at end of file diff --git a/src/App.php b/src/App.php index 8e4fcfc..4e62c73 100644 --- a/src/App.php +++ b/src/App.php @@ -45,7 +45,7 @@ class App * * @var array */ - protected $_config = array(); + protected $_config = []; /** * Class constructor @@ -54,11 +54,10 @@ class App * * @return void */ - public function __construct($config = array()) + public function __construct($config = []) { - - if (version_compare(PHP_VERSION, '5.3.0', '<')) { - exit('Kotori.php requires PHP >= 5.3.0 !'); + if (version_compare(PHP_VERSION, '5.4.0', '<')) { + exit('Kotori.php requires PHP >= 5.4.0 !'); } ini_set('display_errors', 'off'); @@ -76,10 +75,10 @@ public function __construct($config = array()) */ public function run() { - //Define a custom error handler so we can log PHP errors - set_error_handler(array('\\Kotori\Core\Handle', 'error')); - set_exception_handler(array('\\Kotori\Core\Handle', 'exception')); - register_shutdown_function(array('\\Kotori\Core\Handle', 'end')); + // Define a custom error handler so we can log PHP errors + set_error_handler(['\\Kotori\Core\Handle', 'error']); + set_exception_handler(['\\Kotori\Core\Handle', 'exception']); + register_shutdown_function(['\\Kotori\Core\Handle', 'end']); Config::getSoul()->initialize($this->_config); @@ -89,15 +88,15 @@ public function run() !session_id() && session_start(); } - //Build + // Build new Build(Config::getSoul()->APP_FULL_PATH); - //Load application's common functions + // Load application's common functions Helper::import(Config::getSoul()->APP_FULL_PATH . '/common.php'); // @codingStandardsIgnoreStart if (function_exists('spl_autoload_register')) { - spl_autoload_register(array('\\Kotori\\Core\\Helper', 'autoload')); + spl_autoload_register(['\\Kotori\\Core\\Helper', 'autoload']); } else { function __autoload($className) { @@ -109,10 +108,10 @@ function __autoload($className) //Load route class Route::getSoul()->dispatch(); - //Global security filter - array_walk_recursive($_GET, array('\\Kotori\Http\Request', 'filter')); - array_walk_recursive($_POST, array('\\Kotori\Http\Request', 'filter')); - array_walk_recursive($_REQUEST, array('\\Kotori\Http\Request', 'filter')); + // Global security filter + array_walk_recursive($_GET, ['\\Kotori\Http\Request', 'filter']); + array_walk_recursive($_POST, ['\\Kotori\Http\Request', 'filter']); + array_walk_recursive($_REQUEST, ['\\Kotori\Http\Request', 'filter']); } } diff --git a/src/Core/Build.php b/src/Core/Build.php index 7116cb4..7f167ba 100644 --- a/src/Core/Build.php +++ b/src/Core/Build.php @@ -47,13 +47,13 @@ class Build * folders * @var array */ - protected $_folders = array( + protected $_folders = [ 'controllers', 'libraries', 'logs', 'models', 'views', - ); + ]; /** * Class constructor * diff --git a/src/Core/Cache.php b/src/Core/Cache.php index 6e5612c..edabb8e 100644 --- a/src/Core/Cache.php +++ b/src/Core/Cache.php @@ -34,48 +34,18 @@ use Kotori\Debug\Hook; use Kotori\Debug\Log; -class Cache +class Cache implements SoulInterface { + use SoulTrait; /** * Valid cache drivers * * @var array */ - protected $validDrivers = array( + protected $validDrivers = [ 'dummy', 'memcached', - ); - - /** - * Disable Clone - * - * @return boolean - */ - public function __clone() - { - return false; - } - - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } + ]; /** * Reference to the driver @@ -108,7 +78,7 @@ public function __construct() if (isset($config['PREFIX'])) { $this->keyPrefix = $config['PREFIX']; } - + $className = '\\Kotori\\Core\\Cache\\' . ucfirst($this->_adapter); $this->{$this->_adapter} = new $className(); diff --git a/src/Core/Cache/Memcached.php b/src/Core/Cache/Memcached.php index a353653..e8f192b 100644 --- a/src/Core/Cache/Memcached.php +++ b/src/Core/Cache/Memcached.php @@ -49,13 +49,13 @@ class Memcached * * @var array */ - protected $_memcacheConf = array( - 'default' => array( + protected $_memcacheConf = [ + 'default' => [ 'HOST' => '127.0.0.1', 'PORT' => 11211, 'WEIGHT' => 1, - ), - ); + ], + ]; /** * Class constructor @@ -72,7 +72,7 @@ public function __construct() $memcacheConf = isset($config['MEMCACHED']) ? $config['MEMCACHED'] : null; if (is_array($memcacheConf)) { - $this->_memcacheConf = array(); + $this->_memcacheConf = []; foreach ($memcacheConf as $name => $conf) { $this->_memcacheConf[$name] = $conf; @@ -145,7 +145,7 @@ public function get($id) public function set($id, $data, $ttl = 60, $raw = false) { if ($raw !== true) { - $data = array($data, time(), $ttl); + $data = [$data, time(), $ttl]; } if (get_class($this->_memcached) === 'Memcached') { @@ -228,11 +228,11 @@ public function getMetadata($id) list($data, $time, $ttl) = $stored; - return array( + return [ 'expire' => $time + $ttl, 'mtime' => $time, 'data' => $data, - ); + ]; } /** diff --git a/src/Core/Config.php b/src/Core/Config.php index fbba845..101f2a7 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -33,61 +33,32 @@ */ namespace Kotori\Core; +use Exception; use Kotori\Debug\Hook; use Kotori\Http\Request; -class Config +class Config implements SoulInterface { + use SoulTrait; /** * Config Array * * @var array */ - protected $_config = array(); + protected $_config = []; /** * Default Config Array * * @var array */ - protected $_defaults = array( + protected $_defaults = [ 'APP_DEBUG' => true, 'APP_PATH' => './app/', 'URL_MODE' => 'QUERY_STRING', 'TIME_ZONE' => 'Asia/Shanghai', 'USE_SESSION' => true, - ); - - /** - * Disable Clone - * - * @return boolean - */ - public function __clone() - { - return false; - } - - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } + ]; /** * Class constructor @@ -107,7 +78,7 @@ public function __construct() * @param $config Config Array * @return boolean */ - public function initialize($config = array()) + public function initialize($config = []) { $this->_config = $config; if (is_array($this->_config)) { @@ -130,13 +101,13 @@ public function initialize($config = array()) if (array_key_exists($hostName, $this->APP_PATH)) { $appPath = $this->APP_PATH[$hostName]; } else { - throw new \Exception('Cannot found any app paths.'); + throw new Exception('Cannot found any app paths.'); } } else { $appPath = $this->APP_PATH; } - $this->_config = array_merge(array('APP_FULL_PATH' => realpath(realpath('.') . '/' . rtrim($appPath, '/'))), $this->_config); + $this->_config = array_merge(['APP_FULL_PATH' => realpath(realpath('.') . '/' . rtrim($appPath, '/'))], $this->_config); $this->NAMESPACE_PREFIX = basename($this->APP_FULL_PATH) . '\\'; } } @@ -158,7 +129,7 @@ public function __set($key, $value) if (is_string($key)) { $this->_config[$key] = $value; } else { - throw new \Exception('Config Error.'); + throw new Exception('Config Error.'); } } diff --git a/src/Core/Controller.php b/src/Core/Controller.php index 7625d6c..fb64a8d 100644 --- a/src/Core/Controller.php +++ b/src/Core/Controller.php @@ -39,29 +39,9 @@ use Kotori\Http\Response; use Kotori\Http\Route; -class Controller +class Controller implements SoulInterface { - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } - + use SoulTrait; /** * DB selector * diff --git a/src/Core/Database.php b/src/Core/Database.php index f7bda1e..784f208 100644 --- a/src/Core/Database.php +++ b/src/Core/Database.php @@ -43,7 +43,7 @@ class Database extends Medoo * * @var array */ - public $queries = array(); + public $queries = []; /** * Instance Handle @@ -81,18 +81,9 @@ public static function getSoul($key = null) } Config::getSoul()->SELECTED_DB_KEY = $key; - $config = array( - 'database_type' => Config::getSoul()->DB[$key]['TYPE'], - 'database_name' => Config::getSoul()->DB[$key]['NAME'], - 'server' => Config::getSoul()->DB[$key]['HOST'], - 'username' => Config::getSoul()->DB[$key]['USER'], - 'password' => Config::getSoul()->DB[$key]['PWD'], - 'charset' => Config::getSoul()->DB[$key]['CHARSET'], - 'port' => Config::getSoul()->DB[$key]['PORT'], - ); if (!isset(self::$_soul[$key])) { - self::$_soul[$key] = new self($config); + self::$_soul[$key] = new self(Config::getSoul()->DB[$key]); } return self::$_soul[$key]; @@ -107,7 +98,15 @@ public static function getSoul($key = null) */ public function __construct($options = null) { - parent::__construct($options); + parent::__construct([ + 'database_type' => $options['TYPE'], + 'database_name' => $options['NAME'], + 'server' => $options['HOST'], + 'username' => $options['USER'], + 'password' => $options['PWD'], + 'charset' => $options['CHARSET'], + 'port' => $options['PORT'], + ]); Hook::listen(__CLASS__); } diff --git a/src/Core/Handle.php b/src/Core/Handle.php index c8d17a9..9e89bd1 100644 --- a/src/Core/Handle.php +++ b/src/Core/Handle.php @@ -44,7 +44,7 @@ abstract class Handle * * @var array */ - public static $errors = array(); + public static $errors = []; /** * General Error Page @@ -373,12 +373,12 @@ protected static function getSourceCode($file, $line) try { $contents = file($file); - $source = array( + $source = [ 'first' => $first, 'source' => array_slice($contents, $first - 1, 19), - ); + ]; } catch (\Exception $e) { - $source = array(); + $source = []; } return $source; diff --git a/src/Core/Helper.php b/src/Core/Helper.php index 753378e..d8e2759 100644 --- a/src/Core/Helper.php +++ b/src/Core/Helper.php @@ -33,6 +33,7 @@ */ namespace Kotori\Core; +use Exception; use Kotori\Debug\Hook; abstract class Helper @@ -41,7 +42,7 @@ abstract class Helper * Require Array * @var array */ - protected static $_require = array(); + protected static $_require = []; /** * Include One File @@ -127,7 +128,7 @@ public static function autoload($class) public static function mkdirs($pathname, $mode = 0755) { is_dir(dirname($pathname)) || self::mkdirs(dirname($pathname), $mode); - return is_dir($pathname) || @mkdir($pathname, $mode);// @codingStandardsIgnoreLine + return is_dir($pathname) || @mkdir($pathname, $mode); // @codingStandardsIgnoreLine } /** @@ -167,7 +168,7 @@ public static function getComposerVendorPath() } else { // Oh no! Can we default to something? // Or just bail out? - throw new \Exception('Oops, did you require this package via composer?'); + throw new Exception('Oops, did you require this package via composer?'); } } diff --git a/src/Core/Model.php b/src/Core/Model.php index 3eb5864..d98ab95 100644 --- a/src/Core/Model.php +++ b/src/Core/Model.php @@ -64,12 +64,12 @@ public function __get($key) * __call magic * * Allows model to access controller methods - * + * * @param $name * @param $arguments */ public function __call($name, $arguments) { - return call_user_func_array(array(Controller::getSoul(), $name), $arguments); + return call_user_func_array([Controller::getSoul(), $name], $arguments); } } diff --git a/src/Core/Model/Provider.php b/src/Core/Model/Provider.php index a3378f5..9fbf085 100644 --- a/src/Core/Model/Provider.php +++ b/src/Core/Model/Provider.php @@ -31,11 +31,15 @@ */ namespace Kotori\Core\Model; +use Exception; use Kotori\Core\Config; +use Kotori\Core\SoulInterface; +use Kotori\Core\SoulTrait; use Kotori\Debug\Hook; -class Provider +class Provider implements SoulInterface { + use SoulTrait; /** * Initialized Models * @@ -43,37 +47,6 @@ class Provider */ protected $_models = array(); - /** - * Disable Clone - * - * @return boolean - */ - public function __clone() - { - return false; - } - - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } - /** * Class constructor * @@ -102,7 +75,7 @@ public function __get($key) $modelClassName = Config::getSoul()->NAMESPACE_PREFIX . 'models\\' . $key; if (!class_exists($modelClassName)) { - throw new \Exception('Request Model ' . $key . ' is not Found'); + throw new Exception('Request Model ' . $key . ' is not Found'); } else { $model = new $modelClassName(); $this->_models[$key] = $model; diff --git a/src/Core/SoulInterface.php b/src/Core/SoulInterface.php new file mode 100644 index 0000000..e4910e4 --- /dev/null +++ b/src/Core/SoulInterface.php @@ -0,0 +1,43 @@ +_viewPath = $this->_tplDir . $tpl . '.html'; if (!Helper::isFile($this->_viewPath)) { - throw new \Exception('Template is not existed.'); + throw new Exception('Template is not existed.'); } unset($tpl); @@ -161,12 +162,12 @@ public function display($tpl = '') * @param array $data Data Array * @return void */ - public function need($path, $data = array()) + public function need($path, $data = []) { - $this->_needData = array( + $this->_needData = [ 'path' => Config::getSoul()->APP_FULL_PATH . '/views/' . $path . '.html', 'data' => $data, - ); + ]; unset($path); unset($data); extract($this->_needData['data']); diff --git a/src/Debug/Hook.php b/src/Debug/Hook.php index df7c7aa..fd922ea 100644 --- a/src/Debug/Hook.php +++ b/src/Debug/Hook.php @@ -38,7 +38,7 @@ abstract class Hook * * @var array */ - protected static $tags = array(); + protected static $tags = []; /** * get the tags diff --git a/src/Debug/Trace.php b/src/Debug/Trace.php index 75862a6..a635b04 100644 --- a/src/Debug/Trace.php +++ b/src/Debug/Trace.php @@ -35,16 +35,19 @@ use Kotori\Core\Database; use Kotori\Core\Handle; use Kotori\Core\Helper; +use Kotori\Core\SoulInterface; +use Kotori\Core\SoulTrait; use WyriHaximus\HtmlCompress\Factory as htmlParserFactory; -class Trace +class Trace implements SoulInterface { + use SoulTrait; /** * traceTab * * @var array */ - protected $traceTabs = array( + protected $traceTabs = [ 'BASE' => 'Basic', 'CONFIG' => 'Config', 'SERVER' => 'Server', @@ -54,38 +57,7 @@ class Trace 'ERROR' => 'Error', 'SQL' => 'SQL', 'SUPPORT' => 'Support', - ); - - /** - * Disable Clone - * - * @return boolean - */ - public function __clone() - { - return false; - } - - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } + ]; /** * Class constructor @@ -110,7 +82,7 @@ protected function getTrace() $config = Config::getSoul()->getArray(); $server = $_SERVER; $cookie = $_COOKIE; - $info = array(); + $info = []; foreach ($files as $key => $file) { $info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )'; } @@ -123,9 +95,9 @@ protected function getTrace() $error = Handle::$errors; $database = Database::getSoul(Config::getSoul()->SELECTED_DB_KEY); - $sql = $database == null ? array() : $database->queries; + $sql = $database == null ? [] : $database->queries; - $base = array( + $base = [ 'Request Info' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['PHP_SELF'], 'Run Time' => Hook::listen('\\Kotori\\App') . 'μs', 'TPR' => Hook::listen('\\Kotori\\App') != 0 ? pow(10, 6) / Hook::listen('\\Kotori\\App') . ' req/s' : '+inf', @@ -133,14 +105,14 @@ protected function getTrace() 'SQL Queries' => count($sql) . ' queries ', 'File Loaded' => count(get_included_files()), 'Session Info' => 'SESSION_ID=' . session_id(), - ); + ]; - $support = array( + $support = [ 'GitHub', 'Blog', - ); + ]; - $trace = array(); + $trace = []; foreach ($this->traceTabs as $name => $title) { switch (strtoupper($name)) { case 'BASE': @@ -205,7 +177,7 @@ public function showTrace() if (is_array($info)) { foreach ($info as $k => $val) { $val = is_array($val) ? print_r($val, true) : (is_bool($val) ? json_encode($val) : $val); - $val = (in_array($key, array('Support'))) ? $val : htmlentities($val, ENT_COMPAT, 'utf-8'); + $val = (in_array($key, ['Support'])) ? $val : htmlentities($val, ENT_COMPAT, 'utf-8'); $tpl .= '
  • ' . (is_numeric($k) ? '' : $k . ' : ') . $val . '
  • '; } } diff --git a/src/Http/Request.php b/src/Http/Request.php index aa5f37e..6a514f9 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -31,10 +31,13 @@ */ namespace Kotori\Http; +use Kotori\Core\SoulInterface; +use Kotori\Core\SoulTrait; use Kotori\Debug\Hook; -class Request +class Request implements SoulInterface { + use SoulTrait; /** * Params * @@ -49,37 +52,6 @@ class Request */ protected $_ip = null; - /** - * Disable Clone - * - * @return boolean - */ - public function __clone() - { - return false; - } - - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } - /** * Class constructor * @@ -146,7 +118,7 @@ public function input($name, $default = '', $filter = null, $datas = null) } break; case 'path': - $input = array(); + $input = []; if (!empty($_SERVER['PATH_INFO'])) { $depr = '/'; $input = explode($depr, trim($_SERVER['PATH_INFO'], $depr)); @@ -197,7 +169,7 @@ public function input($name, $default = '', $filter = null, $datas = null) $filters = explode(',', $filters); } } elseif (is_int($filters)) { - $filters = array($filters); + $filters = [$filters]; } if (is_array($filters)) { @@ -238,7 +210,7 @@ public function input($name, $default = '', $filter = null, $datas = null) $data = isset($default) ? $default : null; } - is_array($data) && array_walk_recursive($data, array('Request', 'filter')); + is_array($data) && array_walk_recursive($data, ['Request', 'filter']); return $data; } @@ -251,7 +223,7 @@ public function input($name, $default = '', $filter = null, $datas = null) */ protected function array_map_recursive($filter, $data) { - $result = array(); + $result = []; foreach ($data as $key => $val) { $result[$key] = is_array($val) ? $this->array_map_recursive($filter, $val) @@ -346,7 +318,7 @@ public function getClientIp($type = 0) // Check ip $long = sprintf("%u", ip2long($this->_ip)); - $this->_ip = $long ? array($this->_ip, $long) : array('0.0.0.0', 0); + $this->_ip = $long ? [$this->_ip, $long] : ['0.0.0.0', 0]; return $this->_ip[$type]; } @@ -357,13 +329,13 @@ public function getClientIp($type = 0) */ public function getHostName() { - $possibleHostSources = array('HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR'); - $sourceTransformations = array( + $possibleHostSources = ['HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR']; + $sourceTransformations = [ "HTTP_X_FORWARDED_HOST" => function ($value) { $elements = explode(',', $value); return trim(end($elements)); }, - ); + ]; $host = ''; foreach ($possibleHostSources as $source) { if (!empty($host)) { diff --git a/src/Http/Response.php b/src/Http/Response.php index 58ec6bb..71179b0 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -31,16 +31,20 @@ */ namespace Kotori\Http; +use Exception; +use Kotori\Core\SoulInterface; +use Kotori\Core\SoulTrait; use Kotori\Debug\Hook; -class Response +class Response implements SoulInterface { + use SoulTrait; /** * Status array * * @var array */ - protected $_httpCode = array( + protected $_httpCode = [ 100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', @@ -81,7 +85,7 @@ class Response 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', - ); + ]; /** * Default Charset @@ -90,37 +94,6 @@ class Response */ protected $_charset = null; - /** - * Disable Clone - * - * @return boolean - */ - public function __clone() - { - return false; - } - - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } - /** * Class constructor * @@ -164,7 +137,7 @@ public function setCharset($charset = null) public function setStatus($code = 200, $text = '') { if (empty($code) or !is_numeric($code)) { - throw new \Exception('Status codes must be numeric.'); + throw new Exception('Status codes must be numeric.'); } if (empty($text)) { @@ -175,7 +148,7 @@ public function setStatus($code = 200, $text = '') if (isset($this->_httpCode[$code])) { $text = $this->_httpCode[$code]; } else { - throw new \Exception('No status text available. Please check your status code number or supply your own message text.'); + throw new Exception('No status text available. Please check your status code number or supply your own message text.'); } } diff --git a/src/Http/Route.php b/src/Http/Route.php index 4c06507..1f552a9 100644 --- a/src/Http/Route.php +++ b/src/Http/Route.php @@ -33,18 +33,22 @@ */ namespace Kotori\Http; +use Exception; use Kotori\Core\Config; use Kotori\Core\Helper; +use Kotori\Core\SoulInterface; +use Kotori\Core\SoulTrait; use Kotori\Debug\Hook; -class Route +class Route implements SoulInterface { + use SoulTrait; /** * Controllers Array * * @var array */ - protected $_controllers = array(); + protected $_controllers = []; /** * Current controller @@ -72,45 +76,14 @@ class Route * * @var array */ - protected $_uris = array(); + protected $_uris = []; /** * Parsed params * * @var array */ - protected $_params = array(); - - /** - * Disable Clone - * - * @return boolean - */ - public function __clone() - { - return false; - } - - /** - * Instance Handle - * - * @var array - */ - protected static $_soul; - - /** - * get singleton - * - * @return object - */ - public static function getSoul() - { - if (self::$_soul === null) { - self::$_soul = new self(); - } - - return self::$_soul; - } + protected $_params = []; /** * Class constructor @@ -175,12 +148,12 @@ public function dispatch() if ($parsedRoute) { $this->_uri = $parsedRoute; } else { - throw new \Exception('Request URI ' . $this->_uri . ' is not Matched by any route.'); + throw new Exception('Request URI ' . $this->_uri . ' is not Matched by any route.'); } - $this->_uris = ($this->_uri != '') ? explode('/', trim($this->_uri, '/')) : array(); + $this->_uris = ($this->_uri != '') ? explode('/', trim($this->_uri, '/')) : []; - //Clean uris + // Clean uris foreach ($this->_uris as $key => $value) { if ($value == '') { unset($this->_uris[$key]); @@ -191,12 +164,12 @@ public function dispatch() $this->_controller = $this->getController(); $this->_action = $this->getAction(); - //Define some variables + // Define some variables define('CONTROLLER_NAME', $this->_controller); define('ACTION_NAME', $this->_action); define('PUBLIC_DIR', Request::getSoul()->getBaseUrl() . 'public'); - //If is already initialized + // If is already initialized $prefix = Config::getSoul()->NAMESPACE_PREFIX; $controllerClassName = $prefix . 'controllers\\' . $this->_controller; @@ -208,25 +181,25 @@ public function dispatch() } if (!class_exists($controllerClassName)) { - throw new \Exception('Request Controller ' . $this->_controller . ' is not Found.'); + throw new Exception('Request Controller ' . $this->_controller . ' is not Found.'); } if (!method_exists($class, $this->_action)) { - throw new \Exception('Request Action ' . $this->_action . ' is not Found.'); + throw new Exception('Request Action ' . $this->_action . ' is not Found.'); } - //Parse params from uri + // Parse params from uri $this->_params = $this->getParams(); - //Do some final cleaning of the params + // Do some final cleaning of the params $_GET = array_merge($this->_params, $_GET); $_REQUEST = array_merge($_POST, $_GET, $_COOKIE); Response::getSoul()->setHeader('X-Powered-By', 'Kotori'); Response::getSoul()->setHeader('Cache-control', 'private'); - //Call the requested method - call_user_func_array(array($class, $this->_action), $this->_params); + // Call the requested method + call_user_func_array([$class, $this->_action], $this->_params); } /** @@ -240,7 +213,7 @@ protected function getController() if (isset($this->_uris[0]) && '' !== $this->_uris[0]) { $_controller = $this->_uris[0]; } else { - throw new \Exception('Cannot dispatch controller name.'); + throw new Exception('Cannot dispatch controller name.'); } return strip_tags($_controller); @@ -257,7 +230,7 @@ protected function getAction() if (isset($this->_uris[1])) { $_action = $this->_uris[1]; } else { - throw new \Exception('Cannot dispatch action name.'); + throw new Exception('Cannot dispatch action name.'); } return strip_tags($_action); @@ -378,7 +351,7 @@ public function url($uri = '', $module = null) case 'QUERY_STRING': return $uri == '' ? rtrim($baseUrl, '/') : $prefix . $uri; default: - throw new \Exception('URL_MODE Config ERROR'); + throw new Exception('URL_MODE Config ERROR'); } } diff --git a/tests/AppTest.php b/tests/AppTest.php index 52f8714..9b0b189 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -10,13 +10,17 @@ class AppTest extends PHPUnit_Framework_TestCase { - const MYSQL_HOST = '127.0.0.1'; - const MYSQL_USER = 'root'; - const MYSQL_PWD = ''; - const MYSQL_DB = 'kotori_php_test_db'; + protected static $MYSQL_HOST = '127.0.0.1'; + protected static $MYSQL_USER = 'root'; + protected static $MYSQL_PWD = '123456'; + protected static $MYSQL_DB = 'kotori_php_test_db'; public function __construct() { + if (getenv('CI')) { + self::$MYSQL_PWD = ''; + } + $this->createTestDatabase(); } @@ -52,11 +56,11 @@ public function testGetConfigArray() protected function createTestDatabase() { try { - $pdo = new PDO('mysql:host=' . self::MYSQL_HOST, self::MYSQL_USER, self::MYSQL_PWD); + $pdo = new PDO('mysql:host=' . self::$MYSQL_HOST, self::$MYSQL_USER, self::$MYSQL_PWD); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $pdo->exec('DROP DATABASE IF EXISTS `' . self::MYSQL_DB . '`; -CREATE DATABASE `' . self::MYSQL_DB . '`; -USE `' . self::MYSQL_DB . '`; + $pdo->exec('DROP DATABASE IF EXISTS `' . self::$MYSQL_DB . '`; +CREATE DATABASE `' . self::$MYSQL_DB . '`; +USE `' . self::$MYSQL_DB . '`; DROP TABLE IF EXISTS `table`; CREATE TABLE `table` ( @@ -77,10 +81,10 @@ protected function getDatabaseInstance() 'DB' => [ 'db' => [ 'TYPE' => 'mysql', - 'NAME' => self::MYSQL_DB, - 'HOST' => self::MYSQL_HOST, - 'USER' => self::MYSQL_USER, - 'PWD' => self::MYSQL_PWD, + 'NAME' => self::$MYSQL_DB, + 'HOST' => self::$MYSQL_HOST, + 'USER' => self::$MYSQL_USER, + 'PWD' => self::$MYSQL_PWD, ], ], ]); diff --git a/tests/README.md b/tests/README.md index 7691ac8..3bda28d 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,6 +1,6 @@ # Kotori.php Unit Tests -Follow the directions below to run the Kotori.php unit tests. You'll need the latest version of PHPUnit. To save development time, these unit tests require PHP >= 5.3. +Follow the directions below to run the Kotori.php unit tests. You'll need the latest version of PHPUnit. To save development time, these unit tests require PHP >= 5.4. 1. Install the latest version of PHPUnit Visit http://www.phpunit.de/ for installation instructions. @@ -9,4 +9,4 @@ Visit http://www.phpunit.de/ for installation instructions. From the filesystem directory that contains the `tests` directory, you may run all unit tests or specific unit tests. Here are several examples. The '$>' in the examples below is your command prompt. To run all tests: -$> phpunit tests \ No newline at end of file +$> composer test \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d5c05b5..9546bb2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,9 +1,4 @@