1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
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 Sonata\TranslationBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Nicolas Bastien <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class AdminExtensionCompilerPass implements CompilerPassInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function process(ContainerBuilder $container) |
27
|
|
|
{ |
28
|
|
|
$translationTargets = $container->getParameter('sonata_translation.targets'); |
29
|
|
|
$adminExtensionReferences = $this->getAdminExtensionReferenceByTypes(array_keys($translationTargets)); |
30
|
|
|
|
31
|
|
|
foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) { |
32
|
|
|
$admin = $container->getDefinition($id); |
33
|
|
|
$modelClass = $container->getParameterBag()->resolveValue($admin->getArgument(1)); |
34
|
|
|
if (!class_exists($modelClass)) { |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
$modelClassReflection = new \ReflectionClass($modelClass); |
38
|
|
|
|
39
|
|
|
foreach ($adminExtensionReferences as $type => $reference) { |
40
|
|
|
foreach ($translationTargets[$type]['implements'] as $interface) { |
41
|
|
|
if ($modelClassReflection->implementsInterface($interface)) { |
42
|
|
|
$admin->addMethodCall('addExtension', array($reference)); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
foreach ($translationTargets[$type]['instanceof'] as $class) { |
46
|
|
|
if ($modelClassReflection->getName() == $class || $modelClassReflection->isSubclassOf($class)) { |
47
|
|
|
$admin->addMethodCall('addExtension', array($reference)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $types |
56
|
|
|
* |
57
|
|
|
* @return Reference[] |
58
|
|
|
*/ |
59
|
|
|
protected function getAdminExtensionReferenceByTypes(array $types) |
60
|
|
|
{ |
61
|
|
|
$references = array(); |
62
|
|
|
foreach ($types as $type) { |
63
|
|
|
$references[$type] = new Reference('sonata_translation.admin.extension.'.$type.'_translatable'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $references; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|