|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
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 Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Doctrine; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Bundle\ResourceBundle\Doctrine\ODM\MongoDB\TranslatableRepository; |
|
15
|
|
|
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; |
|
16
|
|
|
use Sylius\Component\Resource\Metadata\MetadataInterface; |
|
17
|
|
|
use Sylius\Component\Resource\Model\TranslatableInterface; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Parameter; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
25
|
|
|
* @author Arnaud Langlade <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class DoctrineODMDriver extends AbstractDoctrineDriver |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getType() |
|
33
|
|
|
{ |
|
34
|
|
|
return SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata) |
|
41
|
|
|
{ |
|
42
|
|
|
$modelClass = $metadata->getClass('model'); |
|
43
|
|
|
|
|
44
|
|
|
$repositoryClass = in_array(TranslatableInterface::class, class_implements($modelClass)) |
|
45
|
|
|
? TranslatableRepository::class |
|
46
|
|
|
: new Parameter('sylius.mongodb.odm.repository.class') |
|
47
|
|
|
; |
|
48
|
|
|
|
|
49
|
|
|
if ($metadata->hasClass('repository')) { |
|
50
|
|
|
$repositoryClass = $metadata->getClass('repository'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$unitOfWorkDefinition = new Definition('Doctrine\\ODM\\MongoDB\\UnitOfWork'); |
|
54
|
|
|
$unitOfWorkDefinition |
|
55
|
|
|
->setFactory([new Reference($this->getManagerServiceId($metadata)), 'getUnitOfWork']) |
|
56
|
|
|
->setPublic(false) |
|
57
|
|
|
; |
|
58
|
|
|
|
|
59
|
|
|
$definition = new Definition($repositoryClass); |
|
60
|
|
|
$definition->setArguments([ |
|
61
|
|
|
new Reference($metadata->getServiceId('manager')), |
|
62
|
|
|
$unitOfWorkDefinition, |
|
63
|
|
|
$this->getClassMetadataDefinition($metadata), |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$container->setDefinition($metadata->getServiceId('repository'), $definition); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getManagerServiceId(MetadataInterface $metadata) |
|
73
|
|
|
{ |
|
74
|
|
|
if ($objectManagerName = $this->getObjectManagerName($metadata)) { |
|
75
|
|
|
return sprintf('doctrine_mongodb.odm.%s_document_manager', $objectManagerName); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return 'doctrine_mongodb.odm.document_manager'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function getClassMetadataClassname() |
|
85
|
|
|
{ |
|
86
|
|
|
return 'Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata'; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|