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 #193 from swooletw/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
albertcht authored Jan 14, 2019
2 parents 92d93f9 + 210d9a2 commit feff91b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Concerns/InteractsWithSwooleQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SwooleTW\Http\Concerns;

use Swoole\Table;
use SwooleTW\Http\Table\SwooleTable;

trait InteractsWithSwooleQueue
{
/**
* Indicates if a packet is swoole's queue job.
*
* @param mixed
*/
protected function isSwooleQueuePacket($packet)
{
if (! is_string($packet)) {
return false;
}

$decoded = json_decode($packet, true);

return JSON_ERROR_NONE === json_last_error() && isset($decoded['job']);
}
}
17 changes: 17 additions & 0 deletions src/Concerns/InteractsWithWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ public function onClose($server, $fd, $reactorId)
}
}

/**
* Indicates if a packet is websocket push action.
*
* @param mixed
*/
protected function isWebsocketPushPacket($packet)
{
if ( !is_array($packet)) {
return false;
}

return $this->isWebsocket
&& array_key_exists('action', $packet)
&& $packet['action'] === Websocket::PUSH_ACTION;
}


/**
* Push websocket message to clients.
*
Expand Down
23 changes: 22 additions & 1 deletion src/Concerns/WithApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ trait WithApplication
protected function bootstrap()
{
if ($this->framework === 'laravel') {
$this->app->bootstrap();
$bootstrappers = $this->getBootstrappers();
$this->app->bootstrapWith($bootstrappers);
} else {
// for Lumen 5.7
// https://github.com/laravel/lumen-framework/commit/42cbc998375718b1a8a11883e033617024e57260#diff-c9248b3167fc44af085b81db2e292837
Expand Down Expand Up @@ -137,4 +138,24 @@ protected function preResolveInstances()
}
}
}

/**
* Get bootstrappers.
*
* @return array
* @throws \ReflectionException
*/
protected function getBootstrappers()
{
$kernel = $this->getApplication()->make(Kernel::class);

$reflection = new \ReflectionObject($kernel);
$bootstrappersMethod = $reflection->getMethod('bootstrappers');
$bootstrappersMethod->setAccessible(true);
$bootstrappers = $bootstrappersMethod->invoke($kernel);

array_splice($bootstrappers, -2, 0, ['Illuminate\Foundation\Bootstrap\SetRequestForConsole']);

return $bootstrappers;
}
}
2 changes: 2 additions & 0 deletions src/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Debug\ExceptionHandler;
use SwooleTW\Http\Concerns\InteractsWithWebsocket;
use SwooleTW\Http\Concerns\InteractsWithSwooleQueue;
use SwooleTW\Http\Concerns\InteractsWithSwooleTable;
use Symfony\Component\Debug\Exception\FatalThrowableError;

Expand All @@ -28,6 +29,7 @@ class Manager
{
use InteractsWithWebsocket,
InteractsWithSwooleTable,
InteractsWithSwooleQueue,
WithApplication;

/**
Expand Down
20 changes: 18 additions & 2 deletions tests/fixtures/bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<?php

use Mockery as m;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Contracts\Container\Container;

$kernel = new TestKernel;
$app = m::mock(Container::class);
$app->shouldReceive('bootstrap')
->once();

$app->shouldReceive('make')
->with(Kernel::class)
->once()
->andReturn($kernel);
$app->shouldReceive('bootstrapWith')
->once()
->andReturn($kernel);
$app->shouldReceive('offsetExists')
->with('foo')
->once()
Expand All @@ -17,3 +25,11 @@
$app->shouldReceive('alias');

return $app;

class TestKernel
{
public function bootstrappers()
{
return [];
}
}

0 comments on commit feff91b

Please sign in to comment.