-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoServiceCompilerPass.php
133 lines (110 loc) · 5.18 KB
/
AutoServiceCompilerPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Created by PhpStorm.
* User: Mike Meier <mike.meier@ibrows.ch>
* Date: 10.10.14
* Time: 15:57
*/
namespace Ibrows\Bundle\SonataAdminAnnotationBundle;
use Ibrows\AnnotationReader\AnnotationReaderInterface;
use Ibrows\Bundle\SonataAdminAnnotationBundle\Annotation\AutoService;
use mikemeier\MoserArtzBundle\Entity\User;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class AutoServiceCompilerPass implements CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*
* @api
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('ibrows_sonata_admin_annotation.autoservice')) {
return;
}
$configuration = $container->getParameter('ibrows_sonata_admin_annotation.autoservice');
$entityConfigs = isset($configuration['entities']) ? $configuration['entities'] : array();
if (!$entityConfigs) {
return;
}
$annotationReader = $container->get('ibrows_sonataadmin.annotation.reader');
foreach ($entityConfigs as $entityConfig) {
foreach ($this->getEntityServices($entityConfig, $configuration, $annotationReader) as $serviceId => $definition) {
$container->setDefinition($serviceId, $definition);
}
}
}
/**
* @param array $entityConfig
* @param array $configuration
* @param AnnotationReaderInterface $annotationReader
* @return Definition[]
*/
protected function getEntityServices(array $entityConfig, array $configuration, AnnotationReaderInterface $annotationReader)
{
$services = array();
$directory = $entityConfig['directory'];
$namespacePrefix = $entityConfig['prefix'];
$replacedConfig = array_replace($configuration['default_entity'], $entityConfig);
$finder = new Finder();
$idPrefix = $configuration['service_id_prefix'];
/** @var SplFileInfo $file */
foreach ($finder->in($directory)->files('*.php') as $file) {
$baseName = $file->getBasename('.php');
$relativePath = $file->getRelativePath();
$namespacePieces = array_filter(array($namespacePrefix, str_replace("/", "\\", $relativePath), $baseName));
$idPieces = array_filter(array(str_replace("/", ".", $relativePath), $baseName));
$className = implode("\\", $namespacePieces);
/** @var AutoService $autoServiceAnnotation */
if ($autoServiceAnnotation = $annotationReader->getAnnotationsByType($className, 'AutoServiceInterface', $annotationReader::SCOPE_CLASS)) {
$id = $idPrefix . '.' . implode(".", array_map('strtolower', $idPieces));
$services[$id] = $this->getEntityService($autoServiceAnnotation, $replacedConfig, $className, $baseName);
}
}
return $services;
}
/**
* @param AutoService $autoServiceAnnotation
* @param array $replacedConfig
* @param string $className
* @param string $baseName
* @return Definition
*/
protected function getEntityService(AutoService $autoServiceAnnotation, array $replacedConfig, $className, $baseName)
{
if (!$admin = $autoServiceAnnotation->admin ?: $this->getConfigValue($replacedConfig, 'admin')) {
throw new \RuntimeException("Cannot determine SonataAdmin for " . $className . " provide one in configuration or on the AutoServiceAnnotation");
}
if (!$controller = $autoServiceAnnotation->controller ?: $this->getConfigValue($replacedConfig, 'controller')) {
throw new \RuntimeException("Cannot determine SonataAdminController for " . $className . " provide one in configuration or on the AutoServiceAnnotation");
}
$service = new Definition($admin, array(null, $className, $controller));
$service->addTag(
'sonata.admin',
array(
'manager_type' => 'orm',
'group' => $autoServiceAnnotation->group ?: $baseName,
'label' => $autoServiceAnnotation->label ?: $baseName,
'show_in_dashboard' => !is_null($autoServiceAnnotation->showInDashboard) ? $autoServiceAnnotation->showInDashboard : $this->getConfigValue($replacedConfig, 'show_in_dashboard'),
'label_translator_strategy' => $autoServiceAnnotation->labelTranslatorStrategy ?: $this->getConfigValue($replacedConfig, 'label_translator_strategy'),
'label_catalogue' => $autoServiceAnnotation->labelCatalog ?: $this->getConfigValue($replacedConfig, 'label_catalogue'),
)
);
return $service;
}
/**
* @param array $config
* @param string $key
* @return null|string
*/
protected function getConfigValue(array $config, $key)
{
return isset($config[$key]) ? $config[$key] : null;
}
}