1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\ElasticSearch\Entity; |
4
|
|
|
|
5
|
|
|
use AmaTeam\ElasticSearch\API\Entity; |
6
|
|
|
use AmaTeam\ElasticSearch\API\Entity\AssemblerInterface; |
7
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Descriptor\Hierarchy\CompilerInterface; |
8
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Descriptor\Hierarchy\BuilderInterface; |
9
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Descriptor\ManagerInterface; |
10
|
|
|
use AmaTeam\ElasticSearch\API\Entity\DescriptorInterface; |
11
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\Conversion\DefaultContext; |
12
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Mapping\ConverterInterface; |
13
|
|
|
use AmaTeam\ElasticSearch\API\EntityInterface; |
14
|
|
|
use AmaTeam\ElasticSearch\Entity\Descriptor\Compiler; |
15
|
|
|
use AmaTeam\ElasticSearch\Entity\Descriptor\Hierarchy\Builder; |
16
|
|
|
use AmaTeam\ElasticSearch\Entity\Descriptor\Operations; |
|
|
|
|
17
|
|
|
use AmaTeam\ElasticSearch\Entity\Mapping\Converter; |
18
|
|
|
|
19
|
|
|
class Assembler implements AssemblerInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var BuilderInterface |
23
|
|
|
*/ |
24
|
|
|
private $hierarchyBuilder; |
25
|
|
|
/** |
26
|
|
|
* @var CompilerInterface |
27
|
|
|
*/ |
28
|
|
|
private $compiler; |
29
|
|
|
/** |
30
|
|
|
* @var ConverterInterface |
31
|
|
|
*/ |
32
|
|
|
private $converter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ManagerInterface $manager |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ManagerInterface $manager) { |
38
|
|
|
$this->hierarchyBuilder = new Builder($manager); |
39
|
|
|
$this->compiler = new Compiler(); |
40
|
|
|
$this->converter = new Converter($manager); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function assemble(DescriptorInterface $descriptor): EntityInterface |
44
|
|
|
{ |
45
|
|
|
$copy = $this->prepare($descriptor); |
46
|
|
|
$hierarchy = $this->hierarchyBuilder->build($copy); |
47
|
|
|
$compiled = $this->compiler->compile($hierarchy); |
48
|
|
|
$context = (new DefaultContext())->setRootLevelMapping(true); |
49
|
|
|
$mapping = $this->converter->convert($compiled->getMapping(), $context); |
50
|
|
|
return (new Entity($descriptor->getName())) |
51
|
|
|
->setParentNames($compiled->getParentNames()) |
52
|
|
|
->setRootLevelDocument($compiled->isRootLevelDocument()) |
53
|
|
|
->setOriginalDescriptor(Operations::from($descriptor)) |
54
|
|
|
->setCompiledDescriptor($compiled) |
55
|
|
|
->setHierarchy($hierarchy) |
56
|
|
|
->setMapping($mapping) |
57
|
|
|
->setIndexing($compiled->getIndexing()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function prepare(DescriptorInterface $descriptor): DescriptorInterface |
61
|
|
|
{ |
62
|
|
|
$copy = Operations::from($descriptor); |
63
|
|
|
if ($copy->getParentNames() === null) { |
64
|
|
|
$parent = get_parent_class($descriptor->getName()); |
65
|
|
|
$copy->setParentNames($parent ? [$parent] : []); |
66
|
|
|
} |
67
|
|
|
return $copy; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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: