AdminPoolLoader.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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;
  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. /**
  27. * @param \Sonata\AdminBundle\Admin\Pool $pool
  28. * @param $adminServiceIds
  29. */
  30. public function __construct(Pool $pool, $adminServiceIds)
  31. {
  32. $this->pool = $pool;
  33. $this->adminServiceIds = $adminServiceIds;
  34. }
  35. /**
  36. * @param string $resource
  37. * @param null $type
  38. * @return bool
  39. */
  40. function supports($resource, $type = null)
  41. {
  42. if ($type == 'sonata_admin') {
  43. return true;
  44. }
  45. return false;
  46. }
  47. /**
  48. * @param string $resource
  49. * @param null $type
  50. * @return \Symfony\Component\Routing\RouteCollection
  51. */
  52. function load($resource, $type = null)
  53. {
  54. $collection = new RouteCollection;
  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. return $collection;
  64. }
  65. }