Skip to content

Commit

Permalink
code clear
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanLuLyf committed Mar 14, 2020
1 parent 3dc4a44 commit aa9daf5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 44 deletions.
4 changes: 2 additions & 2 deletions BunnyPHP/BunnyPHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private function route()
if (!empty($appName)) {
self::set('app', $appName);
$appConf = $this->apps[$appName];
$prefix = isset($appConf['namespace']) ? $appConf['namespace'] : '';
$prefix = $appConf['namespace'] ?? '';
if (isset($appConf['path'])) define('SUB_APP_PATH', $appConf['path']);
}
if (!empty($prefix)) {
Expand Down Expand Up @@ -238,7 +238,7 @@ private function processPathParam($decorate, &$pathParam, &$pathValue)

public function get($key)
{
return isset($this->variable[$key]) ? $this->variable[$key] : null;
return $this->variable[$key] ?? null;
}

public function set($key, $value)
Expand Down
2 changes: 1 addition & 1 deletion BunnyPHP/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function get($key, $defaultVal = '')
}
return $tmp;
} else {
return isset($this->configs[$key]) ? $this->configs[$key] : $defaultVal;
return $this->configs[$key] ?? $defaultVal;
}
}

Expand Down
2 changes: 1 addition & 1 deletion BunnyPHP/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FileCache implements Cache

public function __construct($config)
{
$this->dir = isset($config['dir']) ? $config['dir'] : 'cache';
$this->dir = $config['dir'] ?? 'cache';
$this->cacheDir = APP_PATH . $this->dir . '/';
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0777, true);
Expand Down
8 changes: 4 additions & 4 deletions BunnyPHP/FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FileLogger implements Logger

public function __construct($config)
{
$this->filename = isset($config['filename']) ? $config['filename'] : APP_PATH . 'log.log';
$this->filename = $config['filename'] ?? (APP_PATH . 'log.log');
}

private function makeMessage($message, array $context = [], $type = '')
Expand All @@ -23,7 +23,7 @@ private function makeMessage($message, array $context = [], $type = '')
foreach ($context as $k => $v) {
$replace['{' . $k . '}'] = $v;
}
return date("[Y-m-d H:i] ", time()) . '[' . $type . '] ' . strtr($message, $context) . "\n";
return date('[Y-m-d H:i] ', time()) . '[' . $type . '] ' . strtr($message, $context) . "\n";
}

public function info($message, array $context = [])
Expand All @@ -38,13 +38,13 @@ public function error($message, array $context = [])

public function warn($message, array $context = [])
{
error_log($this->makeMessage($message, $context, "WARN"), 3, $this->filename);
error_log($this->makeMessage($message, $context, 'WARN'), 3, $this->filename);
}

public function debug($message, array $context = [])
{
if (APP_DEBUG === true) {
error_log($this->makeMessage($message, $context, "DEBUG"), 3, $this->filename);
error_log($this->makeMessage($message, $context, 'DEBUG'), 3, $this->filename);
}
}
}
4 changes: 2 additions & 2 deletions BunnyPHP/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FileStorage implements Storage

public function __construct($config)
{
$this->dir = isset($config['dir']) ? $config['dir'] : 'upload';
$this->dir = $config['dir'] ?? 'upload';
$this->uploadPath = APP_PATH . $this->dir . '/';
if (!is_dir($this->uploadPath)) {
mkdir($this->uploadPath, 0777, true);
Expand Down Expand Up @@ -43,7 +43,7 @@ public function upload($filename, $path)
mkdir($this->uploadPath . $dir, 0777, true);
}
move_uploaded_file($path, $this->uploadPath . $filename);
return "/$this->dir/" . $filename;
return "/{$this->dir}/$filename";
}

public function remove($filename)
Expand Down
8 changes: 2 additions & 6 deletions BunnyPHP/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function loadLanguage($lang, $basePath)

public function translate($key)
{
return isset($this->translation[$key]) ? $this->translation[$key] : '';
return $this->translation[$key] ?? '';
}

public static function getInstance($lang = null, $basePath = null)
Expand Down Expand Up @@ -89,11 +89,7 @@ public function offsetExists($offset)

public function offsetGet($offset)
{
if (isset($this->translation[$offset])) {
return $this->translation[$offset];
} else {
return $offset;
}
return $this->translation[$offset] ?? $offset;
}

