Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 10 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 11 | use Symfony\Component\HttpKernel\Kernel as BaseKernel; |
||
| 12 | use Symfony\Component\Routing\RouteCollectionBuilder; |
||
| 13 | |||
| 14 | class Kernel extends BaseKernel |
||
| 15 | { |
||
| 16 | use MicroKernelTrait; |
||
| 17 | |||
| 18 | private const CONFIG_EXTS = '.{php,xml,yaml,yml}'; |
||
| 19 | |||
| 20 | public function registerBundles(): iterable |
||
| 29 | |||
| 30 | protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void |
||
| 31 | { |
||
| 32 | $container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php')); |
||
| 33 | $container->setParameter('container.dumper.inline_class_loader', true); |
||
| 34 | $confDir = $this->getProjectDir().'/config'; |
||
| 35 | |||
| 36 | $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob'); |
||
| 37 | $loader->load($confDir.'/{packages}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob'); |
||
| 38 | $loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob'); |
||
| 39 | $loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob'); |
||
| 40 | } |
||
| 41 | |||
| 42 | protected function configureRoutes(RouteCollectionBuilder $routes): void |
||
| 43 | { |
||
| 44 | $confDir = $this->getProjectDir().'/config'; |
||
| 45 | |||
| 46 | $routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob'); |
||
| 47 | $routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob'); |
||
| 48 | $routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob'); |
||
| 49 | } |
||
| 50 | } |
||
| 51 |