Skip to content

Commit

Permalink
Remove common codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Jun 29, 2017
1 parent 24b6fcc commit e961acd
Show file tree
Hide file tree
Showing 25 changed files with 279 additions and 373 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
Expand Down
File renamed without changes.
17 changes: 15 additions & 2 deletions example/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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)$) {
Expand All @@ -11,4 +24,4 @@ location @kotori {
if ($static = 0) {
rewrite ^/(.*)$ /index.php?_i=$1;
}
}
}
31 changes: 15 additions & 16 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class App
*
* @var array
*/
protected $_config = array();
protected $_config = [];

/**
* Class constructor
Expand All @@ -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');
Expand All @@ -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);

Expand All @@ -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)
{
Expand All @@ -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']);
}

}
4 changes: 2 additions & 2 deletions src/Core/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class Build
* folders
* @var array
*/
protected $_folders = array(
protected $_folders = [
'controllers',
'libraries',
'logs',
'models',
'views',
);
];
/**
* Class constructor
*
Expand Down
40 changes: 5 additions & 35 deletions src/Core/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
16 changes: 8 additions & 8 deletions src/Core/Cache/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -228,11 +228,11 @@ public function getMetadata($id)

list($data, $time, $ttl) = $stored;

return array(
return [
'expire' => $time + $ttl,
'mtime' => $time,
'data' => $data,
);
];
}

/**
Expand Down
49 changes: 10 additions & 39 deletions src/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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) . '\\';
}
}
Expand All @@ -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.');
}
}

Expand Down
24 changes: 2 additions & 22 deletions src/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Loading

0 comments on commit e961acd

Please sign in to comment.