Skip to content

Commit

Permalink
Fixed codeclimate issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xujiajun committed Oct 19, 2017
1 parent 01efae0 commit 4987aa6
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 47 deletions.
38 changes: 23 additions & 15 deletions src/Framework/Cache/RedisCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public function __construct($redis)
* @return string|array|false
*/
public function get($cacheName)
{
{
$value = $this->redis->get($cacheName);

if ($value) $value = gzinflate($value);
if ($value) {
$value = gzinflate($value);
}

$jsonData =json_decode($value, true);
$jsonData = json_decode($value, true);
return ($jsonData === NULL) ? $value : $jsonData;
}

Expand All @@ -42,12 +44,13 @@ public function get($cacheName)
* @return mixed
*/
public function set($cacheName, $data, $expireTime = 3600)
{
$data = (is_object($data) || is_array($data)) ? json_encode($data) : $data;
if (strlen($data) > 4096){
{
$data = (is_object($data) || is_array($data)) ? json_encode($data) : $data;

$data = gzdeflate($data, 0);

if (strlen($data) > 4096) {
$data = gzdeflate($data, 6);
}else{
$data = gzdeflate($data, 0);
}

return $this->redis->set($cacheName, $data, $expireTime);
Expand All @@ -63,9 +66,11 @@ public function set($cacheName, $data, $expireTime = 3600)
public function hGet($hashKey, $key)
{
$value = $this->redis->hGet($hashKey, $key);
if ($value) $value = gzinflate($value);
if ($value) {
$value = gzinflate($value);
}

$jsonData = json_decode($value, true);
$jsonData = json_decode($value, true);
return ($jsonData === NULL) ? $value : $jsonData;
}

Expand All @@ -79,12 +84,13 @@ public function hGet($hashKey, $key)
* @return int
*/
public function hSet($hashKey, $key, $data, $expireTime = 3600)
{
{
$data = (is_object($data) || is_array($data)) ? json_encode($data) : $data;
if (strlen($data) > 4096){

$data = gzdeflate($data, 0);

if (strlen($data) > 4096) {
$data = gzdeflate($data, 6);
}else{
$data = gzdeflate($data, 0);
}

$status = $this->redis->hSet($hashKey, $key, $data);
Expand All @@ -103,7 +109,9 @@ public function hSet($hashKey, $key, $data, $expireTime = 3600)
*/
public function hDel($hashKey, $key = null)
{
if ($key) return $this->redis->hDel($hashKey, $key);
if ($key) {
return $this->redis->hDel($hashKey, $key);
}

return $this->redis->hDel($hashKey);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Framework/Cache/RedisServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function register()

if ($config['connect'] == 'persistence') {
$redis->pconnect($config['host'], $config['port']);
} else {
}

if(empty($config['connect']) || $config['connect'] != 'persistence') {
$redis->connect($config['host'], $config['port']);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Config/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function parse($serviceName, $default = [])
{
$config = [];
$serviceName = strtolower($serviceName);
$serviceConfigCacheFile = self::$configCacheDir . "/{$serviceName}.php";
$config = $this->getConfig($serviceConfigCacheFile, __BASEDIR__ . "/config/{$serviceName}.yml");
$cacheFile = self::$configCacheDir . "/{$serviceName}.php";
$config = $this->getConfig($cacheFile, __BASEDIR__ . "/config/{$serviceName}.yml");

if (!$config) {
if (isset($default)) {
Expand Down
7 changes: 3 additions & 4 deletions src/Framework/Event/ExceptionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,17 @@ protected function trace($error)

if (!empty($error['trace'])) {
$error['trace'] = str_replace("#", "<br/>", $error['trace']);
} else {
$error['trace'] = '';
}

if (is_array($error['message'])) {
$error['message'] = json_encode($error['message']);
}

$str = "<style>html, body {height: 100%;}body {margin: 0;padding: 0;width: 100%; display: table;font-weight: 100;font-family: 'Lato';}
.container {margin-top: 100px; vertical-align: middle;width: 1170px;margin-right: auto;margin-left: auto;}.content {text-align: left;display: inline-block;
}.title {font-size: 16px;}h3{color:#a94442;}p {color:#3c763d;}</style><div class=\"container\"><div class=\"content\" style=\"color:#8a6d3b\">
<h2>啊哦!出错了:</h2> </div> <br><div class=\"content\"><h3>错误文件名:</h3><p>{$error['file']}</p></div><br><div class=\"content\">
<h3>line:{$error['line']}</h3></div><br><div class=\"content\"><h3>错误信息:</h3> <p>{$error['message']}</p></div> <br><div class=\"content\"><h3>Trace:</h3><p>{$error['trace']}</p></div><br>";
<h2>something broke! :</h2> </div> <br><div class=\"content\"><h3>error file name:</h3><p>{$error['file']}</p></div><br><div class=\"content\">
<h3>line:{$error['line']}</h3></div><br><div class=\"content\"><h3>error info :</h3> <p>{$error['message']}</p></div> <br><div class=\"content\"><h3>Trace:</h3><p>{$error['trace']}</p></div><br>";

return $str;
}
Expand Down
12 changes: 10 additions & 2 deletions src/Framework/ExceptionHandler/ExceptionHandlerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?php
namespace TastPHP\Framework\ExceptionHandler;

use TastPHP\Framework\Handler\ExceptionsHandler;
use TastPHP\Framework\Service\ServiceProvider;
use TastPHP\Framework\Handler\WhoopsExceptionsHandler;

class ExceptionHandlerServiceProvider extends ServiceProvider
{
public function register()
{
$exceptionHandler = new WhoopsExceptionsHandler();
$exceptionHandler->register($this->app);
if (!$this->app['debug']) {
$exception = new ExceptionsHandler();
$exception->bootstrap();
}

if ($this->app['debug']){
$exceptionHandler = new WhoopsExceptionsHandler();
$exceptionHandler->register();
}
}
}
190 changes: 190 additions & 0 deletions src/Framework/Handler/ExceptionsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace TastPHP\Framework\Handler;

use TastPHP\Framework\Event\AppEvent;
use TastPHP\Framework\Event\ExceptionEvent;
use TastPHP\Framework\Event\KernalEvent;
use TastPHP\Framework\Event\MailEvent;
use TastPHP\Framework\Kernel;

class ExceptionsHandler
{
protected $container;

private $levels = array(
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice',
E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated',
E_ERROR => 'Error',
E_CORE_ERROR => 'Core Error',
E_COMPILE_ERROR => 'Compile Error',
E_PARSE => 'Parse',
0 => 'exception'
);

public function bootstrap()
{
$this->container = Kernel::getInstance();

error_reporting(E_ALL);

set_error_handler([$this, 'handleError']);

set_exception_handler([$this, 'handleException']);

register_shutdown_function([$this, 'handleShutdown']);

ini_set('display_errors', 'Off');
}

/**
* Convert a PHP error to an ErrorException.
*
* @param int $level
* @param string $message
* @param string $file
* @param int $line
* @return void
*/
public function handleError($level, $message, $file = '', $line = 0)
{
if (error_reporting() & $level) {
$error = [
'message' => $message,
'file' => $file,
'line' => $line,
'type' => $level,
];

switch ($level) {
case E_USER_ERROR:
$this->record($error);
if ($this->container->runningInConsole()) {
$this->renderForConsole($error);
} else {
$this->renderHttpResponse($error);
}
break;
default:
$this->record($error, 'warning');
break;
}
return true;
}

return false;
}

public function handleException($e)
{
$error = [
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
'type' => $e->getCode(),
];

if ($this->isFatal($error['type'])) {
$this->record($error);
}

if ($this->container->runningInConsole()) {
$this->renderForConsole($error);
}

if (!$this->container->runningInConsole()) {
$this->renderHttpResponse($error);
}
}

protected function renderForConsole($e)
{

}

/**
* Render an exception as an HTTP response and send it.
*
* @param \Exception $e
* @return void
*/
protected function renderHttpResponse($e)
{
$error = '';
$errorArray = [];

if ($this->container['env'] != 'prod') {
$error = $e;
if (!is_array($error)) {
$trace = debug_backtrace();
$errorArray['message'] = $e;
$errorArray['file'] = $trace[0]['file'];
$errorArray['line'] = $trace[0]['line'];
ob_start();
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$e['trace'] = ob_get_clean();
$error = $errorArray;
}

$this->container['eventDispatcher']->dispatch(AppEvent::EXCEPTION, new ExceptionEvent($error, $this->container));
}

if ($this->container['env'] == 'prod') {
$this->container['eventDispatcher']->dispatch(AppEvent::EXCEPTION, new ExceptionEvent($e, $this->container));
}
}

/**
* Handle the PHP shutdown event.
*
* @return void
*/
public function handleShutdown()
{
if ($e = error_get_last()) {
if ($this->isFatal($e['type'])) {
$this->record($e);
$e['trace'] = '';
if ($this->container->runningInConsole()) {
$this->renderForConsole($e);
}

if (!$this->container->runningInConsole()) {
$this->renderHttpResponse($e);
}
}
}
}

protected function record($e, $type = 'error', $context = array())
{
$env = $this->container['env'];
$node = $this->container['name'];
$body = 'App Env: 【' . $env . '】, node: 【' . $node . '】 <br> [ Exception ' . $e['message'] . '[' . $e['file'] . ' : ' . $e['line'] . ']';

if (($this->container['swift.mail.enabled'] == 'on') && (!$this->container['debug'])) {
$this->container['eventDispatcher']->dispatch(MailEvent::MAIlSEND, new MailEvent($body));
}
\Logger::$type('[' . $this->levels[$e['type']] . '] ' . $e['message'] . '[' . $e['file'] . ' : ' . $e['line'] . ']', $context);
}

/**
* Determine if the error type is fatal.
*
* @param int $type
* @return bool
*/
protected function isFatal($type)
{
return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
}

}
8 changes: 5 additions & 3 deletions src/Framework/Handler/WhoopsExceptionsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TastPHP\Framework\Handler;

use TastPHP\Framework\Container\Container;
use TastPHP\Framework\Kernel;
use Whoops\Handler\PlainTextHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Util\Misc;
Expand All @@ -12,8 +12,9 @@

class WhoopsExceptionsHandler
{
public function register(Container $container)
public function register()
{
$container = Kernel::getInstance();
if (isset($container['display_errors']) && !$container['display_errors']) {
ini_set('display_errors', 'Off');
}
Expand All @@ -22,7 +23,8 @@ public function register(Container $container)

if (Misc::isCommandLine()) {
$whoops->pushHandler(new PlainTextHandler());
} else {
}
if (!Misc::isCommandLine()) {
$myhander = new PrettyPageHandler();
$myhander->addDataTable('Tastphp Application', [
'Version' => $container['version'],
Expand Down
Loading

0 comments on commit 4987aa6

Please sign in to comment.