1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Factory; |
6
|
|
|
|
7
|
|
|
use Cycle\ORM\ORMInterface; |
8
|
|
|
use Cycle\ORM\RepositoryInterface; |
9
|
|
|
use Cycle\ORM\SchemaInterface; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use Yiisoft\Definitions\Exception\NotFoundException; |
|
|
|
|
12
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableClassException; |
|
|
|
|
13
|
|
|
|
14
|
|
|
use function is_string; |
15
|
|
|
|
16
|
|
|
final class RepositoryContainer implements ContainerInterface |
17
|
|
|
{ |
18
|
|
|
private ContainerInterface $rootContainer; |
19
|
|
|
private ORMInterface $orm; |
20
|
|
|
|
21
|
|
|
private bool $rolesBuilt = false; |
22
|
|
|
private array $roles = []; |
23
|
|
|
|
24
|
|
|
private array $instances; |
25
|
|
|
|
26
|
|
|
public function __construct(ContainerInterface $rootContainer) |
27
|
|
|
{ |
28
|
|
|
$this->rootContainer = $rootContainer; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function get($id) |
32
|
|
|
{ |
33
|
|
|
if (isset($this->instances[$id])) { |
34
|
|
|
return $this->instances[$id]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($this->has($id)) { |
38
|
|
|
return $this->instances[$id] = $this->makeRepository($this->roles[$id]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
throw new NotFoundException($id); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function has($id): bool |
45
|
|
|
{ |
46
|
|
|
if (!is_subclass_of($id, RepositoryInterface::class)) { |
47
|
|
|
throw new NotInstantiableClassException( |
48
|
|
|
$id, |
49
|
|
|
sprintf('Can not instantiate "%s" because it is not a subclass of "%s".', $id, RepositoryInterface::class) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!$this->rolesBuilt) { |
54
|
|
|
$this->makeRepositoryList(); |
55
|
|
|
$this->rolesBuilt = true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return isset($this->roles[$id]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function makeRepositoryList(): void |
62
|
|
|
{ |
63
|
|
|
/** @var ORMInterface */ |
64
|
|
|
$this->orm = $this->rootContainer->get(ORMInterface::class); |
|
|
|
|
65
|
|
|
$schema = $this->orm->getSchema(); |
66
|
|
|
$roles = []; |
67
|
|
|
foreach ($schema->getRoles() as $role) { |
68
|
|
|
$repository = $schema->define($role, SchemaInterface::REPOSITORY); |
69
|
|
|
if (is_string($repository)) { |
70
|
|
|
$roles[$repository][] = $role; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
foreach ($roles as $repository => $role) { |
74
|
|
|
if (count($role) === 1) { |
75
|
|
|
$this->roles[$repository] = current($role); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function makeRepository(string $role): RepositoryInterface |
81
|
|
|
{ |
82
|
|
|
return $this->orm->getRepository($role); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths