1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral, Core Components |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Spiral\ORM\Schemas; |
9
|
|
|
|
10
|
|
|
use Interop\Container\ContainerInterface; |
11
|
|
|
use Spiral\Core\FactoryInterface; |
12
|
|
|
use Spiral\Models\Reflections\ReflectionEntity; |
13
|
|
|
use Spiral\ORM\Entities\RecordSource; |
14
|
|
|
use Spiral\ORM\RecordEntity; |
15
|
|
|
use Spiral\Tokenizer\ClassesInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides ability to automatically locate schemas in a project. Can be user redefined in order to |
19
|
|
|
* automatically include custom classes. |
20
|
|
|
* |
21
|
|
|
* This is lazy implementation. |
22
|
|
|
*/ |
23
|
|
|
class SchemaLocator implements LocatorInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Container is used for lazy resolution for ClassesInterface. |
27
|
|
|
* |
28
|
|
|
* @var ContainerInterface |
29
|
|
|
*/ |
30
|
|
|
protected $container; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ContainerInterface $container |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ContainerInterface $container) |
36
|
|
|
{ |
37
|
|
|
$this->container = $container; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Locate all available document schemas in a project. |
42
|
|
|
* |
43
|
|
|
* @return SchemaInterface[] |
44
|
|
|
*/ |
45
|
|
|
public function locateSchemas(): array |
46
|
|
|
{ |
47
|
|
|
if (!$this->container->has(ClassesInterface::class)) { |
48
|
|
|
return []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ClassesInterface $classes |
53
|
|
|
*/ |
54
|
|
|
$classes = $this->container->get(ClassesInterface::class); |
55
|
|
|
|
56
|
|
|
$schemas = []; |
57
|
|
|
foreach ($classes->getClasses(RecordEntity::class) as $class) { |
58
|
|
|
if ($class['abstract']) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$schemas[] = $this->container->get(FactoryInterface::class)->make( |
63
|
|
|
RecordSchema::class, |
64
|
|
|
['reflection' => new ReflectionEntity($class['name']),] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $schemas; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Locate all DocumentSources defined by user. Must return values in a form of |
73
|
|
|
* Document::class => Source::class. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function locateSources(): array |
78
|
|
|
{ |
79
|
|
|
if (!$this->container->has(ClassesInterface::class)) { |
80
|
|
|
return []; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var ClassesInterface $classes |
85
|
|
|
*/ |
86
|
|
|
$classes = $this->container->get(ClassesInterface::class); |
87
|
|
|
|
88
|
|
|
$result = []; |
89
|
|
|
foreach ($classes->getClasses(RecordSource::class) as $class) { |
90
|
|
|
$source = $class['name']; |
91
|
|
|
if ($class['abstract'] || empty($source::RECORD)) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$result[$source::RECORD] = $source; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
} |