AdminPoolLoader.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\FileLoader;
  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 FileLoader
  22. {
  23. const ROUTE_TYPE_NAME = 'sonata_admin';
  24. /**
  25. * @var \Sonata\AdminBundle\Admin\Pool
  26. */
  27. protected $pool;
  28. /**
  29. * @var array
  30. */
  31. protected $adminServiceIds = array();
  32. protected $container;
  33. /**
  34. * @param \Sonata\AdminBundle\Admin\Pool $pool
  35. * @param array $adminServiceIds
  36. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  37. */
  38. public function __construct(Pool $pool, array $adminServiceIds, ContainerInterface $container)
  39. {
  40. $this->pool = $pool;
  41. $this->adminServiceIds = $adminServiceIds;
  42. $this->container = $container;
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function supports($resource, $type = null)
  48. {
  49. return $type === self::ROUTE_TYPE_NAME;
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function load($resource, $type = null)
  55. {
  56. $collection = new SymfonyRouteCollection();
  57. foreach ($this->adminServiceIds as $id) {
  58. $admin = $this->pool->getInstance($id);
  59. foreach ($admin->getRoutes()->getElements() as $code => $route) {
  60. $collection->add($route->getDefault('_sonata_name'), $route);
  61. }
  62. $reflection = new \ReflectionObject($admin);
  63. $collection->addResource(new FileResource($reflection->getFileName()));
  64. }
  65. $reflection = new \ReflectionObject($this->container);
  66. $collection->addResource(new FileResource($reflection->getFileName()));
  67. return $collection;
  68. }
  69. }