AdminPoolLoader.php 2.2 KB

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