Complex classes like ServiceProvider 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 ServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Holds the data instances. |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $datas; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Holds the map for overriding templates. |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $templates = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Holds whether CRUDlex manages i18n. |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $manageI18n = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Initializes needed but yet missing service providers. |
||
| 60 | * |
||
| 61 | * @param Container $app |
||
| 62 | * the application container |
||
| 63 | */ |
||
| 64 | 78 | protected function initMissingServiceProviders(Container $app) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Initializes the available locales. |
||
| 86 | * |
||
| 87 | * @param Translator $translator |
||
| 88 | * the translator |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | * the available locales |
||
| 92 | */ |
||
| 93 | 77 | protected function initLocales(Translator $translator) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Initializes the children of the data entries. |
||
| 106 | */ |
||
| 107 | 77 | protected function initChildren() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 121 | * |
||
| 122 | * @param array $locales |
||
| 123 | * the available locales |
||
| 124 | * @param array $crud |
||
| 125 | * the CRUD entity map |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | * the map with localized entity labels |
||
| 129 | */ |
||
| 130 | 77 | protected function getLocaleLabels(array $locales, array $crud) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Configures the EntityDefinition according to the given |
||
| 143 | * CRUD entity map. |
||
| 144 | * |
||
| 145 | * @param EntityDefinition $definition |
||
| 146 | * the definition to configure |
||
| 147 | * @param array $crud |
||
| 148 | * the CRUD entity map |
||
| 149 | */ |
||
| 150 | 77 | protected function configureDefinition(EntityDefinition $definition, array $crud) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Creates and setups an EntityDefinition instance. |
||
| 174 | * |
||
| 175 | * @param Translator $translator |
||
| 176 | * the Translator to use for some standard field labels |
||
| 177 | * @param EntityDefinitionFactoryInterface $entityDefinitionFactory |
||
| 178 | * the EntityDefinitionFactory to use |
||
| 179 | * @param array $locales |
||
| 180 | * the available locales |
||
| 181 | * @param array $crud |
||
| 182 | * the parsed YAML of a CRUD entity |
||
| 183 | * @param string $name |
||
| 184 | * the name of the entity |
||
| 185 | * |
||
| 186 | * @return EntityDefinition |
||
| 187 | * the EntityDefinition good to go |
||
| 188 | */ |
||
| 189 | 77 | protected function createDefinition(Translator $translator, EntityDefinitionFactoryInterface $entityDefinitionFactory, array $locales, array $crud, $name) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Validates the parsed entity definition. |
||
| 213 | * |
||
| 214 | * @param Container $app |
||
| 215 | * the application container |
||
| 216 | * @param array $entityDefinition |
||
| 217 | * the entity definition to validate |
||
| 218 | */ |
||
| 219 | 77 | protected function validateEntityDefinition(Container $app, array $entityDefinition) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Initializes the instance. |
||
| 232 | * |
||
| 233 | * @param string|null $crudFileCachingDirectory |
||
| 234 | * the writable directory to store the CRUD YAML file cache |
||
| 235 | * @param Container $app |
||
| 236 | * the application container |
||
| 237 | */ |
||
| 238 | 78 | public function init($crudFileCachingDirectory, Container $app) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 260 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 261 | * |
||
| 262 | * @param Container $app |
||
| 263 | * the Container instance of the Silex application |
||
| 264 | */ |
||
| 265 | 11 | public function register(Container $app) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Initializes the crud service right after boot. |
||
| 281 | * |
||
| 282 | * @param Application $app |
||
| 283 | * the Container instance of the Silex application |
||
| 284 | */ |
||
| 285 | 78 | public function boot(Application $app) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Getter for the AbstractData instances. |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | * the entity name of the desired Data instance |
||
| 297 | * |
||
| 298 | * @return AbstractData |
||
| 299 | * the AbstractData instance or null on invalid name |
||
| 300 | */ |
||
| 301 | 70 | public function getData($name) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Getter for all available entity names. |
||
| 311 | * |
||
| 312 | * @return string[] |
||
| 313 | * a list of all available entity names |
||
| 314 | */ |
||
| 315 | 7 | public function getEntities() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Getter for the entities for the navigation bar. |
||
| 322 | * |
||
| 323 | * @return string[] |
||
| 324 | * a list of all available entity names with their group |
||
| 325 | */ |
||
| 326 | 10 | public function getEntitiesNavBar() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Sets a template to use instead of the build in ones. |
||
| 342 | * |
||
| 343 | * @param string $key |
||
| 344 | * the template key to use in this format: |
||
| 345 | * $section.$action.$entity |
||
| 346 | * $section.$action |
||
| 347 | * $section |
||
| 348 | * @param string $template |
||
| 349 | * the template to use for this key |
||
| 350 | */ |
||
| 351 | 12 | public function setTemplate($key, $template) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Determines the Twig template to use for the given parameters depending on |
||
| 358 | * the existance of certain template keys set in this order: |
||
| 359 | * |
||
| 360 | * $section.$action.$entity |
||
| 361 | * $section.$action |
||
| 362 | * $section |
||
| 363 | * |
||
| 364 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 365 | * |
||
| 366 | * @param string $section |
||
| 367 | * the section of the template, either "layout" or "template" |
||
| 368 | * @param string $action |
||
| 369 | * the current calling action like "create" or "show" |
||
| 370 | * @param string $entity |
||
| 371 | * the current calling entity |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | * the best fitting template |
||
| 375 | */ |
||
| 376 | 11 | public function getTemplate($section, $action, $entity) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Sets the locale to be used. |
||
| 397 | * |
||
| 398 | * @param string $locale |
||
| 399 | * the locale to be used. |
||
| 400 | */ |
||
| 401 | 10 | public function setLocale($locale) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Gets the available locales. |
||
| 410 | * |
||
| 411 | * @return array |
||
| 412 | * the available locales |
||
| 413 | */ |
||
| 414 | 78 | public function getLocales() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Gets whether CRUDlex manages the i18n. |
||
| 435 | * @return bool |
||
| 436 | * true if so |
||
| 437 | */ |
||
| 438 | 11 | public function isManageI18n() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Sets whether CRUDlex manages the i18n. |
||
| 445 | * @param bool $manageI18n |
||
| 446 | * true if so |
||
| 447 | */ |
||
| 448 | 1 | public function setManageI18n($manageI18n) |
|
| 452 | |||
| 453 | } |
||
| 454 |