AdminPoolLoader.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $collection->addResource(new FileResource($reflection->getFileName()));
  67. }
  68. $reflection = new \ReflectionObject($this->container);
  69. $collection->addResource(new FileResource($reflection->getFileName()));
  70. return $collection;
  71. }
  72. }