Skip to content

Commit

Permalink
Squashed 'src/framework/' changes from 2da84a5e..ba1657b8
Browse files Browse the repository at this point in the history
ba1657b8 Upstream travis ci config (#160)
ffdb60e5 修复Json Validator会失效的BUG (#153)
4d788e9a 修复自定义注解无法使用Aop切面编程的BUG (#139)
405a83e3 Optimize  (#143)
b72e04ce 修复执行php bin/swoft stop命令时master进程异常未退出的问题,以及停止失败后,pid文件被删除的问题 (#134)
a30bf589 修改`ComposerHelper::getDirByNamespace`匹配有误的BUG (#137)
43088e60 Update PoolHelper.php (#131)
82aadc28 Scan bean according to Composer settings (#115)
5e55e722 Add psysh for debug (#109)
93c3ec76 Parse namespace to dir path according to composer settings (#103)

git-subtree-dir: src/framework
git-subtree-split: ba1657b85d5d5f1e846333e8f5ad03e499b76db8
  • Loading branch information
huangzhhui committed Aug 7, 2018
1 parent 740e211 commit 1fab514
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 33 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 7.0
- 7.1
- 7.2

services:
- mysql
Expand All @@ -12,9 +13,12 @@ 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 ..
- pecl install -f swoole-2.0.12
- 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 -
- echo "extension = swoole.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

before_script:
- phpenv config-rm xdebug.ini
- composer update

script: composer test
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"require-dev": {
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
"friendsofphp/php-cs-fixer": "^2.10",
"psy/psysh": "@stable"
},
"autoload-dev": {
"psr-4": {
Expand All @@ -42,7 +43,7 @@
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
"url": "https://packagist.laravel-china.org"
}
},
"scripts": {
Expand Down
15 changes: 7 additions & 8 deletions src/Bean/Resource/AnnotationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Swoft\Bean\Wrapper\WrapperInterface;
use Swoft\Helper\ComponentHelper;
use Swoft\Helper\JsonHelper;
use Swoft\Helper\ComposerHelper;

/**
* Annotation resource
Expand Down Expand Up @@ -189,8 +188,6 @@ public function parseAnnotationsData()
}

/**
* 添加扫描namespace
*
* @param array $namespaces
*/
public function addScanNamespace(array $namespaces)
Expand All @@ -200,10 +197,12 @@ public function addScanNamespace(array $namespaces)
$this->scanNamespaces[$key] = $namespace;
continue;
}

$nsPath = str_replace("\\", "/", $namespace);
$nsPath = str_replace('App/', 'app/', $nsPath);
$this->scanNamespaces[$namespace] = BASE_PATH . "/" . $nsPath;
$nsPath = ComposerHelper::getDirByNamespace($namespace);
if (!$nsPath) {
$nsPath = str_replace("\\", "/", $namespace);
$nsPath = BASE_PATH . "/" . $nsPath;
}
$this->scanNamespaces[$namespace] = $nsPath;
}

$this->registerNamespace();
Expand Down
12 changes: 10 additions & 2 deletions src/Bean/Wrapper/AbstractWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ private function parseMethodAnnotations(string $className, \ReflectionMethod $me
// 循环方法注解解析
foreach ($methodAnnotations[$methodName] as $methodAnnotationAry) {
foreach ($methodAnnotationAry as $methodAnnotation) {
$annotationClass = get_class($methodAnnotation);
if (!in_array($annotationClass, $this->getMethodAnnotations())) {
if (!$this->inMethodAnnotations($methodAnnotation)) {
continue;
}

Expand All @@ -223,6 +222,15 @@ private function parseMethodAnnotations(string $className, \ReflectionMethod $me
}
}

/**
* @return bool
*/
protected function inMethodAnnotations($methodAnnotation): bool
{
$annotationClass = get_class($methodAnnotation);
return in_array($annotationClass, $this->getMethodAnnotations());
}

/**
* 方法没有配置路由注解解析
*
Expand Down
7 changes: 6 additions & 1 deletion src/Bean/Wrapper/BeanWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function isParsePropertyAnnotations(array $annotations): bool
*/
public function isParseMethodAnnotations(array $annotations): bool
{
return isset($annotations[Cacheable::class]) || isset($annotations[CachePut::class]);
return true;
}

protected function inMethodAnnotations($methodAnnotation): bool
{
return true;
}
}
26 changes: 13 additions & 13 deletions src/Bootstrap/Server/AbstractServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,25 @@ public function reload($onlyTask = false)
*/
public function stop(): bool
{
//获取master进程ID
$masterPid = $this->serverSetting['masterPid'];
//使用swoole_process::kill代替posix_kill
$result = \swoole_process::kill($masterPid);
$timeout = 60;
$startTime = time();
$this->serverSetting['masterPid'] && posix_kill($this->serverSetting['masterPid'], SIGTERM);

$result = true;
while (1) {
$masterIslive = $this->serverSetting['masterPid'] && posix_kill($this->serverSetting['masterPid'], SIGTERM);
if ($masterIslive) {
if (time() - $startTime >= $timeout) {
$result = false;
break;
while (true) {
//检测进程是否退出
if(!\swoole_process::kill($masterPid, 0)) {
//判断是否超时
if(time() - $startTime >= $timeout) {
return false;
}
usleep(10000);
continue;
}

break;
return true;
}
return $result;
}

/**
Expand All @@ -159,7 +159,7 @@ public function isRunning(): bool
$pFile = $this->serverSetting['pfile'];

// Is pid file exist ?
if (file_exists($pFile)) {
if (\file_exists($pFile)) {
// Get pid file content and parse the content
$pidFile = file_get_contents($pFile);
$pids = explode(',', $pidFile);
Expand Down
69 changes: 69 additions & 0 deletions src/Helper/ComposerHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Swoft\Helper;


use Composer\Autoload\ClassLoader;

class ComposerHelper
{

/**
* @var ClassLoader|mixed
*/
static $loader;

/**
* @return ClassLoader
*/
public static function getLoader(): ClassLoader
{
if (! self::$loader) {
$loader = self::findLoader();
$loader instanceof ClassLoader && self::$loader = $loader;
}
return self::$loader;
}

/**
* @return ClassLoader
* @throws \RuntimeException When Composer loader not found
*/
public static function findLoader(): ClassLoader
{
$composerClass = '';
foreach (get_declared_classes() as $declaredClass) {
if (StringHelper::startsWith($declaredClass, 'ComposerAutoloaderInit') && method_exists($declaredClass, 'getLoader')) {
$composerClass = $declaredClass;
break;
}
}
if (! $composerClass) {
throw new \RuntimeException('Composer loader not found.');
}
return $composerClass::getLoader();
}

/**
* @param string $namespace
* @return string
*/
public static function getDirByNamespace(string $namespace): string
{
$dir = '';
$loader = self::findLoader();
$prefixesPsr4 = $loader->getPrefixesPsr4();
$maxLength = 0;
foreach ($prefixesPsr4 as $prefix => $path) {
if (StringHelper::startsWith($namespace, $prefix)) {
$strLen = strlen($prefix);
if ($strLen > $maxLength) {
$dir = current($path) . DIRECTORY_SEPARATOR . substr($namespace, $strLen);
$maxLength = $strLen;
}
}
}
return $dir;
}

}
7 changes: 6 additions & 1 deletion src/Helper/PhpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ public static function isMac(): bool
*/
public static function call($cb, array $args = [])
{
$ret = null;
if (\is_object($cb) || (\is_string($cb) && \function_exists($cb))) {
$ret = $cb(...$args);
} elseif (\is_array($cb)) {
list($obj, $mhd) = $cb;
$ret = \is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
} else {
$ret = \Swoole\Coroutine::call_user_func_array($cb, $args);
if (SWOOLE_VERSION >= '4.0') {
$ret = call_user_func_array($cb, $args);
} else {
$ret = \Swoole\Coroutine::call_user_func_array($cb, $args);
}
}

return $ret;
Expand Down
4 changes: 2 additions & 2 deletions src/Helper/PoolHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class PoolHelper
*/
public static function getContextCntKey(): string
{
return sprintf('connectioins');
return sprintf('connections');
}
}
}
32 changes: 32 additions & 0 deletions test/Cases/Aop/Annotation/DemoAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace SwoftTest\Aop\Annotation;

/**
* Class DemoAnnotation
* @Annotation
* @Target("METHOD")
* @package SwoftTest\Aop\Annotation
*/
class DemoAnnotation
{
/**
* @var string
*/
private $name;

public function __construct(array $values)
{
if (isset($values['name'])) {
$this->name = $values['name'];
}
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
10 changes: 10 additions & 0 deletions test/Cases/Aop/AnnotationAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Cacheable;
use Swoft\Bean\Annotation\CachePut;
use SwoftTest\Aop\Annotation\DemoAnnotation;

/**
*
Expand Down Expand Up @@ -41,4 +42,13 @@ public function cacheable()
{
return 'cacheable';
}

/**
* @DemoAnnotation(name=" hello")
* @return string
*/
public function demoAnnotation()
{
return 'demo';
}
}
17 changes: 14 additions & 3 deletions test/Cases/Aop/AnnotationAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
use Swoft\Bean\Annotation\PointAnnotation;
use Swoft\Bean\Annotation\Cacheable;
use Swoft\Bean\Annotation\CachePut;
use SwoftTest\Aop\Annotation\DemoAnnotation;
use SwoftTest\Aop\Collector\DemoCollector;

/**
* @Aspect
* @PointAnnotation(
* include={
* Cacheable::class,
* CachePut::class
* CachePut::class,
* DemoAnnotation::class
* }
* )
* @uses AnnotationAspect
Expand All @@ -37,9 +40,17 @@ class AnnotationAspect
*/
public function around(ProceedingJoinPoint $proceedingJoinPoint)
{
$tag = ' around before ';
$class = $proceedingJoinPoint->getTarget();
$method = $proceedingJoinPoint->getMethod();

$tag = '';
if ($annotation = DemoCollector::$methodAnnotations[get_class($class)][$method] ?? null) {
$tag .= $annotation->getName();
}

$tag .= ' around before ';
$result = $proceedingJoinPoint->proceed();
$tag .= ' around after ';
return $result.$tag;
return $result . $tag;
}
}
12 changes: 12 additions & 0 deletions test/Cases/Aop/Collector/DemoCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace SwoftTest\Aop\Collector;

class DemoCollector
{
/**
* The annotations of method
*
* @var array
*/
public static $methodAnnotations = [];
}
16 changes: 16 additions & 0 deletions test/Cases/Aop/Parser/DemoAnnotationParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SwoftTest\Aop\Parser;

use Swoft\Bean\Collector;
use SwoftTest\Aop\Collector\DemoCollector;

class DemoAnnotationParser
{
public function parser(string $className, $objectAnnotation = null, string $propertyName = "", string $methodName = "", $propertyValue = null)
{
Collector::$methodAnnotations[$className][$methodName][] = get_class($objectAnnotation);
DemoCollector::$methodAnnotations[$className][$methodName] = $objectAnnotation;
return null;
}
}
Loading

0 comments on commit 1fab514

Please sign in to comment.