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; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
13
|
|
|
use Railt\SDL\Exception\NotFoundException; |
14
|
|
|
use Railt\SDL\Frontend\Builder; |
15
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
16
|
|
|
use Railt\SDL\Frontend\Deferred\DeferredInterface; |
17
|
|
|
use Railt\SDL\Frontend\Deferred\Storage; |
18
|
|
|
use Railt\SDL\Frontend\Invocation\InvocationPrimitive; |
19
|
|
|
use Railt\SDL\IR\SymbolTable\PrimitiveInterface; |
20
|
|
|
use Railt\SDL\IR\Type\TypeNameInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class BaseBuilder |
24
|
|
|
*/ |
25
|
|
|
abstract class BaseBuilder implements BuilderInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Builder |
29
|
|
|
*/ |
30
|
|
|
private $builder; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Storage |
34
|
|
|
*/ |
35
|
|
|
private $store; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* BaseBuilder constructor. |
39
|
|
|
* @param Builder $builder |
40
|
|
|
* @param Storage $store |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Builder $builder, Storage $store) |
43
|
|
|
{ |
44
|
|
|
$this->builder = $builder; |
45
|
|
|
$this->store = $store; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Readable $file |
50
|
|
|
* @return iterable |
51
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
52
|
|
|
* @throws \Railt\SDL\Exception\SyntaxException |
53
|
|
|
*/ |
54
|
|
|
protected function loadFile(Readable $file) |
55
|
|
|
{ |
56
|
|
|
return $this->builder->buildFile($file); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param InvocationPrimitive|PrimitiveInterface $invocation |
61
|
|
|
* @param ContextInterface $context |
62
|
|
|
* @return mixed |
63
|
|
|
* @throws NotFoundException |
64
|
|
|
*/ |
65
|
|
|
protected function invoke(InvocationPrimitive $invocation, ContextInterface $context) |
66
|
|
|
{ |
67
|
|
|
$deferred = $this->loadType($invocation->getName(), $context); |
68
|
|
|
|
69
|
|
|
if ($deferred === null) { |
70
|
|
|
$error = 'Type %s not found or could not be loaded'; |
71
|
|
|
throw new NotFoundException(\sprintf($error, $invocation->getName())); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $deferred->invoke($invocation, $context); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param TypeNameInterface $name |
79
|
|
|
* @param ContextInterface|null $ctx |
80
|
|
|
* @return DeferredInterface|null |
81
|
|
|
*/ |
82
|
|
|
protected function loadType(TypeNameInterface $name, ContextInterface $ctx = null): ?DeferredInterface |
83
|
|
|
{ |
84
|
|
|
if ($ctx && $result = $this->store->first($name->in($ctx->getName()))) { |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->store->first($name); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|