Skip to content

Commit

Permalink
Merge pull request #89 from inhere/master
Browse files Browse the repository at this point in the history
update: add new tag ServerListener, some other update
  • Loading branch information
inhere authored Mar 20, 2018
2 parents 400d1ff + 337f827 commit 5ba081b
Show file tree
Hide file tree
Showing 23 changed files with 338 additions and 113 deletions.
7 changes: 3 additions & 4 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ class App
*
* @var array
*/
private static $aliases
= [
'@swoft' => __DIR__,
];
private static $aliases = [
'@swoft' => __DIR__,
];

/**
* 获取mysqlBean对象
Expand Down
2 changes: 1 addition & 1 deletion src/Bean/Annotation/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Listener
*
* @var string
*/
private $event = "";
private $event = '';

/**
* AutoController constructor.
Expand Down
56 changes: 56 additions & 0 deletions src/Bean/Annotation/ServerListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2018/3/19
* Time: 上午11:43
*/

namespace Swoft\Bean\Annotation;

/**
* Class ServerListener
* 跟 SwooleListener 差别不大,同样可用于监听Swoole的事件,是对 SwooleListener 的补充
* 区别是:
* - SwooleListener 中事件的监听器只允许一个,并且是直接注册到 swoole server上的(监听相同事件将会被覆盖)
* - ServerListener 允许对swoole事件添加多个监听器,会逐个通知
* - ServerListener 不影响 `/framework/src/Bootstrap/Server` 里基础swoole事件的绑定
*
* @package Swoft\Bean\Annotation
*
* @Annotation
* @Target("CLASS")
*/
class ServerListener
{
/**
* the events of listener
*
* @var array
*/
private $event;

/**
* AutoController constructor.
*
* @param array $values
*/
public function __construct(array $values)
{
if (isset($values['value'])) {
$this->event = (array)$values['value'];
}

if (isset($values['event'])) {
$this->event = (array)$values['event'];
}
}

/**
* @return array
*/
public function getEvent(): array
{
return $this->event ?: [];
}
}
6 changes: 3 additions & 3 deletions src/Bean/Annotation/SwooleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class SwooleListener
public function __construct(array $values)
{
if (isset($values['value'])) {
$this->event = $values['value'];
$this->event = (array)$values['value'];
}

if (isset($values['event'])) {
$this->event = $values['event'];
$this->event = (array)$values['event'];
}

if (isset($values['type'])) {
Expand Down Expand Up @@ -82,4 +82,4 @@ public function getOrder(): int
{
return $this->order;
}
}
}
5 changes: 2 additions & 3 deletions src/Bean/Collector/BootstrapCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class BootstrapCollector implements CollectorInterface
*/
private static $bootstraps = [];


/**
* collect
*
Expand All @@ -32,7 +31,7 @@ class BootstrapCollector implements CollectorInterface
* @param null $propertyValue
* @return void
*/
public static function collect(string $className, $objectAnnotation = null, string $propertyName = "", string $methodName = "", $propertyValue = null)
public static function collect(string $className, $objectAnnotation = null, string $propertyName = '', string $methodName = '', $propertyValue = null)
{
if ($objectAnnotation instanceof Bootstrap) {

Expand All @@ -52,4 +51,4 @@ public static function getCollector()
return self::$bootstraps;
}

}
}
9 changes: 8 additions & 1 deletion src/Bean/Collector/ServerListenerCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swoft\Bean\Collector;

use Swoft\Bean\Annotation\BeforeStart;
use Swoft\Bean\Annotation\ServerListener;
use Swoft\Bean\CollectorInterface;
use Swoft\Bootstrap\SwooleEvent;

Expand All @@ -28,6 +29,12 @@ public static function collect(string $className, $objectAnnotation = null, stri
{
if($objectAnnotation instanceof BeforeStart){
self::$listeners[SwooleEvent::ON_BEFORE_START][] = $className;
} elseif($objectAnnotation instanceof ServerListener){
$events = $objectAnnotation->getEvent();

foreach ($events as $event) {
self::$listeners[$event][] = $className;
}
}
}

Expand All @@ -39,4 +46,4 @@ public static function getCollector()
return self::$listeners;
}

}
}
8 changes: 4 additions & 4 deletions src/Bean/Collector/SwooleListenerCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function collect(string $className, $objectAnnotation = null, stri
}

$events = $objectAnnotation->getEvent();
if (empty($events) || !is_array($events)) {
if (empty($events) || !\is_array($events)) {
return;
}

Expand All @@ -45,10 +45,10 @@ public static function collect(string $className, $objectAnnotation = null, stri
if(!SwooleEvent::isSwooleEvent($event)){
continue;
}
if($type == SwooleEvent::TYPE_PORT){
if($type === SwooleEvent::TYPE_PORT){
$order = $objectAnnotation->getOrder();
self::$listeners[$type][$order][$event] = $className;
}elseif ($type == SwooleEvent::TYPE_SERVER){
} elseif ($type === SwooleEvent::TYPE_SERVER){
self::$listeners[$type][$event] = $className;
}
}
Expand All @@ -62,4 +62,4 @@ public static function getCollector()
return self::$listeners;
}

}
}
34 changes: 34 additions & 0 deletions src/Bean/Parser/ServerListenerParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Swoft\Bean\Parser;

