1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Factory; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Cycle\ORM\ORMInterface; |
9
|
|
|
use Cycle\ORM\RepositoryInterface; |
10
|
|
|
use Cycle\ORM\SchemaInterface; |
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
use Yiisoft\Di\Container; |
|
|
|
|
13
|
|
|
use Yiisoft\Di\CompositeContainer; |
|
|
|
|
14
|
|
|
use Yiisoft\Di\Contracts\ServiceProviderInterface; |
|
|
|
|
15
|
|
|
|
16
|
|
|
use Yiisoft\Factory\Exception\NotFoundException; |
|
|
|
|
17
|
|
|
use function is_string; |
18
|
|
|
|
19
|
|
|
final class RepositoryContainer implements ContainerInterface |
20
|
|
|
{ |
21
|
|
|
private ORMInterface $orm; |
22
|
|
|
private array $repositoryFactories = []; |
23
|
|
|
private array $instances; |
24
|
|
|
private bool $build = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* RepositoryContainer constructor. |
28
|
|
|
* @param ORMInterface $orm |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ORMInterface $orm) |
31
|
|
|
{ |
32
|
|
|
$this->orm = $orm; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function get($id) |
37
|
|
|
{ |
38
|
|
|
if (isset($this->instances[$id])) { |
39
|
|
|
return $this->instances[$id]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($this->has($id)) { |
43
|
|
|
return $this->instances[$id] = $this->repositoryFactories[$id](); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
throw new NotFoundException("Repository $id doesn't exist."); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function has($id): bool |
50
|
|
|
{ |
51
|
|
|
if (!$this->build) { |
52
|
|
|
$this->makeRepositoryFactories(); |
53
|
|
|
$this->build = true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return isset($this->repositoryFactories[$id]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function makeRepositoryFactories(): void |
60
|
|
|
{ |
61
|
|
|
$schema = $this->orm->getSchema(); |
62
|
|
|
$roles = []; |
63
|
|
|
foreach ($schema->getRoles() as $role) { |
64
|
|
|
$repository = $schema->define($role, SchemaInterface::REPOSITORY); |
65
|
|
|
if (is_string($repository)) { |
66
|
|
|
$roles[$repository][] = $role; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
foreach ($roles as $repo => $role) { |
70
|
|
|
if (count($role) === 1) { |
71
|
|
|
$this->repositoryFactories[$repo] = $this->makeRepositoryFactory(current($role)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @psalm-pure |
78
|
|
|
*/ |
79
|
|
|
private function makeRepositoryFactory(string $role): Closure |
80
|
|
|
{ |
81
|
|
|
$orm = $this->orm; |
82
|
|
|
return static fn (): RepositoryInterface => $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