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 |
||
| 30 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Holds the {@see AbstractData} instances. |
||
| 34 | */ |
||
| 35 | protected $datas; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Reads and returns the contents of the given Yaml file. If |
||
| 39 | * it goes wrong, it throws an exception. |
||
| 40 | * |
||
| 41 | * @param string $fileName |
||
| 42 | * the file to read |
||
| 43 | * |
||
| 44 | * @return array |
||
| 45 | * the file contents |
||
| 46 | * |
||
| 47 | * @throws \RuntimeException |
||
| 48 | * thrown if the file could not be read or parsed |
||
| 49 | */ |
||
| 50 | protected function readYaml($fileName) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Initializes needed but yet missing service providers. |
||
| 65 | * |
||
| 66 | * @param Container $app |
||
| 67 | * the application container |
||
| 68 | */ |
||
| 69 | protected function initMissingServiceProviders(Container $app) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Initializes the available locales. |
||
| 90 | * |
||
| 91 | * @param Container $app |
||
| 92 | * the application container |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | * the available locales |
||
| 96 | */ |
||
| 97 | protected function initLocales(Container $app) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Initializes the children of the data entries. |
||
| 109 | */ |
||
| 110 | protected function initChildren() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 123 | * |
||
| 124 | * @param array $locales |
||
| 125 | * the available locales |
||
| 126 | * @param array $crud |
||
| 127 | * the CRUD entity map |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | * the map with localized entity labels |
||
| 131 | */ |
||
| 132 | protected function getLocaleLabels($locales, $crud) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Configures the EntityDefinition according to the given |
||
| 144 | * CRUD entity map. |
||
| 145 | * |
||
| 146 | * @param EntityDefinition $definition |
||
| 147 | * the definition to configure |
||
| 148 | * @param array $crud |
||
| 149 | * the CRUD entity map |
||
| 150 | */ |
||
| 151 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Creates and setups an EntityDefinition instance. |
||
| 171 | * |
||
| 172 | * @param Container $app |
||
| 173 | * the application container |
||
| 174 | * @param array $locales |
||
| 175 | * the available locales |
||
| 176 | * @param array $crud |
||
| 177 | * the parsed YAML of a CRUD entity |
||
| 178 | * @param string $name |
||
| 179 | * the name of the entity |
||
| 180 | * |
||
| 181 | * @return EntityDefinition |
||
| 182 | * the EntityDefinition good to go |
||
| 183 | */ |
||
| 184 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Validates the parsed entity definition. |
||
| 209 | * |
||
| 210 | * @param Container $app |
||
| 211 | * the application container |
||
| 212 | * @param array $entityDefinition |
||
| 213 | * the entity definition to validate |
||
| 214 | */ |
||
| 215 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Initializes the instance. |
||
| 227 | * |
||
| 228 | * @param DataFactoryInterface $dataFactory |
||
| 229 | * the factory to create the concrete AbstractData instances |
||
| 230 | * @param string $crudFile |
||
| 231 | * the CRUD YAML file to parse |
||
| 232 | * @param FileProcessorInterface $fileProcessor |
||
| 233 | * the file processor used for file fields |
||
| 234 | * @param Container $app |
||
| 235 | * the application container |
||
| 236 | */ |
||
| 237 | public function init(DataFactoryInterface $dataFactory, $crudFile, FileProcessorInterface $fileProcessor, Container $app) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 261 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 262 | * |
||
| 263 | * @param Container $app |
||
| 264 | * the Container instance of the Silex application |
||
| 265 | */ |
||
| 266 | public function register(Container $app) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Initializes the crud service right after boot. |
||
| 275 | * |
||
| 276 | * @param Application $app |
||
| 277 | * the Container instance of the Silex application |
||
| 278 | */ |
||
| 279 | public function boot(Application $app) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Getter for the {@see AbstractData} instances. |
||
| 286 | * |
||
| 287 | * @param string $name |
||
| 288 | * the entity name of the desired Data instance |
||
| 289 | * |
||
| 290 | * @return AbstractData |
||
| 291 | * the AbstractData instance or null on invalid name |
||
| 292 | */ |
||
| 293 | public function getData($name) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Getter for all available entity names. |
||
| 302 | * |
||
| 303 | * @return string[] |
||
| 304 | * a list of all available entity names |
||
| 305 | */ |
||
| 306 | public function getEntities() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Determines the Twig template to use for the given parameters depending on |
||
| 312 | * the existance of certain keys in the Container $app in this order: |
||
| 313 | * |
||
| 314 | * crud.$section.$action.$entity |
||
| 315 | * crud.$section.$action |
||
| 316 | * crud.$section |
||
| 317 | * |
||
| 318 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 319 | * |
||
| 320 | * @param Container $app |
||
| 321 | * the Silex application |
||
| 322 | * @param string $section |
||
| 323 | * the section of the template, either "layout" or "template" |
||
| 324 | * @param string $action |
||
| 325 | * the current calling action like "create" or "show" |
||
| 326 | * @param string $entity |
||
| 327 | * the current calling entity |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | * the best fitting template |
||
| 331 | */ |
||
| 332 | public function getTemplate(Container $app, $section, $action, $entity) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Sets the locale to be used. |
||
| 353 | * |
||
| 354 | * @param string $locale |
||
| 355 | * the locale to be used. |
||
| 356 | */ |
||
| 357 | public function setLocale($locale) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Gets the available locales. |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | * the available locales |
||
| 368 | */ |
||
| 369 | public function getLocales() { |
||
| 386 | |||
| 387 | } |
||
| 388 |