Skip to content

Commit

Permalink
fixed some error
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymframework committed Apr 28, 2019
1 parent 7c894e9 commit cb50395
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 60 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

[![packagist-badge](https://img.shields.io/packagist/v/swooletw/laravel-swoole.svg)](https://packagist.org/packages/swooletw/laravel-swoole)
[![Total Downloads](https://poser.pugx.org/swooletw/laravel-swoole/downloads)](https://packagist.org/packages/swooletw/laravel-swoole)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/swooletw/laravel-swoole/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/swooletw/laravel-swoole/?branch=master)
[![travis-badge](https://api.travis-ci.org/swooletw/laravel-swoole.svg?branch=master)](https://travis-ci.org/swooletw/laravel-swoole)

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/learning-student/owuan-swoole/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/learning-student/owuan-swoole/?branch=master)[![Build Status](https://travis-ci.com/learning-student/owuan-swoole.svg?branch=master)](https://travis-ci.com/learning-student/owuan-swoole)
This package provides a high performance HTTP server to speed up your Laravel/Lumen application based on [Swoole](http://www.swoole.com/).

## Version Compatibility
Expand Down
32 changes: 0 additions & 32 deletions copy_versioned_files.php

This file was deleted.

2 changes: 1 addition & 1 deletion routes/websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// called while socket on connect
});

Websocket::on('disconnect', function ($websocket) {
Websocket::on('disconnect', function () {
// called while socket on disconnect
});

Expand Down
22 changes: 16 additions & 6 deletions src/Cache/Caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
namespace SwooleTW\Http\Cache;

use Anonym\Components\Cache\Cache;
use Anonym\Components\Cache\CacheInterface;
use Anonym\Components\Cache\DriverAdapterInterface;
use Anonym\Components\Cache\DriverNotInstalledException;
use function foo\func;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
Expand Down Expand Up @@ -121,27 +117,41 @@ private function checkCacheExists($request): bool
/**
* @param $request
* @return Response
* @throws \RuntimeException
*/
private function getCachedResponse($request): Response
{
$uri = $request->server['request_uri'];

return $this->caching->get($uri);
$response = $this->caching->get($uri);


if (!$response instanceof Response) {
throw new \RuntimeException('Cache removed');
}

return $response;
}


/**
* @return \Closure
*/
public function cachePreEvent(): callable
{
return function ($request) {


if (!$this->checkCacheExists($request)) {
return false;
}

return $this->getCachedResponse($request);
try {
return $this->getCachedResponse($request);

} catch (\RuntimeException $exception) {
return false;
}

};
}
Expand Down
4 changes: 1 addition & 3 deletions src/Cache/SwooleLumenRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public function addRoute($method, $uri, $action)

// save only GET routes
if ($method === 'GET' && is_array($action) && isset($action['cache'])) {

$caching = $this->app->make(Caching::class)->cacheRoute($uri);

$this->app->make(Caching::class)->cacheRoute($uri);
}
}
}
38 changes: 23 additions & 15 deletions src/Commands/HttpServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ class HttpServerCommand extends Command
*/
public function handle()
{
$this->checkEnvironment();
if (!$this->checkEnvironment()) {
return;
}


$this->loadConfigs();
$this->initAction();
$this->runAction();
Expand Down Expand Up @@ -135,7 +139,7 @@ protected function stop()
{
$pid = $this->getCurrentPid();

if (! $this->isRunning($pid)) {
if (!$this->isRunning($pid)) {
$this->error("Failed! There is no swoole_http_server process running.");

return;
Expand Down Expand Up @@ -179,7 +183,7 @@ protected function reload()
{
$pid = $this->getCurrentPid();

if (! $this->isRunning($pid)) {
if (!$this->isRunning($pid)) {
$this->error("Failed! There is no swoole_http_server process running.");

return;
Expand All @@ -189,7 +193,7 @@ protected function reload()

$isRunning = $this->killProcess($pid, SIGUSR1);

if (! $isRunning) {
if (!$isRunning) {
$this->error('> failure');

return;
Expand Down Expand Up @@ -246,7 +250,7 @@ protected function initAction()
{
$this->action = $this->argument('action');

if (! in_array($this->action, ['start', 'stop', 'restart', 'reload', 'infos'], true)) {
if (!in_array($this->action, ['start', 'stop', 'restart', 'reload', 'infos'], true)) {
$this->error(
"Invalid argument '{$this->action}'. Expected 'start', 'stop', 'restart', 'reload' or 'infos'."
);
Expand All @@ -256,12 +260,13 @@ protected function initAction()
}

/**
* @param \SwooleTW\Http\Server\Facades\Server $server
* @param Server $server
*
* @return \Swoole\Process
*/
protected function getHotReloadProcess($server)
{

$recursively = Arr::get($this->config, 'hot_reload.recursively');
$directory = Arr::get($this->config, 'hot_reload.directory');
$filter = Arr::get($this->config, 'hot_reload.filter');
Expand All @@ -284,7 +289,7 @@ protected function getHotReloadProcess($server)
*/
protected function isRunning($pid)
{
if (! $pid) {
if (!$pid) {
return false;
}

Expand Down Expand Up @@ -312,7 +317,7 @@ protected function killProcess($pid, $sig, $wait = 0)
$start = time();

do {
if (! $this->isRunning($pid)) {
if (!$this->isRunning($pid)) {
break;
}

Expand All @@ -337,7 +342,7 @@ protected function getCurrentPid()
$path = $this->getPidPath();

return $this->currentPid = file_exists($path)
? (int) file_get_contents($path) ?? $this->removePidFile()
? (int)file_get_contents($path) ?? $this->removePidFile()
: null;
}

Expand Down Expand Up @@ -372,25 +377,28 @@ protected function isDaemon(): bool
/**
* Check running enironment.
*/
protected function checkEnvironment()
protected function checkEnvironment(): bool
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$this->error("Swoole extension doesn't support Windows OS yet.");

exit(1);
return false;
}

if (! extension_loaded('swoole')) {
if (!extension_loaded('swoole')) {
$this->error("Can't detect Swoole extension installed.");

exit(1);
return false;
}

if (! version_compare(swoole_version(), '4.0.0', 'ge')) {
if (!version_compare(swoole_version(), '4.0.0', 'ge')) {
$this->error("Your Swoole version must be higher than 4.0 to use coroutine.");

exit(1);
return false;
}

return true;

}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Server/Facades/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Class Server
*
* @mixin \Swoole\Http\Server
* @method void reload()
*/
class Server extends Facade
{
Expand Down
9 changes: 9 additions & 0 deletions src/Server/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
use SwooleTW\Http\Event\EventBase;
use App\Events\Event;

/**
* if swoole_version not provided,
*/
if (!function_exists('swoole_version')) {
function swoole_version(): string {
return '0';
}
}

/**
* This is only for `function not exists` in config/swoole_http.php.
*/
Expand Down

0 comments on commit cb50395

Please sign in to comment.