MicroKernelTrait.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Kernel;
  11. use Symfony\Component\Config\Loader\LoaderInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\Routing\RouteCollectionBuilder;
  14. /**
  15. * A Kernel that provides configuration hooks.
  16. *
  17. * @author Ryan Weaver <ryan@knpuniversity.com>
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. trait MicroKernelTrait
  21. {
  22. /**
  23. * Add or import routes into your application.
  24. *
  25. * $routes->import('config/routing.yml');
  26. * $routes->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
  27. *
  28. * @param RouteCollectionBuilder $routes
  29. */
  30. abstract protected function configureRoutes(RouteCollectionBuilder $routes);
  31. /**
  32. * Configures the container.
  33. *
  34. * You can register extensions:
  35. *
  36. * $c->loadFromExtension('framework', array(
  37. * 'secret' => '%secret%'
  38. * ));
  39. *
  40. * Or services:
  41. *
  42. * $c->register('halloween', 'FooBundle\HalloweenProvider');
  43. *
  44. * Or parameters:
  45. *
  46. * $c->setParameter('halloween', 'lot of fun');
  47. *
  48. * @param ContainerBuilder $c
  49. * @param LoaderInterface $loader
  50. */
  51. abstract protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader);
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function registerContainerConfiguration(LoaderInterface $loader)
  56. {
  57. $loader->load(function (ContainerBuilder $container) use ($loader) {
  58. $container->loadFromExtension('framework', array(
  59. 'router' => array(
  60. 'resource' => 'kernel:loadRoutes',
  61. 'type' => 'service',
  62. ),
  63. ));
  64. $this->configureContainer($container, $loader);
  65. $container->addObjectResource($this);
  66. });
  67. }
  68. /**
  69. * @internal
  70. */
  71. public function loadRoutes(LoaderInterface $loader)
  72. {
  73. $routes = new RouteCollectionBuilder($loader);
  74. $this->configureRoutes($routes);
  75. return $routes->build();
  76. }
  77. }