Complex classes like AddDependencyCallsCompilerPass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AddDependencyCallsCompilerPass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class AddDependencyCallsCompilerPass implements CompilerPassInterface |
||
35 | { |
||
36 | public function process(ContainerBuilder $container) |
||
37 | { |
||
38 | // check if translator service exist |
||
39 | if (!$container->has('translator')) { |
||
40 | throw new \RuntimeException('The "translator" service is not yet enabled. |
||
41 | It\'s required by SonataAdmin to display all labels properly. |
||
42 | |||
43 | To learn how to enable the translator service please visit: |
||
44 | http://symfony.com/doc/current/translation.html#configuration |
||
45 | '); |
||
46 | } |
||
47 | |||
48 | $parameterBag = $container->getParameterBag(); |
||
49 | $groupDefaults = $admins = $classes = []; |
||
50 | |||
51 | $pool = $container->getDefinition('sonata.admin.pool'); |
||
52 | |||
53 | foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $tags) { |
||
54 | foreach ($tags as $attributes) { |
||
55 | $definition = $container->getDefinition($id); |
||
56 | $parentDefinition = null; |
||
57 | |||
58 | // Temporary fix until we can support service locators |
||
59 | $definition->setPublic(true); |
||
60 | |||
61 | if ($definition instanceof ChildDefinition) { |
||
62 | $parentDefinition = $container->getDefinition($definition->getParent()); |
||
63 | } |
||
64 | |||
65 | $this->replaceDefaultArguments([ |
||
66 | 0 => $id, |
||
67 | 2 => CRUDController::class, |
||
68 | ], $definition, $parentDefinition); |
||
69 | $this->applyConfigurationFromAttribute($definition, $attributes); |
||
70 | $this->applyDefaults($container, $id, $attributes); |
||
71 | |||
72 | $arguments = $parentDefinition ? |
||
73 | array_merge($parentDefinition->getArguments(), $definition->getArguments()) : |
||
74 | $definition->getArguments(); |
||
75 | |||
76 | $admins[] = $id; |
||
77 | |||
78 | if (!isset($classes[$arguments[1]])) { |
||
79 | $classes[$arguments[1]] = []; |
||
80 | } |
||
81 | |||
82 | $classes[$arguments[1]][] = $id; |
||
83 | |||
84 | $showInDashboard = (bool) (isset($attributes['show_in_dashboard']) ? $parameterBag->resolveValue($attributes['show_in_dashboard']) : true); |
||
85 | if (!$showInDashboard) { |
||
86 | continue; |
||
87 | } |
||
88 | |||
89 | $resolvedGroupName = isset($attributes['group']) ? |
||
90 | $parameterBag->resolveValue($attributes['group']) : |
||
91 | $container->getParameter('sonata.admin.configuration.default_group'); |
||
92 | $labelCatalogue = $attributes['label_catalogue'] ?? |
||
93 | $container->getParameter('sonata.admin.configuration.default_label_catalogue'); |
||
94 | $icon = $attributes['icon'] ?? |
||
95 | $container->getParameter('sonata.admin.configuration.default_icon'); |
||
96 | $onTop = $attributes['on_top'] ?? false; |
||
97 | $keepOpen = $attributes['keep_open'] ?? false; |
||
98 | |||
99 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
100 | $groupDefaults[$resolvedGroupName] = [ |
||
101 | 'label' => $resolvedGroupName, |
||
102 | 'label_catalogue' => $labelCatalogue, |
||
103 | 'icon' => $icon, |
||
104 | 'roles' => [], |
||
105 | 'on_top' => false, |
||
106 | 'keep_open' => false, |
||
107 | ]; |
||
108 | } |
||
109 | |||
110 | $groupDefaults[$resolvedGroupName]['items'][] = [ |
||
111 | 'admin' => $id, |
||
112 | 'label' => !empty($attributes['label']) ? $attributes['label'] : '', |
||
113 | 'route' => '', |
||
114 | 'route_params' => [], |
||
115 | 'route_absolute' => false, |
||
116 | ]; |
||
117 | |||
118 | if (isset($groupDefaults[$resolvedGroupName]['on_top']) && $groupDefaults[$resolvedGroupName]['on_top'] |
||
119 | || $onTop && (\count($groupDefaults[$resolvedGroupName]['items']) > 1)) { |
||
120 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
121 | } |
||
122 | $groupDefaults[$resolvedGroupName]['on_top'] = $onTop; |
||
123 | |||
124 | $groupDefaults[$resolvedGroupName]['keep_open'] = $keepOpen; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | $dashboardGroupsSettings = $container->getParameter('sonata.admin.configuration.dashboard_groups'); |
||
129 | if (!empty($dashboardGroupsSettings)) { |
||
130 | $groups = $dashboardGroupsSettings; |
||
131 | |||
132 | foreach ($dashboardGroupsSettings as $groupName => $group) { |
||
133 | $resolvedGroupName = $parameterBag->resolveValue($groupName); |
||
134 | if (!isset($groupDefaults[$resolvedGroupName])) { |
||
135 | $groupDefaults[$resolvedGroupName] = [ |
||
136 | 'items' => [], |
||
137 | 'label' => $resolvedGroupName, |
||
138 | 'roles' => [], |
||
139 | 'on_top' => false, |
||
140 | 'keep_open' => false, |
||
141 | ]; |
||
142 | } |
||
143 | |||
144 | if (empty($group['items'])) { |
||
145 | $groups[$resolvedGroupName]['items'] = $groupDefaults[$resolvedGroupName]['items']; |
||
146 | } |
||
147 | |||
148 | if (empty($group['label'])) { |
||
149 | $groups[$resolvedGroupName]['label'] = $groupDefaults[$resolvedGroupName]['label']; |
||
150 | } |
||
151 | |||
152 | if (empty($group['label_catalogue'])) { |
||
153 | $groups[$resolvedGroupName]['label_catalogue'] = 'SonataAdminBundle'; |
||
154 | } |
||
155 | |||
156 | if (empty($group['icon'])) { |
||
157 | $groups[$resolvedGroupName]['icon'] = $groupDefaults[$resolvedGroupName]['icon']; |
||
158 | } |
||
159 | |||
160 | if (!empty($group['item_adds'])) { |
||
161 | $groups[$resolvedGroupName]['items'] = array_merge($groups[$resolvedGroupName]['items'], $group['item_adds']); |
||
162 | } |
||
163 | |||
164 | if (empty($group['roles'])) { |
||
165 | $groups[$resolvedGroupName]['roles'] = $groupDefaults[$resolvedGroupName]['roles']; |
||
166 | } |
||
167 | |||
168 | if (isset($groups[$resolvedGroupName]['on_top']) && !empty($group['on_top']) && $group['on_top'] |
||
169 | && (\count($groups[$resolvedGroupName]['items']) > 1)) { |
||
170 | throw new \RuntimeException('You can\'t use "on_top" option with multiple same name groups.'); |
||
171 | } |
||
172 | if (empty($group['on_top'])) { |
||
173 | $groups[$resolvedGroupName]['on_top'] = $groupDefaults[$resolvedGroupName]['on_top']; |
||
174 | } |
||
175 | |||
176 | if (empty($group['keep_open'])) { |
||
177 | $groups[$resolvedGroupName]['keep_open'] = $groupDefaults[$resolvedGroupName]['keep_open']; |
||
178 | } |
||
179 | } |
||
180 | } elseif ($container->getParameter('sonata.admin.configuration.sort_admins')) { |
||
181 | $groups = $groupDefaults; |
||
182 | |||
183 | $elementSort = static function (&$element) { |
||
184 | usort( |
||
185 | $element['items'], |
||
186 | static function ($a, $b) { |
||
187 | $a = !empty($a['label']) ? $a['label'] : $a['admin']; |
||
188 | $b = !empty($b['label']) ? $b['label'] : $b['admin']; |
||
189 | |||
190 | if ($a === $b) { |
||
191 | return 0; |
||
192 | } |
||
193 | |||
194 | return $a < $b ? -1 : 1; |
||
195 | } |
||
196 | ); |
||
197 | }; |
||
198 | |||
199 | /* |
||
200 | * 1) sort the groups by their index |
||
201 | * 2) sort the elements within each group by label/admin |
||
202 | */ |
||
203 | ksort($groups); |
||
204 | array_walk($groups, $elementSort); |
||
205 | } else { |
||
206 | $groups = $groupDefaults; |
||
207 | } |
||
208 | |||
209 | $pool->addMethodCall('setAdminServiceIds', [$admins]); |
||
210 | $pool->addMethodCall('setAdminGroups', [$groups]); |
||
211 | $pool->addMethodCall('setAdminClasses', [$classes]); |
||
212 | |||
213 | $routeLoader = $container->getDefinition('sonata.admin.route_loader'); |
||
214 | $routeLoader->replaceArgument(1, $admins); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * This method read the attribute keys and configure admin class to use the related dependency. |
||
219 | */ |
||
220 | public function applyConfigurationFromAttribute(Definition $definition, array $attributes) |
||
221 | { |
||
222 | $keys = [ |
||
223 | 'model_manager', |
||
224 | 'form_contractor', |
||
225 | 'show_builder', |
||
226 | 'list_builder', |
||
227 | 'datagrid_builder', |
||
228 | 'translator', |
||
229 | 'configuration_pool', |
||
230 | 'router', |
||
231 | 'validator', |
||
232 | 'security_handler', |
||
233 | 'menu_factory', |
||
234 | 'route_builder', |
||
235 | 'label_translator_strategy', |
||
236 | ]; |
||
237 | |||
238 | foreach ($keys as $key) { |
||
239 | $method = $this->generateSetterMethodName($key); |
||
240 | if (!isset($attributes[$key]) || $definition->hasMethodCall($method)) { |
||
241 | continue; |
||
242 | } |
||
243 | |||
244 | $definition->addMethodCall($method, [new Reference($attributes[$key])]); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Apply the default values required by the AdminInterface to the Admin service definition. |
||
250 | * |
||
251 | * @param string $serviceId |
||
252 | * |
||
253 | * @return Definition |
||
254 | */ |
||
255 | public function applyDefaults(ContainerBuilder $container, $serviceId, array $attributes = []) |
||
358 | |||
359 | /** |
||
360 | * @param string $serviceId |
||
361 | */ |
||
362 | public function fixTemplates( |
||
418 | |||
419 | /** |
||
420 | * Replace the empty arguments required by the Admin service definition. |
||
421 | */ |
||
422 | private function replaceDefaultArguments( |
||
441 | |||
442 | private function generateSetterMethodName(string $key): string |
||
443 | { |
||
446 | } |
||
447 |