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 |
||
| 29 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Holds the {@see AbstractData} instances. |
||
| 33 | */ |
||
| 34 | protected $datas; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Initializes needed but yet missing service providers. |
||
| 38 | * |
||
| 39 | * @param Container $app |
||
| 40 | * the application container |
||
| 41 | */ |
||
| 42 | 77 | protected function initMissingServiceProviders(Container $app) { |
|
| 43 | |||
| 44 | 77 | if (!$app->offsetExists('translator')) { |
|
| 45 | 77 | $app->register(new LocaleServiceProvider()); |
|
| 46 | 77 | $app->register(new TranslationServiceProvider(), [ |
|
| 47 | 77 | 'locale_fallbacks' => ['en'], |
|
| 48 | 77 | ]); |
|
| 49 | 77 | } |
|
| 50 | |||
| 51 | 77 | if (!$app->offsetExists('session')) { |
|
| 52 | 67 | $app->register(new SessionServiceProvider()); |
|
| 53 | 67 | } |
|
| 54 | |||
| 55 | 77 | if (!$app->offsetExists('twig')) { |
|
| 56 | 67 | $app->register(new TwigServiceProvider()); |
|
| 57 | 67 | } |
|
| 58 | 77 | $app['twig.loader.filesystem']->addPath(__DIR__.'/../views/', 'crud'); |
|
| 59 | 77 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Initializes the available locales. |
||
| 63 | * |
||
| 64 | * @param Container $app |
||
| 65 | * the application container |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | * the available locales |
||
| 69 | */ |
||
| 70 | 77 | protected function initLocales(Container $app) { |
|
| 71 | 77 | $locales = $this->getLocales(); |
|
| 72 | 77 | $localeDir = __DIR__.'/../locales'; |
|
| 73 | 77 | $app['translator']->addLoader('yaml', new YamlFileLoader()); |
|
| 74 | 77 | foreach ($locales as $locale) { |
|
| 75 | 77 | $app['translator']->addResource('yaml', $localeDir.'/'.$locale.'.yml', $locale); |
|
| 76 | 77 | } |
|
| 77 | 77 | return $locales; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Initializes the children of the data entries. |
||
| 82 | */ |
||
| 83 | 77 | protected function initChildren() { |
|
| 84 | 77 | foreach ($this->datas as $name => $data) { |
|
| 85 | 77 | $fields = $data->getDefinition()->getFieldNames(); |
|
| 86 | 77 | foreach ($fields as $field) { |
|
| 87 | 77 | if ($data->getDefinition()->getType($field) == 'reference') { |
|
| 88 | 76 | $this->datas[$data->getDefinition()->getSubTypeField($field, 'reference', 'entity')]->getDefinition()->addChild($data->getDefinition()->getTable(), $field, $name); |
|
| 89 | 76 | } |
|
| 90 | 77 | } |
|
| 91 | 77 | } |
|
| 92 | 77 | } |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Gets a map with localized entity labels from the CRUD YML. |
||
| 96 | * |
||
| 97 | * @param array $locales |
||
| 98 | * the available locales |
||
| 99 | * @param array $crud |
||
| 100 | * the CRUD entity map |
||
| 101 | * |
||
| 102 | * @return array |
||
| 103 | * the map with localized entity labels |
||
| 104 | */ |
||
| 105 | 77 | protected function getLocaleLabels($locales, $crud) { |
|
| 106 | 77 | $localeLabels = []; |
|
| 107 | 77 | foreach ($locales as $locale) { |
|
| 108 | 77 | if (array_key_exists('label_'.$locale, $crud)) { |
|
| 109 | 77 | $localeLabels[$locale] = $crud['label_'.$locale]; |
|
| 110 | 77 | } |
|
| 111 | 77 | } |
|
| 112 | 77 | return $localeLabels; |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Configures the EntityDefinition according to the given |
||
| 117 | * CRUD entity map. |
||
| 118 | * |
||
| 119 | * @param EntityDefinition $definition |
||
| 120 | * the definition to configure |
||
| 121 | * @param array $crud |
||
| 122 | * the CRUD entity map |
||
| 123 | */ |
||
| 124 | 77 | protected function configureDefinition(EntityDefinition $definition, array $crud) { |
|
| 125 | $toConfigure = [ |
||
| 126 | 77 | 'deleteCascade', |
|
| 127 | 77 | 'listFields', |
|
| 128 | 77 | 'filter', |
|
| 129 | 77 | 'childrenLabelFields', |
|
| 130 | 77 | 'pageSize', |
|
| 131 | 77 | 'initialSortField', |
|
| 132 | 77 | 'initialSortAscending', |
|
| 133 | 'navBarGroup' |
||
| 134 | 77 | ]; |
|
| 135 | 77 | foreach ($toConfigure as $field) { |
|
| 136 | 77 | if (array_key_exists($field, $crud)) { |
|
| 137 | 77 | $function = 'set'.ucfirst($field); |
|
| 138 | 77 | $definition->$function($crud[$field]); |
|
| 139 | 77 | } |
|
| 140 | 77 | } |
|
| 141 | 77 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Creates and setups an EntityDefinition instance. |
||
| 145 | * |
||
| 146 | * @param Container $app |
||
| 147 | * the application container |
||
| 148 | * @param array $locales |
||
| 149 | * the available locales |
||
| 150 | * @param array $crud |
||
| 151 | * the parsed YAML of a CRUD entity |
||
| 152 | * @param string $name |
||
| 153 | * the name of the entity |
||
| 154 | * |
||
| 155 | * @return EntityDefinition |
||
| 156 | * the EntityDefinition good to go |
||
| 157 | */ |
||
| 158 | 77 | protected function createDefinition(Container $app, array $locales, array $crud, $name) { |
|
| 159 | 77 | $label = array_key_exists('label', $crud) ? $crud['label'] : $name; |
|
| 160 | 77 | $localeLabels = $this->getLocaleLabels($locales, $crud); |
|
| 161 | $standardFieldLabels = [ |
||
| 162 | 77 | 'id' => $app['translator']->trans('crudlex.label.id'), |
|
| 163 | 77 | 'created_at' => $app['translator']->trans('crudlex.label.created_at'), |
|
| 164 | 77 | 'updated_at' => $app['translator']->trans('crudlex.label.updated_at') |
|
| 165 | 77 | ]; |
|
| 166 | |||
| 167 | 77 | $factory = $app->offsetExists('crud.entitydefinitionfactory') ? $app['crud.entitydefinitionfactory'] : new EntityDefinitionFactory(); |
|
| 168 | |||
| 169 | 77 | $definition = $factory->createEntityDefinition( |
|
| 170 | 77 | $crud['table'], |
|
| 171 | 77 | $crud['fields'], |
|
| 172 | 77 | $label, |
|
| 173 | 77 | $localeLabels, |
|
| 174 | 77 | $standardFieldLabels, |
|
| 175 | $this |
||
| 176 | 77 | ); |
|
| 177 | 77 | $this->configureDefinition($definition, $crud); |
|
| 178 | 77 | return $definition; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Validates the parsed entity definition. |
||
| 183 | * |
||
| 184 | * @param Container $app |
||
| 185 | * the application container |
||
| 186 | * @param array $entityDefinition |
||
| 187 | * the entity definition to validate |
||
| 188 | */ |
||
| 189 | 77 | protected function validateEntityDefinition(Container $app, array $entityDefinition) { |
|
| 190 | 77 | $doValidate = !$app->offsetExists('crud.validateentitydefinition') || $app['crud.validateentitydefinition'] === true; |
|
| 191 | 77 | if ($doValidate) { |
|
| 192 | 76 | $validator = $app->offsetExists('crud.entitydefinitionvalidator') |
|
| 193 | 76 | ? $app['crud.entitydefinitionvalidator'] |
|
| 194 | 76 | : new EntityDefinitionValidator(); |
|
| 195 | 76 | $validator->validate($entityDefinition); |
|
| 196 | 76 | } |
|
| 197 | 77 | } |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Initializes the instance. |
||
| 201 | * |
||
| 202 | * @param DataFactoryInterface $dataFactory |
||
| 203 | * the factory to create the concrete AbstractData instances |
||
| 204 | * @param string $crudFile |
||
| 205 | * the CRUD YAML file to parse |
||
| 206 | * @param string|null $crudFileCachingDirectory |
||
| 207 | * the writable directory to store the CRUD YAML file cache |
||
| 208 | * @param FileProcessorInterface $fileProcessor |
||
| 209 | * the file processor used for file fields |
||
| 210 | * @param Container $app |
||
| 211 | * the application container |
||
| 212 | */ |
||
| 213 | 78 | public function init(DataFactoryInterface $dataFactory, $crudFile, $crudFileCachingDirectory, FileProcessorInterface $fileProcessor, Container $app) { |
|
| 214 | |||
| 215 | 78 | $reader = new YamlReader($crudFileCachingDirectory); |
|
| 216 | 78 | $parsedYaml = $reader->read($crudFile); |
|
| 217 | |||
| 218 | 77 | $this->validateEntityDefinition($app, $parsedYaml); |
|
| 219 | |||
| 220 | 77 | $locales = $this->initLocales($app); |
|
| 221 | 77 | $this->datas = []; |
|
| 222 | 77 | foreach ($parsedYaml as $name => $crud) { |
|
| 223 | 77 | $definition = $this->createDefinition($app, $locales, $crud, $name); |
|
| 224 | 77 | $this->datas[$name] = $dataFactory->createData($definition, $fileProcessor); |
|
| 225 | 77 | } |
|
| 226 | |||
| 227 | 77 | $this->initChildren(); |
|
| 228 | |||
| 229 | 77 | } |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
| 233 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
| 234 | * |
||
| 235 | * @param Container $app |
||
| 236 | * the Container instance of the Silex application |
||
| 237 | */ |
||
| 238 | public function register(Container $app) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Initializes the crud service right after boot. |
||
| 250 | * |
||
| 251 | * @param Application $app |
||
| 252 | * the Container instance of the Silex application |
||
| 253 | */ |
||
| 254 | 77 | public function boot(Application $app) { |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Getter for the {@see AbstractData} instances. |
||
| 262 | * |
||
| 263 | * @param string $name |
||
| 264 | * the entity name of the desired Data instance |
||
| 265 | * |
||
| 266 | * @return AbstractData |
||
| 267 | * the AbstractData instance or null on invalid name |
||
| 268 | */ |
||
| 269 | 71 | public function getData($name) { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Getter for all available entity names. |
||
| 278 | * |
||
| 279 | * @return string[] |
||
| 280 | * a list of all available entity names |
||
| 281 | */ |
||
| 282 | 3 | public function getEntities() { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Getter for the entities for the navigation bar. |
||
| 288 | * |
||
| 289 | * @return string[] |
||
| 290 | * a list of all available entity names with their group |
||
| 291 | */ |
||
| 292 | 10 | public function getEntitiesNavBar() { |
|
| 293 | 10 | $result = []; |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Determines the Twig template to use for the given parameters depending on |
||
| 307 | * the existance of certain keys in the Container $app in this order: |
||
| 308 | * |
||
| 309 | * crud.$section.$action.$entity |
||
| 310 | * crud.$section.$action |
||
| 311 | * crud.$section |
||
| 312 | * |
||
| 313 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
| 314 | * |
||
| 315 | * @param Container $app |
||
| 316 | * the Silex application |
||
| 317 | * @param string $section |
||
| 318 | * the section of the template, either "layout" or "template" |
||
| 319 | * @param string $action |
||
| 320 | * the current calling action like "create" or "show" |
||
| 321 | * @param string $entity |
||
| 322 | * the current calling entity |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | * the best fitting template |
||
| 326 | */ |
||
| 327 | 11 | public function getTemplate(Container $app, $section, $action, $entity) { |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Sets the locale to be used. |
||
| 348 | * |
||
| 349 | * @param string $locale |
||
| 350 | * the locale to be used. |
||
| 351 | */ |
||
| 352 | 10 | public function setLocale($locale) { |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Gets the available locales. |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | * the available locales |
||
| 363 | */ |
||
| 364 | 78 | public function getLocales() { |
|
| 381 | |||
| 382 | } |
||
| 383 |