AdminPoolLoader.php 2.2 KB

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