]> Raphaël G. Git Repositories - treebundle/blob - Factory.php
Version 0.0.8
[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\FileUtil;
21 use Rapsys\PackBundle\Util\SluggerUtil;
22 use Rapsys\UserBundle\Factory as BaseFactory;
23
24 use Symfony\Component\HttpFoundation\RequestStack;
25 use Symfony\Component\Routing\RouterInterface;
26 use Symfony\Contracts\Translation\TranslatorInterface;
27
28 /**
29 * This factory is used to create default repository objects for entities at runtime.
30 */
31 class Factory extends BaseFactory {
32 /**
33 * Initializes a new RepositoryFactory instance
34 *
35 * @param ContainerInterface $container The container instance
36 * @param FileUtil $file The FileUtil instance
37 * @param RequestStack $request The request stack
38 * @param RouterInterface $router The router instance
39 * @param SluggerUtil $slugger The SluggerUtil instance
40 * @param TranslatorInterface $translator The TranslatorInterface instance
41 * @param string $locale The current locale
42 * @param array $languages The languages list
43 */
44 public function __construct(protected ContainerInterface $container, protected FileUtil $file, protected RequestStack $request, protected RouterInterface $router, protected SluggerUtil $slugger, protected TranslatorInterface $translator, protected string $locale, protected array $languages) {
45 }
46
47 /**
48 * Create a new repository instance for an entity class
49 *
50 * @param EntityManagerInterface $entityManager The EntityManager instance.
51 * @param string $entityName The name of the entity.
52 */
53 protected function createRepository(EntityManagerInterface $entityManager, string $entityName): ObjectRepository {
54 //Get class metadata
55 $metadata = $entityManager->getClassMetadata($entityName);
56
57 //Get repository class
58 $repositoryClass = $metadata->customRepositoryClassName ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
59
60 //Set to current locale
61 //XXX: current request is not yet populated in constructor
62 $this->locale = $this->request->getCurrentRequest()?->getLocale() ?? $this->locale;
63
64 //Return repository class instance
65 //XXX: router, slugger, translator, languages and locale arguments will be ignored by default
66 return new $repositoryClass($entityManager, $metadata, $this->container, $this->file, $this->router, $this->slugger, $this->translator, $this->locale, $this->languages);
67 }
68 }