use Swoft\Bean\Annotation\Scope;
use Swoft\Bean\Annotation\ServerListener;
use Swoft\Bean\Collector\ServerListenerCollector;

/**
* Class ServerListenerParser
* @package Swoft\Bean\Parser
* @author inhere <[email protected]>
*/
class ServerListenerParser extends AbstractParser
{
/**
* @param string $className
* @param ServerListener $objectAnnotation
* @param string $propertyName
* @param string $methodName
* @param mixed $propertyValue
*
* @return array
*/
public function parser(string $className, $objectAnnotation = null, string $propertyName = '', string $methodName = '', $propertyValue = null)
{
$beanName = $className;
$scope = Scope::SINGLETON;

ServerListenerCollector::collect($className, $objectAnnotation, $propertyName, $methodName, $propertyValue);

return [$beanName, $scope, ''];
}
}
63 changes: 63 additions & 0 deletions src/Bean/Wrapper/ServerListenerWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Swoft\Bean\Wrapper;

use Swoft\Bean\Annotation\ServerListener;
use Swoft\Bean\Annotation\Inject;

/**
* Class ServerListenerWrapper
* @package Swoft\Bean\Wrapper
* @author inhere <[email protected]>
*/
class ServerListenerWrapper extends AbstractWrapper
{
/**
* Class annotation
*
* @var array
*/
protected $classAnnotations = [
ServerListener::class,
];

/**
* Property annotations
*
* @var array
*/
protected $propertyAnnotations = [
Inject::class,
];

/**
* 是否解析类注解
*
* @param array $annotations
* @return bool
*/
public function isParseClassAnnotations(array $annotations): bool
{
return isset($annotations[ServerListener::class]);
}

/**
* 是否解析属性注解
*
* @param array $annotations
* @return bool
*/
public function isParsePropertyAnnotations(array $annotations): bool
{
return isset($annotations[Inject::class]);
}

/**
* @param array $annotations
* @return bool
*/
public function isParseMethodAnnotations(array $annotations): bool
{
return false;
}
}
7 changes: 5 additions & 2 deletions src/Bootstrap/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ class Bootstrap implements Bootable
public function bootstrap()
{
$bootstraps = BootstrapCollector::getCollector();
array_multisort(array_column($bootstraps, 'order'), SORT_ASC, $bootstraps);
$temp = \array_column($bootstraps, 'order');

\array_multisort($temp, SORT_ASC, $bootstraps);

foreach ($bootstraps as $bootstrapBeanName => $name){
/* @var Bootable $bootstrap*/
$bootstrap = App::getBean($bootstrapBeanName);
$bootstrap->bootstrap();
}
}
}
}
8 changes: 5 additions & 3 deletions src/Bootstrap/Listeners/BeforeStartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class BeforeStartListener implements BeforeStartInterface
{
/**
* @param AbstractServer $server
* @throws \Swoft\Exception\InvalidArgumentException
*/
public function onBeforeStart(AbstractServer &$server)
{
Expand All @@ -32,10 +33,11 @@ public function onBeforeStart(AbstractServer &$server)

/**
* check task
* @throws \Swoft\Exception\InvalidArgumentException
*/
private function checkTask()
{
$settings = App::getAppProperties()->get("server");
$settings = App::getAppProperties()->get('server');
$settings = $settings['setting'];
$collector = SwooleListenerCollector::getCollector();

Expand All @@ -47,7 +49,7 @@ private function checkTask()
}

if (!$isConfigTask && $isInstallTask) {
throw new InvalidArgumentException("Please set task_worker_num > 0 !");
throw new InvalidArgumentException('Please set task_worker_num > 0 !');
}
}
}
}
14 changes: 14 additions & 0 deletions src/Bootstrap/Listeners/Interfaces/ManagerStartInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Swoft\Bootstrap\Listeners\Interfaces;

use Swoole\Server;

/**
* Interface ManagerStartInterface
* @package Swoft\Bootstrap\Listeners\Interfaces
*/
interface ManagerStartInterface
{
public function onManagerStart(Server $server);
}
14 changes: 14 additions & 0 deletions src/Bootstrap/Listeners/Interfaces/WorkerStartInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Swoft\Bootstrap\Listeners\Interfaces;

use Swoole\Server;

/**
* Interface WorkerStartInterface
* @package Swoft\Bootstrap\Listeners\Interfaces
*/
interface WorkerStartInterface
{
public function onWorkerStart(Server $server, int $workerId, bool $isWorker);
}
Loading

0 comments on commit 5ba081b

Please sign in to comment.