public function offsetSet($offset, $value)
Expand Down
12 changes: 6 additions & 6 deletions BunnyPHP/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static function create($debug = false)
{
$table = self::name();
$vars = get_class_vars(get_called_class());
$pk = isset($vars['_pk']) ? $vars['_pk'] : [];
$ai = isset($vars['_ai']) ? $vars['_ai'] : '';
$uk = isset($vars['_uk']) ? $vars['_uk'] : [];
$pk = $vars['_pk'] ?? [];
$ai = $vars['_ai'] ?? '';
$uk = $vars['_uk'] ?? [];
return BunnyPHP::getDatabase()->createTable($table, $vars['_column'], $pk, $ai, $uk, $debug);
}

Expand Down Expand Up @@ -88,11 +88,11 @@ public function where($where, $param = [])

public function join($tableName, $condition = [], $select = [], $mod = 'left')
{
if (substr($tableName, -5) == "Model") {
if (substr($tableName, -5) == 'Model') {
$tableName = $tableName::name();
}
if (count($select) == 0) {
$this->_column[] = $tableName . ".*";
$this->_column[] = $tableName . '.*';
} else {
foreach ($select as $item) {
if (is_array($item)) {
Expand All @@ -113,7 +113,7 @@ public function join($tableName, $condition = [], $select = [], $mod = 'left')
$conditionArr[] = "{$tableName}.{$k}={$v}";
}
}
$this->_join .= " on (" . implode(' and ', $conditionArr) . ")";
$this->_join .= ' on (' . implode(' and ', $conditionArr) . ')';
return $this;
}

Expand Down
16 changes: 8 additions & 8 deletions BunnyPHP/PdoDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ public function __construct($conf = [])
$dsn = $conf['dsn'];
$this->db_type = explode(':', $dsn)[0];
} else {
$db_type = isset($conf['type']) ? strtolower($conf['type']) : 'mysql';
if (!isset($conf['host'])) $conf['host'] = 'host';
$db_type = strtolower($conf['type'] ?? 'mysql');
$host = $conf['host'] ?? 'localhost';
if ($db_type == 'mysql') {
if (!isset($conf['port'])) $conf['port'] = 3306;
$dsn = "mysql:host=${conf['host']};port=${conf['port']};dbname=${conf['database']};charset=utf8mb4";
$port = $conf['port'] ?? 3306;
$dsn = "mysql:host=$host;port=$port;dbname=${conf['database']};charset=utf8mb4";
} elseif ($db_type == 'sqlite') {
$dsn = "sqlite:${conf['database']}";
} elseif ($db_type == 'pgsql') {
if (!isset($conf['port'])) $conf['port'] = 5432;
$dsn = "pgsql:host=${conf['host']};port=${conf['port']};dbname=${conf['database']}";
$port = $conf['port'] ?? 5432;
$dsn = "pgsql:host=$host;port=$port;dbname=${conf['database']}";
}
$this->db_type = $db_type;
}
$username = isset($conf['username']) ? $conf['username'] : '';
$password = isset($conf['password']) ? $conf['password'] : '';
$username = $conf['username'] ?? '';
$password = $conf['password'] ?? '';
if (!empty($dsn)) {
$option = [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_EMULATE_PREPARES => false];
$this->conn = new PDO($dsn, $username, $password, $option);
Expand Down
2 changes: 1 addition & 1 deletion BunnyPHP/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Request implements \ArrayAccess

public function getHeader($name)
{
$header_name = "HTTP_" . strtoupper($name);
$header_name = 'HTTP_' . strtoupper($name);
if (isset($_SERVER[$header_name])) {
return $_SERVER[$header_name];
}
Expand Down
14 changes: 1 addition & 13 deletions BunnyPHP/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,7 @@ public static function info($context = [], $mode = BunnyPHP::MODE_NORMAL, $code

private static function get_message($context)
{
if (isset($context['bunny_error'])) {
return $context['bunny_error'];
} elseif (isset($context['bunny_info'])) {
return $context['bunny_info'];
} elseif (isset($context['response'])) {
return $context['response'];
} elseif (isset($context['tp_error_msg'])) {
return $context['tp_error_msg'];
} elseif (isset($context['tp_info_msg'])) {
return $context['tp_info_msg'];
} else {
return '';
}
return $context['bunny_error'] ?? $context['bunny_info'] ?? $context['response'] ?? $context['tp_error_msg'] ?? $context['tp_info_msg'] ?? '';
}

public static function get_url($mod, $action, $params = [])
Expand Down

0 comments on commit aa9daf5

Please sign in to comment.