Add more mapping annotations

This commit is contained in:
huangzhhui 2018-12-30 22:32:39 +08:00
parent 940105c0f8
commit dda195767d
11 changed files with 243 additions and 21 deletions

View File

@ -20,6 +20,10 @@ use Hyperf\Di\Aop\ArroundInterface;
*/
class Aspect extends AbstractAnnotation
{
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
// @TODO Add order property.

View File

@ -31,12 +31,13 @@ class Inject extends AbstractAnnotation
$this->docReader = new PhpDocReader();
}
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
if (! $this->value) {
if (null !== $this->value) {
$this->value = $this->docReader->getPropertyClass(ReflectionManager::reflectClass($className)->getProperty($target));
}
if (isset($this->value)) {
AnnotationCollector::collectProperty($className, $target, static::class, $this->value);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Hyperf\HttpServer\Annotation;
use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\Di\Annotation\AnnotationCollector;
/**
* @Annotation
* @Target({"METHOD"})
*/
class DeleteMapping extends AbstractAnnotation
{
/**
* @var string
*/
public $path;
public function __construct($value = null)
{
parent::__construct($value);
if (isset($value['path'])) {
$this->path = $value['path'];
}
}
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
if ($this->methods && $this->path) {
AnnotationCollector::collectMethod($className, $target, static::class, [
'methods' => ['DELETE'],
'path' => $this->path,
]);
}
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Hyperf\HttpServer\Annotation;
use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\Di\Annotation\AnnotationCollector;
/**
* @Annotation
* @Target({"METHOD"})
*/
class GetMapping extends AbstractAnnotation
{
/**
* @var string
*/
public $path;
public function __construct($value = null)
{
parent::__construct($value);
if (isset($value['path'])) {
$this->path = $value['path'];
}
}
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
if ($this->methods && $this->path) {
AnnotationCollector::collectMethod($className, $target, static::class, [
'methods' => ['GET'],
'path' => $this->path,
]);
}
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Hyperf\HttpServer\Annotation;
use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\Di\Annotation\AnnotationCollector;
/**
* @Annotation
* @Target({"METHOD"})
*/
class PatchMapping extends AbstractAnnotation
{
/**
* @var string
*/
public $path;
public function __construct($value = null)
{
parent::__construct($value);
if (isset($value['path'])) {
$this->path = $value['path'];
}
}
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
if ($this->methods && $this->path) {
AnnotationCollector::collectMethod($className, $target, static::class, [
'methods' => ['PATCH'],
'path' => $this->path,
]);
}
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Hyperf\HttpServer\Annotation;
use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\Di\Annotation\AnnotationCollector;
/**
* @Annotation
* @Target({"METHOD"})
*/
class PostMapping extends AbstractAnnotation
{
/**
* @var string
*/
public $path;
public function __construct($value = null)
{
parent::__construct($value);
if (isset($value['path'])) {
$this->path = $value['path'];
}
}
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
if ($this->methods && $this->path) {
AnnotationCollector::collectMethod($className, $target, static::class, [
'methods' => ['POST'],
'path' => $this->path,
]);
}
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Hyperf\HttpServer\Annotation;
use Hyperf\Di\Annotation\AbstractAnnotation;
use Hyperf\Di\Annotation\AnnotationCollector;
/**
* @Annotation
* @Target({"METHOD"})
*/
class PutMapping extends AbstractAnnotation
{
/**
* @var string
*/
public $path;
public function __construct($value = null)
{
parent::__construct($value);
if (isset($value['path'])) {
$this->path = $value['path'];
}
}
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
if ($this->methods && $this->path) {
AnnotationCollector::collectMethod($className, $target, static::class, [
'methods' => ['PUT'],
'path' => $this->path,
]);
}
}
}

View File

@ -33,7 +33,7 @@ class RequestMapping extends AbstractAnnotation
public function __construct($value = null)
{
$this->value = $value;
parent::__construct($value);
if (isset($value['methods'])) {
// Explode a string to a array
$this->methods = explode(',', Str::upper(str_replace(' ', '', $value['methods'])));
@ -43,6 +43,9 @@ class RequestMapping extends AbstractAnnotation
}
}
/**
* {@inheritDoc}
*/
public function collect(string $className, ?string $target): void
{
if ($this->methods && $this->path) {

View File

@ -21,6 +21,11 @@ use Hyperf\Di\Exception\ConflictAnnotationException;
use Hyperf\Di\ReflectionManager;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PatchMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Utils\Str;
use Psr\Container\ContainerInterface;
@ -128,8 +133,17 @@ class DispatcherFactory
$router->addGroup($prefix, function ($router) use ($className, $methodMetadata) {
foreach ($methodMetadata as $method => $values) {
if (isset($values[RequestMapping::class])) {
$item = $values[RequestMapping::class];
$mappingAnnotations = [
RequestMapping::class,
GetMapping::class,
PostMapping::class,
PutMapping::class,
PatchMapping::class,
DeleteMapping::class,
];
foreach ($mappingAnnotations as $mappingAnnotation) {
if (isset($values[$mappingAnnotation])) {
$item = $values[$mappingAnnotation];
if ($item['path'][0] !== '/') {
$item['path'] = '/' . $item['path'];
}
@ -139,6 +153,7 @@ class DispatcherFactory
]);
}
}
}
});
}

View File

@ -39,6 +39,9 @@ abstract class Pool implements PoolInterface
*/
protected $option;
/**
* @var int
*/
protected $currentConnections = 0;
public function __construct(ContainerInterface $container)
@ -77,9 +80,6 @@ abstract class Pool implements PoolInterface
}
}
/**
* @return int
*/
public function getCurrentConnections(): int
{
return $this->currentConnections;

View File

@ -101,17 +101,11 @@ class PoolOption implements PoolOptionInterface
return $this;
}
/**
* @return float
*/
public function getWaitTimeout(): float
{
return $this->waitTimeout;
}
/**
* @param float $waitTimeout
*/
public function setWaitTimeout(float $waitTimeout): self
{
$this->waitTimeout = $waitTimeout;