1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Reflection\Definition; |
11
|
|
|
|
12
|
|
|
use Railt\Reflection\AbstractTypeDefinition; |
13
|
|
|
use Railt\Reflection\Contracts\Definition\ObjectDefinition; |
|
|
|
|
14
|
|
|
use Railt\Reflection\Contracts\Definition\SchemaDefinition as SchemaDefinitionInterface; |
15
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
16
|
|
|
use Railt\Reflection\Contracts\Type as TypeInterface; |
17
|
|
|
use Railt\Reflection\Document; |
18
|
|
|
use Railt\Reflection\Type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class SchemaDefinition |
22
|
|
|
*/ |
23
|
|
|
class SchemaDefinition extends AbstractTypeDefinition implements SchemaDefinitionInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $query; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
protected $mutation; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string|null |
37
|
|
|
*/ |
38
|
|
|
protected $subscription; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* SchemaDefinition constructor. |
42
|
|
|
* @param Document $document |
43
|
|
|
* @param string|null $name |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Document $document, string $name = null) |
46
|
|
|
{ |
47
|
|
|
parent::__construct($document, $name ?? self::DEFAULT_SCHEMA_NAME); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return TypeInterface |
52
|
|
|
*/ |
53
|
8 |
|
public static function getType(): TypeInterface |
54
|
|
|
{ |
55
|
8 |
|
return Type::of(Type::SCHEMA); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ObjectDefinition $query |
60
|
|
|
* @return SchemaDefinition |
61
|
|
|
*/ |
62
|
|
|
public function withQuery(ObjectDefinition $query): SchemaDefinition |
63
|
|
|
{ |
64
|
|
|
$this->query = $query->getName(); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return ObjectDefinition|TypeDefinition |
71
|
|
|
*/ |
72
|
|
|
public function getQuery(): ObjectDefinition |
73
|
|
|
{ |
74
|
|
|
return $this->fetch($this->query); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ObjectDefinition|null $mutation |
79
|
|
|
* @return SchemaDefinition |
80
|
|
|
*/ |
81
|
|
|
public function withMutation(?ObjectDefinition $mutation): SchemaDefinition |
82
|
|
|
{ |
83
|
|
|
$this->mutation = $mutation ? $mutation->getName() : null; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return null|ObjectDefinition|TypeDefinition |
90
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
91
|
|
|
*/ |
92
|
|
|
public function getMutation(): ?ObjectDefinition |
93
|
|
|
{ |
94
|
|
|
return $this->fetchOrNull($this->mutation); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function hasMutation(): bool |
101
|
|
|
{ |
102
|
|
|
return $this->mutation !== null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param ObjectDefinition|null $subscription |
107
|
|
|
* @return SchemaDefinition |
108
|
|
|
*/ |
109
|
|
|
public function withSubscription(?ObjectDefinition $subscription): SchemaDefinition |
110
|
|
|
{ |
111
|
|
|
$this->subscription = $subscription ? $subscription->getName() : null; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return null|ObjectDefinition|TypeDefinition |
118
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
119
|
|
|
*/ |
120
|
|
|
public function getSubscription(): ?ObjectDefinition |
121
|
|
|
{ |
122
|
|
|
return $this->fetchOrNull($this->subscription); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function hasSubscription(): bool |
129
|
|
|
{ |
130
|
|
|
return $this->subscription !== null; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: