]> Raphaël G. Git Repositories - treebundle/blob - Factory.php
Add bundle user repository
[treebundle] / Factory.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys TreeBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\TreeBundle;
13
14 use Doctrine\ORM\EntityManagerInterface;
15 use Doctrine\ORM\Repository\RepositoryFactory;
16 use Doctrine\Persistence\ObjectRepository;
17
18 use Psr\Container\ContainerInterface;
19
20 use Rapsys\PackBundle\Util\SluggerUtil;
21 use Rapsys\UserBundle\Factory as BaseFactory;
22
23 use Symfony\Component\HttpFoundation\RequestStack;
24 use Symfony\Component\Routing\RouterInterface;
25 use Symfony\Contracts\Translation\TranslatorInterface;
26
27 /**
28 * This factory is used to create default repository objects for entities at runtime.
29 */
30 final class Factory extends BaseFactory {
31 /**
32 * Initializes a new RepositoryFactory instance
33 *
34 * @param ContainerInterface $container The container instance
35 * @param RequestStack $request The request stack
36 * @param RouterInterface $router The router instance
37 * @param SluggerUtil $slugger The SluggerUtil instance
38 * @param TranslatorInterface $translator The TranslatorInterface instance
39 * @param string $locale The current locale
40 * @param array $languages The languages list
41 */
42 public function __construct(private ContainerInterface $container, private RequestStack $request, private RouterInterface $router, private SluggerUtil $slugger, private TranslatorInterface $translator, private string $locale, private array $languages) {
43 }
44
45 /**
46 * Create a new repository instance for an entity class
47 *
48 * @param EntityManagerInterface $entityManager The EntityManager instance.
49 * @param string $entityName The name of the entity.
50 */
51 protected function createRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository {
52 //Get class metadata
53 $metadata = $entityManager->getClassMetadata($entityName);
54
55 //Get repository class
56 $repositoryClass = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
57
58 //Set to current locale
59 //XXX: current request is not yet populated in constructor
60 $this->locale = $this->request->getCurrentRequest()?->getLocale() ?? $this->locale;
61
62 //Return repository class instance
63 //XXX: router, slugger, translator, languages and locale arguments will be ignored by default
64 return new $repositoryClass($entityManager, $metadata, $this->container, $this->router, $this->slugger, $this->translator, $this->locale, $this->languages);
65 }
66 }