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 |
||
33 | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
||
34 | { |
||
35 | |||
36 | /** |
||
37 | * Holds the data instances. |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $datas; |
||
41 | |||
42 | /** |
||
43 | * Holds the map for overriding templates. |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $templates = []; |
||
47 | |||
48 | /** |
||
49 | * Holds whether CRUDlex manages i18n. |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $manageI18n = true; |
||
53 | |||
54 | /** |
||
55 | * Initializes needed but yet missing service providers. |
||
56 | * |
||
57 | * @param Container $app |
||
58 | * the application container |
||
59 | */ |
||
60 | 78 | protected function initMissingServiceProviders(Container $app) |
|
79 | |||
80 | /** |
||
81 | * Initializes the available locales. |
||
82 | * |
||
83 | * @param Translator $translator |
||
84 | * the translator |
||
85 | * |
||
86 | * @return array |
||
87 | * the available locales |
||
88 | */ |
||
89 | 77 | protected function initLocales(Translator $translator) |
|
99 | |||
100 | /** |
||
101 | * Initializes the children of the data entries. |
||
102 | */ |
||
103 | 77 | protected function initChildren() |
|
114 | |||
115 | /** |
||
116 | * Gets a map with localized entity labels from the CRUD YML. |
||
117 | * |
||
118 | * @param array $locales |
||
119 | * the available locales |
||
120 | * @param array $crud |
||
121 | * the CRUD entity map |
||
122 | * |
||
123 | * @return array |
||
124 | * the map with localized entity labels |
||
125 | */ |
||
126 | 77 | protected function getLocaleLabels(array $locales, array $crud) |
|
136 | |||
137 | /** |
||
138 | * Configures the EntityDefinition according to the given |
||
139 | * CRUD entity map. |
||
140 | * |
||
141 | * @param EntityDefinition $definition |
||
142 | * the definition to configure |
||
143 | * @param array $crud |
||
144 | * the CRUD entity map |
||
145 | */ |
||
146 | 77 | protected function configureDefinition(EntityDefinition $definition, array $crud) |
|
167 | |||
168 | /** |
||
169 | * Creates and setups an EntityDefinition instance. |
||
170 | * |
||
171 | * @param Translator $translator |
||
172 | * the Translator to use for some standard field labels |
||
173 | * @param EntityDefinitionFactoryInterface $entityDefinitionFactory |
||
174 | * the EntityDefinitionFactory to use |
||
175 | * @param array $locales |
||
176 | * the available locales |
||
177 | * @param array $crud |
||
178 | * the parsed YAML of a CRUD entity |
||
179 | * @param string $name |
||
180 | * the name of the entity |
||
181 | * |
||
182 | * @return EntityDefinition |
||
183 | * the EntityDefinition good to go |
||
184 | */ |
||
185 | 77 | protected function createDefinition(Translator $translator, EntityDefinitionFactoryInterface $entityDefinitionFactory, 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 | 77 | protected function validateEntityDefinition(Container $app, array $entityDefinition) |
|
225 | |||
226 | /** |
||
227 | * Initializes the instance. |
||
228 | * |
||
229 | * @param string|null $crudFileCachingDirectory |
||
230 | * the writable directory to store the CRUD YAML file cache |
||
231 | * @param Container $app |
||
232 | * the application container |
||
233 | */ |
||
234 | 78 | public function init($crudFileCachingDirectory, Container $app) |
|
253 | |||
254 | /** |
||
255 | * Implements ServiceProviderInterface::register() registering $app['crud']. |
||
256 | * $app['crud'] contains an instance of the ServiceProvider afterwards. |
||
257 | * |
||
258 | * @param Container $app |
||
259 | * the Container instance of the Silex application |
||
260 | */ |
||
261 | 11 | public function register(Container $app) |
|
274 | |||
275 | /** |
||
276 | * Initializes the crud service right after boot. |
||
277 | * |
||
278 | * @param Application $app |
||
279 | * the Container instance of the Silex application |
||
280 | */ |
||
281 | 78 | public function boot(Application $app) |
|
287 | |||
288 | /** |
||
289 | * Getter for the AbstractData instances. |
||
290 | * |
||
291 | * @param string $name |
||
292 | * the entity name of the desired Data instance |
||
293 | * |
||
294 | * @return AbstractData |
||
295 | * the AbstractData instance or null on invalid name |
||
296 | */ |
||
297 | 70 | public function getData($name) |
|
304 | |||
305 | /** |
||
306 | * Getter for all available entity names. |
||
307 | * |
||
308 | * @return string[] |
||
309 | * a list of all available entity names |
||
310 | */ |
||
311 | 7 | public function getEntities() |
|
315 | |||
316 | /** |
||
317 | * Getter for the entities for the navigation bar. |
||
318 | * |
||
319 | * @return string[] |
||
320 | * a list of all available entity names with their group |
||
321 | */ |
||
322 | 10 | public function getEntitiesNavBar() |
|
335 | |||
336 | /** |
||
337 | * Sets a template to use instead of the build in ones. |
||
338 | * |
||
339 | * @param string $key |
||
340 | * the template key to use in this format: |
||
341 | * $section.$action.$entity |
||
342 | * $section.$action |
||
343 | * $section |
||
344 | * @param string $template |
||
345 | * the template to use for this key |
||
346 | */ |
||
347 | 12 | public function setTemplate($key, $template) |
|
351 | |||
352 | /** |
||
353 | * Determines the Twig template to use for the given parameters depending on |
||
354 | * the existance of certain template keys set in this order: |
||
355 | * |
||
356 | * $section.$action.$entity |
||
357 | * $section.$action |
||
358 | * $section |
||
359 | * |
||
360 | * If nothing exists, this string is returned: "@crud/<action>.twig" |
||
361 | * |
||
362 | * @param string $section |
||
363 | * the section of the template, either "layout" or "template" |
||
364 | * @param string $action |
||
365 | * the current calling action like "create" or "show" |
||
366 | * @param string $entity |
||
367 | * the current calling entity |
||
368 | * |
||
369 | * @return string |
||
370 | * the best fitting template |
||
371 | */ |
||
372 | 11 | public function getTemplate($section, $action, $entity) |
|
390 | |||
391 | /** |
||
392 | * Sets the locale to be used. |
||
393 | * |
||
394 | * @param string $locale |
||
395 | * the locale to be used. |
||
396 | */ |
||
397 | 10 | public function setLocale($locale) |
|
403 | |||
404 | /** |
||
405 | * Gets the available locales. |
||
406 | * |
||
407 | * @return array |
||
408 | * the available locales |
||
409 | */ |
||
410 | 78 | public function getLocales() |
|
428 | |||
429 | /** |
||
430 | * Gets whether CRUDlex manages the i18n. |
||
431 | * @return bool |
||
432 | * true if so |
||
433 | */ |
||
434 | 11 | public function isManageI18n() |
|
438 | |||
439 | /** |
||
440 | * Sets whether CRUDlex manages the i18n. |
||
441 | * @param bool $manageI18n |
||
442 | * true if so |
||
443 | */ |
||
444 | 1 | public function setManageI18n($manageI18n) |
|
448 | |||
449 | } |
||
450 |