Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More code refactoring.
Fix for OS Helper.
Added Hot Reload with FSWatch.
  • Loading branch information
fractalzombie committed Dec 31, 2018
commit aed922f141487cfe9dc084f098d81e5fe7fc1ac6
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"predis/predis": "^1.1",
"ext-json": "*",
"ext-fileinfo": "*",
"ext-pdo": "*"
"ext-pdo": "*",
"ext-pcntl": "*"
},
"require-dev": {
"laravel/lumen-framework": "~5.3",
Expand Down
8 changes: 4 additions & 4 deletions config/swoole_http.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
|--------------------------------------------------------------------------
*/
'hot_reload' => [
'enabled' => env('SWOOLE_HOT_RELOAD_ENABLE', true),
'level' => env('SWOOLE_HOT_RELOAD_DEEP_LEVEL', 10),
'directory' => env('SWOOLE_HOT_RELOAD_DIRECTORY', base_path()),
'enabled' => env('SWOOLE_HOT_RELOAD_ENABLE', false),
'recursively' => env('SWOOLE_HOT_RELOAD_RECURSIVELY', true),
'directory' => env('SWOOLE_HOT_RELOAD_DIRECTORY', base_path('app')),
'log' => env('SWOOLE_HOT_RELOAD_LOG', true),
'files' => ['*.php'],
'filter' => env('SWOOLE_HOT_RELOAD_FILTER', '.php'),
],

/*
Expand Down
58 changes: 45 additions & 13 deletions src/Commands/HttpServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Swoole\Process;
use SwooleTW\Http\Helpers\Alias;
use SwooleTW\Http\HotReload\FSEvent;
use SwooleTW\Http\HotReload\FSProcess;
use SwooleTW\Http\Server\Facades\Server;
use SwooleTW\Http\Server\Manager;
use Throwable;

Expand Down Expand Up @@ -67,7 +71,7 @@ public function handle()
*/
protected function loadConfigs()
{
$this->config = $this->laravel->make('config')->get('swoole_http');
$this->config = $this->laravel->make(Alias::CONFIG)->get('swoole_http');
}

/**
Expand All @@ -91,6 +95,7 @@ protected function start()

$host = Arr::get($this->config, 'server.host');
$port = Arr::get($this->config, 'server.port');
$hotReloadEnabled = Arr::get($this->config, 'hot_reload.enabled');

$this->info('Starting swoole http server...');
$this->info("Swoole http server started: <http://{$host}:{$port}>");
Expand All @@ -101,7 +106,14 @@ protected function start()
);
}

$this->laravel->make(Manager::class)->run();
$manager = $this->laravel->make(Manager::class);
$server = $this->laravel->make(Server::class);

if ($hotReloadEnabled) {
$manager->addProcess($this->getHotReloadProcess($server));
}

$manager->run();
}

/**
Expand Down Expand Up @@ -231,6 +243,26 @@ protected function initAction()
}
}

/**
* @param \SwooleTW\Http\Server\Facades\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');
$log = Arr::get($this->config, 'hot_reload.log');

$cb = function (FSEvent $event) use ($server, $log) {
$log ? $this->info($event->getPath()) : null;
$server->reload();
};

return (new FSProcess($filter, $recursively, $directory))->make($cb);
}

/**
* If Swoole process is running.
*
Expand Down Expand Up @@ -333,19 +365,19 @@ protected function checkEnvironment()
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$this->error("Swoole extension doesn't support Windows OS yet.");

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

return;
} else {
if (! version_compare(swoole_version(), '4.0.0', 'ge')) {
$this->error("Your Swoole version must be higher than 4.0 to use coroutine.");
if (! extension_loaded('swoole')) {
$this->error("Can't detect Swoole extension installed.");

return;
}
}
exit(1);
}

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);
}
}
}
3 changes: 2 additions & 1 deletion src/Concerns/ResetApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SwooleTW\Http\Concerns;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Container\Container;
use SwooleTW\Http\Exceptions\SandboxException;
use SwooleTW\Http\Server\Resetters\ResetterContract;
Expand All @@ -28,7 +29,7 @@ trait ResetApplication
*/
protected function setInitialConfig()
{
$this->config = clone $this->getBaseApp()->make('config');
$this->config = clone $this->getBaseApp()->make(Repository::class);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Coroutine/Connectors/MySqlConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SwooleTW\Http\Coroutine\Connectors;


use Illuminate\Database\Connectors\MySqlConnector as BaseConnector;
use Illuminate\Support\Str;
use SwooleTW\Http\Coroutine\PDO as SwoolePDO;
Expand Down
3 changes: 2 additions & 1 deletion src/Coroutine/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ public function fetch(
/**
* Returns a single column from the next row of a result set or FALSE if there are no more rows.
*
* @param int $column_number
* @param int|null $columnNumber
*
* 0-indexed number of the column you wish to retrieve from the row.
* If no value is supplied, PDOStatement::fetchColumn() fetches the first column.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers/OS.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class OS
*
* @const string
*/
public const LINUX = 'linux';
public const LINUX = 'lin';

/**
* Linux
Expand All @@ -39,7 +39,7 @@ final class OS
*/
public static function is(string ...$types): bool
{
return \in_array(static::current(), $types, true);
return Str::contains(static::current(), $types);
}

/**
Expand All @@ -49,6 +49,6 @@ public static function is(string ...$types): bool
*/
public static function current(): string
{
return Str::lower(PHP_OS);
return Str::substr(Str::lower(PHP_OS), 0, 3);
}
}
138 changes: 138 additions & 0 deletions src/HotReload/FSEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace SwooleTW\Http\HotReload;

use Illuminate\Support\Carbon;

/**
* Class FSEvent
*/
class FSEvent
{
/**
* Event - Created
*
* @var string
*/
public const Created = 'Created';

/**
* Event - Updated
*
* @var string
*/
public const Updated = 'Updated';

/**
* Event - Removed
*
* @var string
*/
public const Removed = 'Removed';

/**
* Event - Renamed
*
* @var string
*/
public const Renamed = 'Renamed';

/**
* Event - OwnerModified
*
* @var string
*/
public const OwnerModified = 'OwnerModified';

/**
* Event - AttributeModified
*
* @var string
*/
public const AttributeModified = 'AttributeModified';

/**
* Event - MovedFrom
*
* @var string
*/
public const MovedFrom = 'MovedFrom';

/**
* Event - MovedTo
*
* @var string
*/
public const MovedTo = 'MovedTo';

/**
* When event fired
*
* @var \Illuminate\Support\Carbon
*/
protected $when;

/**
* Directory or file path
*
* @var string
*/
protected $path;

/**
* Event types
*
* @var array
*/
protected $types;

/**
* Event constructor.
*
* @param \Illuminate\Support\Carbon $when
* @param string $path
* @param array $types
*/
public function __construct(Carbon $when, string $path, array $types)
{
$this->when = $when;
$this->path = $path;
$this->types = $types;
}

/**
* @return \Illuminate\Support\Carbon
*/
public function getWhen(): Carbon
{
return $this->when;
}

/**
* @return string
*/
public function getPath(): string
{
return $this->path;
}

/**
* @return array
*/
public function getTypes(): array
{
return $this->types;
}

/**
* Checks if event types has needed type(s)
*
* @param string ...$types
*
* @return bool
*/
public function isType(string ...$types): bool
{
return count(array_intersect($this->types, $types)) > 0;
}
}
35 changes: 35 additions & 0 deletions src/HotReload/FSEventParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace SwooleTW\Http\HotReload;

use Illuminate\Support\Carbon;

/**
* Class FSEventParser
*/
class FSEventParser
{
protected const REGEX = '/^([\S+]{3}\s+[\S+]{3}\s+[\d+]{2}\s+[\d+]{2}:[\d+]{2}:{0,2}:[\d+]{2}:{0,2}\s+[\d+]{0,4})\s+(\/[\S+]*)\s+([\S+*\s+]*)/mi';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the reason that FSEventParser::toEvent($event) is always null in my environment. I'm using OSX and fswatch 1.14.0. Here's what I received for the fswatch event:

Mon Jan  7 23:30:28 2019 /Users/Albert/Projects/laravel-swoole-demo/vendor/swooletw/laravel-swoole/src/HotReload/FSProcess.php PlatformSpecific Updated IsFile

I haven't tested it on other platforms. But it works for my after changing the regex to:

^([\S+]{3}\s+[\S+]{3}\s+[\d+]{1,2}\s+...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fractalzombie ,

Thank you so much ^^


protected const DATE = 1;
protected const PATH = 2;
protected const EVENTS = 3;

/**
* @param string $event
*
* @return \SwooleTW\Http\HotReload\FSEvent
*/
public static function toEvent(string $event): ?FSEvent
{
if (preg_match(static::REGEX, $event, $matches)) {
$date = Carbon::parse($matches[static::DATE]);
$path = $matches[static::PATH];
$events = explode(' ', $matches[static::EVENTS]);

return new FSEvent($date, $path, $events);
}

return null;
}
}
Loading