AdminPoolLoader.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Route;
  11. use Sonata\AdminBundle\Admin\Pool;
  12. use Symfony\Component\Config\Loader\Loader;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
  16. /**
  17. * Class AdminPoolLoader.
  18. *
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class AdminPoolLoader extends Loader
  22. {
  23. const ROUTE_TYPE_NAME = 'sonata_admin';
  24. /**
  25. * @var Pool
  26. */
  27. protected $pool;
  28. /**
  29. * @var array
  30. */
  31. protected $adminServiceIds = array();
  32. /**
  33. * @var ContainerInterface
  34. */
  35. protected $container;
  36. /**
  37. * @param Pool $pool
  38. * @param array $adminServiceIds
  39. * @param ContainerInterface $container
  40. */
  41. public function __construct(Pool $pool, array $adminServiceIds, ContainerInterface $container)
  42. {
  43. $this->pool = $pool;
  44. $this->adminServiceIds = $adminServiceIds;
  45. $this->container = $container;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function supports($resource, $type = null)
  51. {
  52. return $type === self::ROUTE_TYPE_NAME;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function load($resource, $type = null)
  58. {
  59. $collection = new SymfonyRouteCollection();
  60. foreach ($this->adminServiceIds as $id) {
  61. $admin = $this->pool->getInstance($id);
  62. foreach ($admin->getRoutes()->getElements() as $code => $route) {
  63. $collection->add($route->getDefault('_sonata_name'), $route);
  64. }
  65. $reflection = new \ReflectionObject($admin);
  66. if (file_exists($reflection->getFileName())) {
  67. $collection->addResource(new FileResource($reflection->getFileName()));
  68. }
  69. }
  70. $reflection = new \ReflectionObject($this->container);
  71. if (file_exists($reflection->getFileName())) {
  72. $collection->addResource(new FileResource($reflection->getFileName()));
  73. }
  74. return $collection;
  75. }
  76. }