Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #50 from swooletw/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
albertcht authored May 12, 2018
2 parents 0a56086 + e43e962 commit 456c366
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 17 deletions.
18 changes: 15 additions & 3 deletions config/swoole_http.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
// Normally this value should be 1~4 times larger according to your cpu cores.
'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num() * 2),
'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num() * 2),
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num() * 2),
'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num()),
'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num()),
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num()),
// The data to receive can't be larger than buffer_output_size.
'package_max_length' => 20 * 1024 * 1024,
// The data to send can't be larger than buffer_output_size.
Expand All @@ -35,6 +35,9 @@
'max_request' => 3000,
// Enable coroutine send
'send_yield' => true,
// You must --enable-openssl while compiling Swoole
'ssl_cert_file' => null,
'ssl_key_file' => null,
],
],

Expand Down Expand Up @@ -63,6 +66,15 @@
//
],

/*
|--------------------------------------------------------------------------
| Providers here will be registered on every request.
|--------------------------------------------------------------------------
*/
'providers' => [
Illuminate\Pagination\PaginationServiceProvider::class,
],

/*
|--------------------------------------------------------------------------
| Define your swoole tables here.
Expand Down
1 change: 0 additions & 1 deletion src/Server/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Container\Container;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
Expand Down
8 changes: 4 additions & 4 deletions src/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ protected function createSwooleServer()
$server = $this->isWebsocket ? WebsocketServer::class : HttpServer::class;
$host = $this->container['config']->get('swoole_http.server.host');
$port = $this->container['config']->get('swoole_http.server.port');
$hasCert = $this->container['config']->get('swoole_http.server.options.ssl_cert_file');
$hasKey = $this->container['config']->get('swoole_http.server.options.ssl_key_file');
$args = $hasCert && $hasKey ? [SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL] : [];

$this->server = new $server($host, $port);
$this->server = new $server($host, $port, ...$args);
}

/**
Expand Down Expand Up @@ -250,9 +253,6 @@ public function onRequest($swooleRequest, $swooleResponse)
$application = $this->sandbox->getApplication();
$this->sandbox->enable();

// bind illuminate request to laravel/lumen
$application->getApplication()->instance('request', $illuminateRequest);

// handle request via laravel/lumen's dispatcher
$illuminateResponse = $application->run($illuminateRequest);
$response = Response::make($illuminateResponse, $swooleResponse);
Expand Down
72 changes: 67 additions & 5 deletions src/Server/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Container\Container;
use SwooleTW\Http\Server\Application;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;

class Sandbox
Expand All @@ -30,6 +31,11 @@ class Sandbox
*/
protected $request;

/**
* @var array
*/
protected $providers = [];

/**
* @var boolean
*/
Expand All @@ -52,7 +58,8 @@ public static function make(Application $application)
public function __construct(Application $application)
{
$this->setApplication($application);
$this->setInitialConfig($application);
$this->setInitialConfig();
$this->setInitialProviders();
}

/**
Expand All @@ -77,12 +84,26 @@ public function setRequest(Request $request)

/**
* Set config snapshot.
*
* @param \SwooleTW\Http\Server\Application
*/
protected function setInitialConfig(Application $application)
protected function setInitialConfig()
{
$this->config = clone $this->application->getApplication()->make('config');
}

/**
* Initialize customized service providers.
*/
protected function setInitialProviders()
{
$this->config = clone $application->getApplication()['config'];
$application = $this->application->getApplication();
$providers = $this->config->get('swoole_http.providers', []);

foreach ($providers as $provider) {
if (class_exists($provider)) {
$provider = new $provider($application);
$this->providers[get_class($provider)] = $provider;
}
}
}

/**
Expand Down Expand Up @@ -111,8 +132,10 @@ protected function resetLaravelApp($application)
$this->resetSession($application);
$this->resetCookie($application);
$this->clearInstances($application);
$this->bindRequest($application);
$this->rebindRouterContainer($application);
$this->rebindViewContainer($application);
$this->resetProviders($application);
}

/**
Expand All @@ -126,6 +149,45 @@ protected function clearInstances($application)
}
}

/**
* Bind illuminate request to laravel/lumen application.
*/
protected function bindRequest($application)
{
if ($this->request instanceof Request) {
$application->instance('request', $this->request);
}
}

/**
* Re-register and reboot service providers.
*/
protected function resetProviders($application)
{
foreach ($this->providers as $provider) {
$this->rebindProviderContainer($provider, $application);
if (method_exists($provider, 'register')) {
$provider->register();
}
if (method_exists($provider, 'boot')) {
$application->call([$provider, 'boot']);
}
}
}

/**
* Rebind service provider's container.
*/
protected function rebindProviderContainer($provider, $application)
{
$closure = function () use ($application) {
$this->app = $application;
};

$resetProvider = $closure->bindTo($provider, $provider);
$resetProvider();
}

/**
* Reset laravel/lumen's config to initial values.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Websocket/CanWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ protected function normalizePushData(array $data)
*/
protected function callOnConnect($illuminateRequest)
{
$application = $this->sandbox->getLaravelApp();
// set currnt request to sandbox
$this->sandbox->setRequest($illuminateRequest);

// bind illuminate request to laravel/lumen
$application->instance('request', $illuminateRequest);
Facade::clearResolvedInstance('request');
// get application from sandbox
$application = $this->sandbox->getLaravelApp();

// reset session
if (isset($application['session'])) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Server/SandboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ protected function getSandbox()

protected function getContainer()
{
$config = m::mock(Illuminate\Config\Repository::class);
$config->shouldReceive('get')
->andReturn([]);
$container = m::mock(Container::class);
$container->shouldReceive('offsetGet')
->andReturn((object)[]);
$container->shouldReceive('make')
->andReturn($config);

return $container;
}
Expand Down

0 comments on commit 456c366

Please sign in to comment.