AdminPoolLoader.php 2.2 KB

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