Complex classes like SonataAdminExtension 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 SonataAdminExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class SonataAdminExtension extends AbstractExtension |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var Pool |
||
| 40 | */ |
||
| 41 | protected $pool; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var LoggerInterface |
||
| 45 | */ |
||
| 46 | protected $logger; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var TranslatorInterface|null |
||
| 50 | */ |
||
| 51 | protected $translator; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string[] |
||
| 55 | */ |
||
| 56 | private $xEditableTypeMapping = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ContainerInterface |
||
| 60 | */ |
||
| 61 | private $templateRegistries; |
||
| 62 | |||
| 63 | public function __construct( |
||
| 64 | Pool $pool, |
||
| 65 | LoggerInterface $logger = null, |
||
| 66 | TranslatorInterface $translator = null, |
||
| 67 | ContainerInterface $templateRegistries = null |
||
| 68 | ) { |
||
| 69 | // NEXT_MAJOR: make the translator parameter required |
||
| 70 | if (null === $translator) { |
||
| 71 | @trigger_error( |
||
|
|
|||
| 72 | 'The $translator parameter will be required fields with the 4.0 release.', |
||
| 73 | E_USER_DEPRECATED |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | $this->pool = $pool; |
||
| 77 | $this->logger = $logger; |
||
| 78 | $this->translator = $translator; |
||
| 79 | $this->templateRegistries = $templateRegistries; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getFilters() |
||
| 83 | { |
||
| 84 | return [ |
||
| 85 | new TwigFilter( |
||
| 86 | 'render_list_element', |
||
| 87 | [$this, 'renderListElement'], |
||
| 88 | [ |
||
| 89 | 'is_safe' => ['html'], |
||
| 90 | 'needs_environment' => true, |
||
| 91 | ] |
||
| 92 | ), |
||
| 93 | new TwigFilter( |
||
| 94 | 'render_view_element', |
||
| 95 | [$this, 'renderViewElement'], |
||
| 96 | [ |
||
| 97 | 'is_safe' => ['html'], |
||
| 98 | 'needs_environment' => true, |
||
| 99 | ] |
||
| 100 | ), |
||
| 101 | new TwigFilter( |
||
| 102 | 'render_view_element_compare', |
||
| 103 | [$this, 'renderViewElementCompare'], |
||
| 104 | [ |
||
| 105 | 'is_safe' => ['html'], |
||
| 106 | 'needs_environment' => true, |
||
| 107 | ] |
||
| 108 | ), |
||
| 109 | new TwigFilter( |
||
| 110 | 'render_relation_element', |
||
| 111 | [$this, 'renderRelationElement'] |
||
| 112 | ), |
||
| 113 | new TwigFilter( |
||
| 114 | 'sonata_urlsafeid', |
||
| 115 | [$this, 'getUrlsafeIdentifier'] |
||
| 116 | ), |
||
| 117 | new TwigFilter( |
||
| 118 | 'sonata_xeditable_type', |
||
| 119 | [$this, 'getXEditableType'] |
||
| 120 | ), |
||
| 121 | new TwigFilter( |
||
| 122 | 'sonata_xeditable_choices', |
||
| 123 | [$this, 'getXEditableChoices'] |
||
| 124 | ), |
||
| 125 | ]; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function getFunctions() |
||
| 129 | { |
||
| 130 | return [ |
||
| 131 | new TwigFunction('canonicalize_locale_for_moment', [$this, 'getCanonicalizedLocaleForMoment'], ['needs_context' => true]), |
||
| 132 | new TwigFunction('canonicalize_locale_for_select2', [$this, 'getCanonicalizedLocaleForSelect2'], ['needs_context' => true]), |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getName() |
||
| 137 | { |
||
| 138 | return 'sonata_admin'; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * render a list element from the FieldDescription. |
||
| 143 | * |
||
| 144 | * @param mixed $object |
||
| 145 | * @param array $params |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function renderListElement( |
||
| 150 | Environment $environment, |
||
| 151 | $object, |
||
| 152 | FieldDescriptionInterface $fieldDescription, |
||
| 153 | $params = [] |
||
| 154 | ) { |
||
| 155 | $template = $this->getTemplate( |
||
| 156 | $fieldDescription, |
||
| 157 | $this->getTemplateRegistry($fieldDescription->getAdmin()->getCode())->getTemplate('base_list_field'), |
||
| 158 | $environment |
||
| 159 | ); |
||
| 160 | |||
| 161 | return $this->render($fieldDescription, $template, array_merge($params, [ |
||
| 162 | 'admin' => $fieldDescription->getAdmin(), |
||
| 163 | 'object' => $object, |
||
| 164 | 'value' => $this->getValueFromFieldDescription($object, $fieldDescription), |
||
| 165 | 'field_description' => $fieldDescription, |
||
| 166 | ]), $environment); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @deprecated since 3.33, to be removed in 4.0. Use render instead |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function output( |
||
| 175 | FieldDescriptionInterface $fieldDescription, |
||
| 176 | Template $template, |
||
| 177 | array $parameters, |
||
| 178 | Environment $environment |
||
| 179 | ) { |
||
| 180 | return $this->render( |
||
| 181 | $fieldDescription, |
||
| 182 | new TemplateWrapper($environment, $template), |
||
| 183 | $parameters, |
||
| 184 | $environment |
||
| 185 | ); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * return the value related to FieldDescription, if the associated object does no |
||
| 190 | * exists => a temporary one is created. |
||
| 191 | * |
||
| 192 | * @param object $object |
||
| 193 | * |
||
| 194 | * @throws \RuntimeException |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function getValueFromFieldDescription( |
||
| 199 | $object, |
||
| 200 | FieldDescriptionInterface $fieldDescription, |
||
| 201 | array $params = [] |
||
| 202 | ) { |
||
| 203 | if (isset($params['loop']) && $object instanceof \ArrayAccess) { |
||
| 204 | throw new \RuntimeException('remove the loop requirement'); |
||
| 205 | } |
||
| 206 | |||
| 207 | $value = null; |
||
| 208 | |||
| 209 | try { |
||
| 210 | $value = $fieldDescription->getValue($object); |
||
| 211 | } catch (NoValueException $e) { |
||
| 212 | if ($fieldDescription->getAssociationAdmin()) { |
||
| 213 | $value = $fieldDescription->getAssociationAdmin()->getNewInstance(); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | return $value; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * render a view element. |
||
| 222 | * |
||
| 223 | * @param mixed $object |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function renderViewElement( |
||
| 228 | Environment $environment, |
||
| 229 | FieldDescriptionInterface $fieldDescription, |
||
| 230 | $object |
||
| 231 | ) { |
||
| 232 | $template = $this->getTemplate( |
||
| 233 | $fieldDescription, |
||
| 234 | '@SonataAdmin/CRUD/base_show_field.html.twig', |
||
| 235 | $environment |
||
| 236 | ); |
||
| 237 | |||
| 238 | try { |
||
| 239 | $value = $fieldDescription->getValue($object); |
||
| 240 | } catch (NoValueException $e) { |
||
| 241 | $value = null; |
||
| 242 | } |
||
| 243 | |||
| 244 | return $this->render($fieldDescription, $template, [ |
||
| 245 | 'field_description' => $fieldDescription, |
||
| 246 | 'object' => $object, |
||
| 247 | 'value' => $value, |
||
| 248 | 'admin' => $fieldDescription->getAdmin(), |
||
| 249 | ], $environment); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * render a compared view element. |
||
| 254 | * |
||
| 255 | * @param mixed $baseObject |
||
| 256 | * @param mixed $compareObject |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function renderViewElementCompare( |
||
| 261 | Environment $environment, |
||
| 262 | FieldDescriptionInterface $fieldDescription, |
||
| 263 | $baseObject, |
||
| 264 | $compareObject |
||
| 265 | ) { |
||
| 266 | $template = $this->getTemplate( |
||
| 267 | $fieldDescription, |
||
| 268 | '@SonataAdmin/CRUD/base_show_field.html.twig', |
||
| 269 | $environment |
||
| 270 | ); |
||
| 271 | |||
| 272 | try { |
||
| 273 | $baseValue = $fieldDescription->getValue($baseObject); |
||
| 274 | } catch (NoValueException $e) { |
||
| 275 | $baseValue = null; |
||
| 276 | } |
||
| 277 | |||
| 278 | try { |
||
| 279 | $compareValue = $fieldDescription->getValue($compareObject); |
||
| 280 | } catch (NoValueException $e) { |
||
| 281 | $compareValue = null; |
||
| 282 | } |
||
| 283 | |||
| 284 | $baseValueOutput = $template->render([ |
||
| 285 | 'admin' => $fieldDescription->getAdmin(), |
||
| 286 | 'field_description' => $fieldDescription, |
||
| 287 | 'value' => $baseValue, |
||
| 288 | ]); |
||
| 289 | |||
| 290 | $compareValueOutput = $template->render([ |
||
| 291 | 'field_description' => $fieldDescription, |
||
| 292 | 'admin' => $fieldDescription->getAdmin(), |
||
| 293 | 'value' => $compareValue, |
||
| 294 | ]); |
||
| 295 | |||
| 296 | // Compare the rendered output of both objects by using the (possibly) overridden field block |
||
| 297 | $isDiff = $baseValueOutput !== $compareValueOutput; |
||
| 298 | |||
| 299 | return $this->render($fieldDescription, $template, [ |
||
| 300 | 'field_description' => $fieldDescription, |
||
| 301 | 'value' => $baseValue, |
||
| 302 | 'value_compare' => $compareValue, |
||
| 303 | 'is_diff' => $isDiff, |
||
| 304 | 'admin' => $fieldDescription->getAdmin(), |
||
| 305 | ], $environment); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param mixed $element |
||
| 310 | * |
||
| 311 | * @throws \RuntimeException |
||
| 312 | * |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | public function renderRelationElement($element, FieldDescriptionInterface $fieldDescription) |
||
| 316 | { |
||
| 317 | if (!is_object($element)) { |
||
| 318 | return $element; |
||
| 319 | } |
||
| 320 | |||
| 321 | $propertyPath = $fieldDescription->getOption('associated_property'); |
||
| 322 | |||
| 323 | if (null === $propertyPath) { |
||
| 324 | // For BC kept associated_tostring option behavior |
||
| 325 | $method = $fieldDescription->getOption('associated_tostring'); |
||
| 326 | |||
| 327 | if ($method) { |
||
| 328 | @trigger_error( |
||
| 329 | 'Option "associated_tostring" is deprecated since version 2.3 and will be removed in 4.0. ' |
||
| 330 | .'Use "associated_property" instead.', |
||
| 331 | E_USER_DEPRECATED |
||
| 332 | ); |
||
| 333 | } else { |
||
| 334 | $method = '__toString'; |
||
| 335 | } |
||
| 336 | |||
| 337 | if (!method_exists($element, $method)) { |
||
| 338 | throw new \RuntimeException(sprintf( |
||
| 339 | 'You must define an `associated_property` option or '. |
||
| 340 | 'create a `%s::__toString` method to the field option %s from service %s is ', |
||
| 341 | get_class($element), |
||
| 342 | $fieldDescription->getName(), |
||
| 343 | $fieldDescription->getAdmin()->getCode() |
||
| 344 | )); |
||
| 345 | } |
||
| 346 | |||
| 347 | return call_user_func([$element, $method]); |
||
| 348 | } |
||
| 349 | |||
| 350 | if (is_callable($propertyPath)) { |
||
| 351 | return $propertyPath($element); |
||
| 352 | } |
||
| 353 | |||
| 354 | return $this->pool->getPropertyAccessor()->getValue($element, $propertyPath); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get the identifiers as a string that is safe to use in a url. |
||
| 359 | * |
||
| 360 | * @param object $model |
||
| 361 | * |
||
| 362 | * @return string string representation of the id that is safe to use in a url |
||
| 363 | */ |
||
| 364 | public function getUrlsafeIdentifier($model, AdminInterface $admin = null) |
||
| 365 | { |
||
| 366 | if (null === $admin) { |
||
| 367 | $admin = $this->pool->getAdminByClass(ClassUtils::getClass($model)); |
||
| 368 | } |
||
| 369 | |||
| 370 | return $admin->getUrlsafeIdentifier($model); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string[] $xEditableTypeMapping |
||
| 375 | */ |
||
| 376 | public function setXEditableTypeMapping($xEditableTypeMapping) |
||
| 377 | { |
||
| 378 | $this->xEditableTypeMapping = $xEditableTypeMapping; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return string|bool |
||
| 383 | */ |
||
| 384 | public function getXEditableType($type) |
||
| 385 | { |
||
| 386 | return isset($this->xEditableTypeMapping[$type]) ? $this->xEditableTypeMapping[$type] : false; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return xEditable choices based on the field description choices options & catalogue options. |
||
| 391 | * With the following choice options: |
||
| 392 | * ['Status1' => 'Alias1', 'Status2' => 'Alias2'] |
||
| 393 | * The method will return: |
||
| 394 | * [['value' => 'Status1', 'text' => 'Alias1'], ['value' => 'Status2', 'text' => 'Alias2']]. |
||
| 395 | * |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | public function getXEditableChoices(FieldDescriptionInterface $fieldDescription) |
||
| 399 | { |
||
| 400 | $choices = $fieldDescription->getOption('choices', []); |
||
| 401 | $catalogue = $fieldDescription->getOption('catalogue'); |
||
| 402 | $xEditableChoices = []; |
||
| 403 | if (!empty($choices)) { |
||
| 404 | reset($choices); |
||
| 405 | $first = current($choices); |
||
| 406 | // the choices are already in the right format |
||
| 407 | if (is_array($first) && array_key_exists('value', $first) && array_key_exists('text', $first)) { |
||
| 408 | $xEditableChoices = $choices; |
||
| 409 | } else { |
||
| 410 | foreach ($choices as $value => $text) { |
||
| 411 | if ($catalogue) { |
||
| 412 | if (null !== $this->translator) { |
||
| 413 | $text = $this->translator->trans($text, [], $catalogue); |
||
| 414 | // NEXT_MAJOR: Remove this check |
||
| 415 | } elseif (method_exists($fieldDescription->getAdmin(), 'trans')) { |
||
| 416 | $text = $fieldDescription->getAdmin()->trans($text, [], $catalogue); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | $xEditableChoices[] = [ |
||
| 421 | 'value' => $value, |
||
| 422 | 'text' => $text, |
||
| 423 | ]; |
||
| 424 | } |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | if (false === $fieldDescription->getOption('required', true) |
||
| 429 | && false === $fieldDescription->getOption('multiple', false) |
||
| 430 | ) { |
||
| 431 | $xEditableChoices = array_merge([[ |
||
| 432 | 'value' => '', |
||
| 433 | 'text' => '', |
||
| 434 | ]], $xEditableChoices); |
||
| 435 | } |
||
| 436 | |||
| 437 | return $xEditableChoices; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Returns a canonicalized locale for "moment" NPM library, |
||
| 442 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
| 443 | * |
||
| 444 | * @return null|string |
||
| 445 | */ |
||
| 446 | final public function getCanonicalizedLocaleForMoment(array $context) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Returns a canonicalized locale for "select2" NPM library, |
||
| 470 | * or `null` if the locale's language is "en", which doesn't require localization. |
||
| 471 | * |
||
| 472 | * @return null|string |
||
| 473 | */ |
||
| 474 | final public function getCanonicalizedLocaleForSelect2(array $context) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Get template. |
||
| 504 | * |
||
| 505 | * @param string $defaultTemplate |
||
| 506 | * |
||
| 507 | * @return TemplateWrapper |
||
| 508 | */ |
||
| 509 | protected function getTemplate( |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | private function render( |
||
| 545 | FieldDescriptionInterface $fieldDescription, |
||
| 546 | TemplateWrapper $template, |
||
| 547 | array $parameters, |
||
| 548 | Environment $environment |
||
| 549 | ) { |
||
| 550 | $content = $template->render($parameters); |
||
| 551 | |||
| 552 | if ($environment->isDebug()) { |
||
| 553 | $commentTemplate = <<<'EOT' |
||
| 554 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * @param string $adminCode |
||
| 579 | * |
||
| 580 | * @throws ServiceCircularReferenceException |
||
| 581 | * @throws ServiceNotFoundException |
||
| 582 | * |
||
| 583 | * @return TemplateRegistryInterface |
||
| 584 | */ |
||
| 585 | private function getTemplateRegistry($adminCode) |
||
| 596 | } |
||
| 597 |
If you suppress an error, we recommend checking for the error condition explicitly: