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\SDL\Frontend\Builder\Definition; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
14
|
|
|
use Railt\Reflection\Contracts\Definition\SchemaDefinition; |
15
|
|
|
use Railt\SDL\Frontend\Ast\Definition\ObjectDefinitionNode; |
16
|
|
|
use Railt\SDL\Frontend\Ast\Definition\SchemaDefinitionNode; |
17
|
|
|
use Railt\SDL\Frontend\Builder\DefinitionBuilder; |
18
|
|
|
use Railt\SDL\IR\Type; |
19
|
|
|
use Railt\SDL\IR\TypeDefinition; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class SchemaBuilder |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
class SchemaBuilder extends DefinitionBuilder |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param Readable $file |
28
|
|
|
* @param RuleInterface|SchemaDefinitionNode $ast |
29
|
|
|
* @return \Generator|mixed |
30
|
|
|
*/ |
31
|
|
|
public function build(Readable $file, RuleInterface $ast) |
32
|
|
|
{ |
33
|
|
|
// TODO detach from reflection component |
34
|
|
|
$schema = new TypeDefinition($ast->getFullName() ?: SchemaDefinition::DEFAULT_SCHEMA_NAME); |
|
|
|
|
35
|
|
|
$schema->in($file, $ast->getOffset()); |
36
|
|
|
|
37
|
|
|
$schema->type = Type::SCHEMA; |
38
|
|
|
$schema->description = $ast->getDescription(); |
|
|
|
|
39
|
|
|
|
40
|
|
|
yield from $this->loadSchemaFields($ast, $schema); |
|
|
|
|
41
|
|
|
|
42
|
|
|
return $schema; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param SchemaDefinitionNode $ast |
47
|
|
|
* @param TypeDefinition $schema |
48
|
|
|
* @return \Generator |
49
|
|
|
*/ |
50
|
|
|
protected function loadSchemaFields(SchemaDefinitionNode $ast, TypeDefinition $schema): \Generator |
51
|
|
|
{ |
52
|
|
|
$schema->fields = []; |
53
|
|
|
|
54
|
|
|
foreach ($ast->getSchemaFields() as $field) { |
55
|
|
|
$schema->fields[] = yield $field; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.