Completed
Push — master ( b291f7...8eacf5 )
by Julián
04:50
created

src/RelationalRepositoryFactory.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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
This use statement conflicts with another class in this namespace, Jgut\Doctrine\Repository\ORM\RelationalRepository.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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