|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation; |
|
12
|
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
|
14
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Common trait used un multiple parsers |
|
18
|
|
|
*/ |
|
19
|
|
|
trait AnnotationParserHelper |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Get default name based in given class using naming convention |
|
23
|
|
|
* |
|
24
|
|
|
* @param \ReflectionClass $refClass |
|
25
|
|
|
* @param DefinitionManager $definitionManager |
|
26
|
|
|
* |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function getDefaultName(\ReflectionClass $refClass, DefinitionManager $definitionManager): string |
|
30
|
|
|
{ |
|
31
|
1 |
|
if ($definitionManager->hasTypeForClass($refClass->getName())) { |
|
32
|
1 |
|
return $definitionManager->getTypeForClass($refClass->getName()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
preg_match('/\w+$/', $refClass->getName(), $matches); |
|
36
|
|
|
|
|
37
|
1 |
|
return lcfirst($matches[0] ?? ''); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get object type using naming convention |
|
42
|
|
|
* if Query is placed under User\AllUsers namespace, then "User" is the object type |
|
43
|
|
|
* |
|
44
|
|
|
* Mutation\User\UpdateUser -> User |
|
45
|
|
|
* Query\User\Users -> User |
|
46
|
|
|
* Form\Input\User\AddUserInput -> User |
|
47
|
|
|
* |
|
48
|
|
|
* @param \ReflectionClass $refClass |
|
49
|
|
|
* @param DefinitionManager $definitionManager |
|
50
|
|
|
* |
|
51
|
|
|
* @return ObjectDefinitionInterface |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function getObjectDefinition(\ReflectionClass $refClass, DefinitionManager $definitionManager): ObjectDefinitionInterface |
|
54
|
|
|
{ |
|
55
|
1 |
|
if ($definitionManager->hasTypeForClass($refClass->getName())) { |
|
56
|
1 |
|
return $definitionManager->getType($definitionManager->getTypeForClass($refClass->getName())); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$objectType = null; |
|
|
|
|
|
|
60
|
1 |
|
preg_match('/(\w+)(\\\\w+)?\\\\(\w+)$/', $refClass->getName(), $matches); |
|
61
|
1 |
View Code Duplication |
if (!isset($matches[1]) || !$definitionManager->hasType($matches[1])) { |
|
|
|
|
|
|
62
|
|
|
$error = sprintf('Can`t resolve a valid object type for "%s"', $refClass->getName()); |
|
63
|
|
|
throw new \RuntimeException($error); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
return $definitionManager->getType($matches[1]); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|