AdminPoolLoader.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  18. */
  19. class AdminPoolLoader extends Loader
  20. {
  21. const ROUTE_TYPE_NAME = 'sonata_admin';
  22. /**
  23. * @var Pool
  24. */
  25. protected $pool;
  26. /**
  27. * @var array
  28. */
  29. protected $adminServiceIds = array();
  30. /**
  31. * @var ContainerInterface
  32. */
  33. protected $container;
  34. /**
  35. * @param Pool $pool
  36. * @param array $adminServiceIds
  37. * @param ContainerInterface $container
  38. */
  39. public function __construct(Pool $pool, array $adminServiceIds, ContainerInterface $container)
  40. {
  41. $this->pool = $pool;
  42. $this->adminServiceIds = $adminServiceIds;
  43. $this->container = $container;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function supports($resource, $type = null)
  49. {
  50. return $type === self::ROUTE_TYPE_NAME;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function load($resource, $type = null)
  56. {
  57. $collection = new SymfonyRouteCollection();
  58. foreach ($this->adminServiceIds as $id) {
  59. $admin = $this->pool->getInstance($id);
  60. foreach ($admin->getRoutes()->getElements() as $code => $route) {
  61. $collection->add($route->getDefault('_sonata_name'), $route);
  62. }
  63. $reflection = new \ReflectionObject($admin);
  64. if (file_exists($reflection->getFileName())) {
  65. $collection->addResource(new FileResource($reflection->getFileName()));
  66. }
  67. }
  68. $reflection = new \ReflectionObject($this->container);
  69. if (file_exists($reflection->getFileName())) {
  70. $collection->addResource(new FileResource($reflection->getFileName()));
  71. }
  72. return $collection;
  73. }
  74. }