Optimized remove unnecessary collector

This commit is contained in:
张城铭 2019-07-11 19:27:16 +08:00
parent a1d498de9f
commit 82f7548224
5 changed files with 21 additions and 47 deletions

View File

@ -57,4 +57,17 @@ class AnnotationCollector extends MetadataCollector
{
return static::get($class . '._m.' . $method);
}
public static function getMethodByAnnotation(string $annotation): array
{
$result = [];
foreach (static::$container as $class => $metadata) {
foreach ($metadata['_m'] ?? [] as $method => $_metadata) {
if ($value = $_metadata[$annotation] ?? null) {
$result[] = ['class' => $class, 'method' => $method, 'annotation' => $value];
}
}
}
return $result;
}
}

View File

@ -30,13 +30,11 @@ trait AnnotationTrait
public function collectClass(string $className): void
{
GraphQLCollector::collectClass(static::class, $className);
AnnotationCollector::collectClass($className, static::class, $this);
}
public function collectMethod(string $className, ?string $target): void
{
GraphQLCollector::collectMethod(static::class, $className, $target);
AnnotationCollector::collectMethod($className, $target, static::class, $this);
}

View File

@ -1,39 +0,0 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\GraphQL\Annotation;
use Hyperf\Di\MetadataCollector;
class GraphQLCollector extends MetadataCollector
{
public static function collectMethod(string $annotation, string $class, string $method)
{
static::$container[$annotation][] = compact('class', 'method');
}
public static function collectClass(string $annotation, string $class)
{
static::$container[$annotation][] = $class;
}
public static function getClassByAnnotation(string $annotation)
{
$data = static::$container[$annotation] ?? [];
return array_column($data, 'class');
}
public static function getClass(string $annotation)
{
return static::$container[$annotation] ?? [];
}
}

View File

@ -12,7 +12,7 @@ declare(strict_types=1);
namespace Hyperf\GraphQL;
use Hyperf\GraphQL\Annotation\GraphQLCollector;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\GraphQL\Annotation\Mutation;
use Hyperf\GraphQL\Annotation\Query;
use Psr\Container\ContainerInterface;
@ -50,7 +50,8 @@ class QueryProvider implements QueryProviderInterface
public function getQueries(): array
{
$queryList = [];
$classes = GraphQLCollector::getClassByAnnotation(Query::class);
$classes = AnnotationCollector::getMethodByAnnotation(Query::class);
$classes = array_unique(array_column($classes, 'class'));
foreach ($classes as $className) {
$fieldsBuilder = $this->fieldsBuilderFactory->buildFieldsBuilder($this->recursiveTypeMapper);
$queryList = array_merge($queryList, $fieldsBuilder->getQueries($this->container->get($className)));
@ -64,7 +65,8 @@ class QueryProvider implements QueryProviderInterface
public function getMutations(): array
{
$mutationList = [];
$classes = GraphQLCollector::getClassByAnnotation(Mutation::class);
$classes = AnnotationCollector::getMethodByAnnotation(Mutation::class);
$classes = array_unique(array_column($classes, 'class'));
foreach ($classes as $className) {
$fieldsBuilder = $this->fieldsBuilderFactory->buildFieldsBuilder($this->recursiveTypeMapper);
$mutationList = array_merge($mutationList, $fieldsBuilder->getMutations($this->container->get($className)));

View File

@ -14,7 +14,7 @@ namespace Hyperf\GraphQL;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\OutputType;
use Hyperf\GraphQL\Annotation\GraphQLCollector;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\GraphQL\Annotation\Type;
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface;
@ -571,8 +571,8 @@ class TypeMapper implements TypeMapperInterface
{
if ($this->classes === null) {
$this->classes = [];
$classes = GraphQLCollector::getClass(Type::class);
foreach ($classes as $className) {
$classes = AnnotationCollector::getClassByAnnotation(Type::class);
foreach (array_keys($classes) as $className) {
if (! \class_exists($className)) {
continue;
}