Skip to content

Commit

Permalink
Squashed 'src/framework/' changes from ba1657b..b3a45be
Browse files Browse the repository at this point in the history
b3a45be fix error on trigger event handler (#196)
b1a48cd fix a bug when warnings appear in the high version of swoole (#195)
9c4da9f 自定义组件支持别名配置 (#181)
bccf7a6 修复restart不能正常使用的BUG (#180)
97bfe2e 修改连接池配置的timeout字段为float (#177)
525097b 修改自定义组件只能扫描Aop、Command、Bootstrap三个文件的BUG (#169)
0b5f320 兼容swoole 4.0.3版本 (#166)
1d11b56 增加用户自定义组件配置 (#150)

git-subtree-dir: src/framework
git-subtree-split: b3a45be705c9cb53cb357925e228e4f60e6d76d5
  • Loading branch information
swoft-bot committed Oct 6, 2018
1 parent 1fab514 commit f398e06
Show file tree
Hide file tree
Showing 22 changed files with 361 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ before_install:
install:
- wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz -O hiredis.tar.gz && mkdir -p hiredis && tar -xf hiredis.tar.gz -C hiredis --strip-components=1 && cd hiredis && sudo make -j$(nproc) && sudo make install && sudo ldconfig && cd ..
- echo 'no' | pecl install -f redis
- wget https://github.com/swoole/swoole-src/archive/v4.0.2.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-async-redis && make -j$(nproc) && make install && cd -
- wget https://github.com/swoole/swoole-src/archive/v4.0.3.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure --enable-async-redis && make -j$(nproc) && make install && cd -
- echo "extension = swoole.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"autoload-dev": {
"psr-4": {
"SwoftTest\\": "test/Cases"
"SwoftTest\\": "test/Cases",
"SwoftTest\\Testing\\": "test/Testing/"
}
},
"repositories": {
Expand Down
14 changes: 14 additions & 0 deletions src/Bean/Resource/AnnotationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ abstract class AnnotationResource extends AbstractResource
'Example',
];

/**
* the custom components
* @var array
*/
protected $customComponents = [];

/**
* AnnotationResource constructor.
*
Expand All @@ -88,6 +94,9 @@ abstract class AnnotationResource extends AbstractResource
public function __construct(array $properties)
{
$this->properties = $properties;
if (isset($properties['components']['custom']) && is_array($properties['components']['custom'])) {
$this->customComponents = $properties['components']['custom'];
}
}

/**
Expand Down Expand Up @@ -349,4 +358,9 @@ private function parseClassAnnotations(string $className, array $annotation, arr
}
}
}

public function getComponentNamespaces(): array
{
return $this->componentNamespaces;
}
}
80 changes: 80 additions & 0 deletions src/Bean/Resource/CustomComponentsRegister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace Swoft\Bean\Resource;

use Swoft\Helper\ComposerHelper;

trait CustomComponentsRegister
{
/**
* Register the server namespace
* @author limx
*/
public function registerServerNamespace()
{
foreach ($this->customComponents as $ns => $componentDir) {
if (is_int($ns)) {
$ns = $componentDir;
$componentDir = ComposerHelper::getDirByNamespace($ns);
$ns = rtrim($ns, "\\");
$componentDir = rtrim($componentDir, "/");
}

$this->componentNamespaces[] = $ns;
$componentDir = alias($componentDir);

foreach ($this->serverScan as $dir) {
$scanDir = $componentDir . DS . $dir;
if (!is_dir($scanDir)) {
continue;
}

$scanNs = $ns . "\\" . $dir;
$this->scanNamespaces[$scanNs] = $scanDir;
}
}
}

/**
* Register the worker namespace
* @author limx
*/
public function registerWorkerNamespace()
{
foreach ($this->customComponents as $ns => $componentDir) {
if (is_int($ns)) {
$ns = $componentDir;
$componentDir = ComposerHelper::getDirByNamespace($ns);
$ns = rtrim($ns, "\\");
$componentDir = rtrim($componentDir, "/");
}

$this->componentNamespaces[] = $ns;
$componentDir = alias($componentDir);

if (!is_dir($componentDir)) {
continue;
}

$scanDirs = scandir($componentDir, null);

foreach ($scanDirs as $dir) {
if ($dir == '.' || $dir == '..') {
continue;
}
if (\in_array($dir, $this->serverScan, true)) {
continue;
}

$scanDir = $componentDir . DS . $dir;

if (!is_dir($scanDir)) {
$this->scanFiles[$ns][] = $scanDir;
continue;
}
$scanNs = $ns . '\\' . $dir;

$this->scanNamespaces[$scanNs] = $scanDir;
}
}
}
}
4 changes: 4 additions & 0 deletions src/Bean/Resource/ServerAnnotationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
class ServerAnnotationResource extends AnnotationResource
{
use CustomComponentsRegister;

/**
* Register the scaned namespace
*/
Expand Down Expand Up @@ -46,5 +48,7 @@ public function registerNamespace()
$this->scanNamespaces[$scanNs] = $scanDir;
}
}

$this->registerServerNamespace();
}
}
4 changes: 4 additions & 0 deletions src/Bean/Resource/WorkerAnnotationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class WorkerAnnotationResource extends AnnotationResource
{
use CustomComponentsRegister;

/**
* Register the scaned namespace
*/
Expand Down Expand Up @@ -67,5 +69,7 @@ public function registerNamespace()
$this->scanNamespaces[$scanNs] = $scanDir;
}
}

$this->registerWorkerNamespace();
}
}
14 changes: 7 additions & 7 deletions src/Bootstrap/Server/AbstractServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,23 @@ public function reload($onlyTask = false)
*/
public function stop(): bool
{
//获取master进程ID
// 获取master进程ID
$masterPid = $this->serverSetting['masterPid'];
//使用swoole_process::kill代替posix_kill
// 使用swoole_process::kill代替posix_kill
$result = \swoole_process::kill($masterPid);
$timeout = 60;
$startTime = time();
while (true) {
//检测进程是否退出
if(!\swoole_process::kill($masterPid, 0)) {
//判断是否超时
if(time() - $startTime >= $timeout) {
// Check the process status
if (\swoole_process::kill($masterPid, 0)) {
// 判断是否超时
if (time() - $startTime >= $timeout) {
return false;
}
usleep(10000);
continue;
}

return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function resume($coroutineId)
*/
public static function isSupportCoroutine(): bool
{
if (swoole_version() >= '2.0.11') {
if (version_compare(swoole_version(), '2.0.11', '>=')) {
return true;
} else {
return App::isWorkerStatus();
Expand All @@ -119,6 +119,6 @@ public static function isSupportCoroutine(): bool
*/
public static function shouldWrapCoroutine()
{
return App::isWorkerStatus() && swoole_version() >= '2.0.11';
return App::isWorkerStatus() && version_compare(swoole_version(), '2.0.11', '>=');
}
}
2 changes: 1 addition & 1 deletion src/Event/LazyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(callable $callback)
*/
public function handle(EventInterface $event)
{
return PhpHelper::call($this->callback, $event);
return PhpHelper::call($this->callback, [$event]);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions src/Pool/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,18 @@ private function getConnectionByChannel(): ConnectionInterface
return $this->channel->pop();
}

// When swoole version is larger than 4.0.3, Channel->select is removed.
if (version_compare(swoole_version(), '4.0.3', '>=')) {
$result = $this->channel->pop($maxWaitTime);
if ($result === false) {
throw new ConnectionException('Connection pool waiting queue timeout, timeout=' . $maxWaitTime);
}
return $result;
}

$writes = [];
$reads = [$this->channel];
$result = $this->channel->select($reads, $writes, $maxWaitTime);
$reads = [$this->channel];
$result = $this->channel->select($reads, $writes, $maxWaitTime);

if ($result === false || empty($reads)) {
throw new ConnectionException('Connection pool waiting queue timeout, timeout='.$maxWaitTime);
Expand Down
4 changes: 2 additions & 2 deletions src/Pool/PoolConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function getMaxActive(): int;
public function getMaxWait(): int;

/**
* @return int
* @return float
*/
public function getTimeout(): int;
public function getTimeout(): float;

/**
* @return array
Expand Down
6 changes: 3 additions & 3 deletions src/Pool/PoolProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PoolProperties implements PoolConfigInterface
/**
* Connection timeout
*
* @var int
* @var float
*/
protected $timeout = 3;

Expand Down Expand Up @@ -126,9 +126,9 @@ public function getMaxWait(): int
}

/**
* @return int
* @return float
*/
public function getTimeout(): int
public function getTimeout(): float
{
return $this->timeout;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Testing/SwooleResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public function cookie($name, $value = null, $expires = null, $path = null, $dom

/**
* 设置HttpCode,如404, 501, 200
* @param $code
* @param int $code
* @param string $reason
*/
public function status($code)
public function status($code, $reason = NULL)
{
}

Expand Down
23 changes: 23 additions & 0 deletions test/Cases/BeanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
*/
namespace SwoftTest;

use Swoft\App;
use Swoft\Bean\Resource\ServerAnnotationResource;
use Swoft\Proxy\Proxy;
use SwoftTest\Bean\ProxyTest;
use SwoftTest\Bean\TestHandler;
use SwoftTest\Testing\Bean\Config;

/**
* Class BeanTest
Expand Down Expand Up @@ -39,4 +42,24 @@ public function testProxy()
$this->assertEquals('p1p2beforeafter', $proxy->publicFun2Trait('p1', 'p2'));
$this->assertEquals('p1p2beforeafter', $proxy->publicFun3Trait('p1', 'p2'));
}

public function testCustomComponentNamespaces()
{
$config = App::getProperties()->toArray();
$this->assertArrayHasKey('components', $config);
$resource = new ServerAnnotationResource($config);
$resource->registerNamespace();

$namespace = $resource->getComponentNamespaces();
$this->assertNotFalse(array_search('SwoftTest', $namespace));
}

public function testCustomComponentSupportAlias()
{
$config = bean(Config::class);
$this->assertEquals('test', $config->getName());

$config = bean(\SwoftTest\Testing\Bean2\Config::class);
$this->assertEquals('test', $config->getName());
}
}
33 changes: 33 additions & 0 deletions test/Cases/Connection/DemoConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace SwoftTest\Connection;

use Swoft\Pool\AbstractConnection;

class DemoConnection extends AbstractConnection
{
/**
* @var Client
*/
protected $connection;

public function createConnection()
{
$this->connection = new \stdClass();
$this->connection->id = uniqid();
}

public function reconnect()
{
$this->createConnection();
}

public function check(): bool
{
return true;
}

public function getConnection()
{
return $this->connection;
}
}
22 changes: 22 additions & 0 deletions test/Cases/CorouotineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace SwoftTest;

use Swoft\Core\Coroutine;

/**
* Class CorouotineTest
*
* @package Swoft\Test\Cases
*/
class CorouotineTest extends AbstractTestCase
{
public function testIsSupportCoroutine()
{
$this->assertTrue(Coroutine::isSupportCoroutine());
}

public function testShouldWrapCoroutine()
{
$this->assertFalse(Coroutine::shouldWrapCoroutine());
}
}

0 comments on commit f398e06

Please sign in to comment.