AdminPoolLoader.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. class AdminPoolLoader extends FileLoader
  17. {
  18. /**
  19. * @var Bundle\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 $adminServiceIds
  30. */
  31. public function __construct(Pool $pool, $adminServiceIds, $container)
  32. {
  33. $this->pool = $pool;
  34. $this->adminServiceIds = $adminServiceIds;
  35. $this->container = $container;
  36. }
  37. /**
  38. * @param string $resource
  39. * @param null $type
  40. * @return bool
  41. */
  42. public function supports($resource, $type = null)
  43. {
  44. if ($type == 'sonata_admin') {
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * @param string $resource
  51. * @param null $type
  52. * @return \Symfony\Component\Routing\RouteCollection
  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. }