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 .= '