Completed
Branch feature/split-orm (fa2f8e)
by Anton
03:35
created

SchemaLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ODM\Schemas;
8
9
use Interop\Container\ContainerInterface;
10
use Spiral\Models\Reflections\ReflectionEntity;
11
use Spiral\ODM\Configs\MutatorsConfig;
12
use Spiral\ODM\DocumentEntity;
13
use Spiral\Tokenizer\ClassesInterface;
14
15
/**
16
 * Provides ability to automatically locate schemas in a project. Can be user redefined in order to
17
 * automatically include custom classes.
18
 */
19
class SchemaLocator
20
{
21
    /**
22
     * Container is used for lazy resolution for ClassesInterface.
23
     *
24
     * @var ContainerInterface
25
     */
26
    protected $container;
27
28
    /**
29
     * @param ContainerInterface $container
30
     */
31
    public function __construct(ContainerInterface $container)
32
    {
33
        $this->container = $container;
34
    }
35
36
    /**
37
     * Locate all available document schemas in a project.
38
     *
39
     * @return SchemaInterface[]
40
     */
41
    public function locateSchemas(): array
42
    {
43
        if (!$this->container->has(ClassesInterface::class)) {
44
            return [];
45
        }
46
47
        /**
48
         * @var ClassesInterface $classes
49
         */
50
        $classes = $this->container->get(ClassesInterface::class);
51
52
        $schemas = [];
53
        foreach ($classes->getClasses(DocumentEntity::class) as $class) {
54
            if ($class['abstract']) {
55
                continue;
56
            }
57
58
            $schemas[] = new DocumentSchema(
59
                new ReflectionEntity($class['name']),
60
                $this->container->get(MutatorsConfig::class)
61
            );
62
        }
63
64
        return $schemas;
65
    }
66
}