These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * doctrine-orm-repositories (https://github.com/juliangut/doctrine-orm-repositories). |
||
5 | * Doctrine2 ORM utility entity repositories. |
||
6 | * |
||
7 | * @license MIT |
||
8 | * @link https://github.com/juliangut/doctrine-orm-repositories |
||
9 | * @author Julián Gutiérrez <[email protected]> |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Jgut\Doctrine\Repository\ORM; |
||
15 | |||
16 | use Doctrine\ORM\EntityManager; |
||
17 | use Doctrine\ORM\EntityManagerInterface; |
||
18 | use Doctrine\ORM\Repository\RepositoryFactory; |
||
19 | use Jgut\Doctrine\Repository\Factory\AbstractRepositoryFactory; |
||
20 | use Jgut\Doctrine\Repository\RelationalRepository; |
||
0 ignored issues
–
show
|
|||
21 | |||
22 | /** |
||
23 | * Relational entity repository factory. |
||
24 | */ |
||
25 | class RelationalRepositoryFactory extends AbstractRepositoryFactory implements RepositoryFactory |
||
26 | { |
||
27 | /** |
||
28 | * The list of EntityRepository instances. |
||
29 | * |
||
30 | * @var \Doctrine\Common\Persistence\ObjectRepository[] |
||
31 | */ |
||
32 | private $repositoryList = []; |
||
33 | |||
34 | /** |
||
35 | * RelationalRepositoryFactory constructor. |
||
36 | */ |
||
37 | public function __construct() |
||
38 | { |
||
39 | parent::__construct(RelationalRepository::class); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function getRepository(EntityManagerInterface $entityManager, $entityName) |
||
46 | { |
||
47 | /* @var \Doctrine\ORM\EntityManager $entityManager */ |
||
48 | $repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager); |
||
49 | |||
50 | if (array_key_exists($repositoryHash, $this->repositoryList)) { |
||
51 | return $this->repositoryList[$repositoryHash]; |
||
52 | } |
||
53 | |||
54 | $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName); |
||
55 | |||
56 | return $this->repositoryList[$repositoryHash]; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Create a new repository instance for an entity class. |
||
61 | * |
||
62 | * @param EntityManager $entityManager |
||
63 | * @param string $entityName |
||
64 | * |
||
65 | * @return \Doctrine\Common\Persistence\ObjectRepository |
||
66 | */ |
||
67 | private function createRepository(EntityManager $entityManager, $entityName) |
||
68 | { |
||
69 | $metadata = $entityManager->getClassMetadata($entityName); |
||
70 | $repositoryClassName = $metadata->customRepositoryClassName ?: $this->getDefaultRepositoryClassName(); |
||
71 | |||
72 | return new $repositoryClassName($entityManager, $metadata); |
||
73 | } |
||
74 | } |
||
75 |
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: