AdminPoolLoader.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\BaseApplicationBundle\Route;
  11. use Symfony\Component\Routing\RouteCollection;
  12. use Symfony\Component\Routing\Route;
  13. use Symfony\Component\Routing\Loader\Loader;
  14. use Symfony\Component\Routing\Resource\FileResource;
  15. use Sonata\BaseApplicationBundle\Admin\Pool;
  16. class AdminPoolLoader extends Loader
  17. {
  18. /**
  19. * @var Bundle\Soanta\BaseApplicationBundle\Admin\Pool
  20. */
  21. protected $pool;
  22. public function __construct(Pool $pool)
  23. {
  24. $this->pool = $pool;
  25. }
  26. function supports($resource, $type = null)
  27. {
  28. if (substr($resource, -22) == 'base_application.admin') {
  29. return true;
  30. }
  31. return false;
  32. }
  33. function load($resource, $type = null)
  34. {
  35. $collection = new RouteCollection;
  36. foreach ($this->pool->getInstances() as $admin) {
  37. foreach ($admin->getUrls() as $action => $configuration) {
  38. $default = isset($configuration['defaults']) ? $configuration['defaults'] : array();
  39. if(!isset($default['_controller'])) {
  40. $default['_controller'] = sprintf('%s:%s', $admin->getBaseControllerName(), $action);
  41. }
  42. $collection->add($configuration['name'], new Route(
  43. $configuration['pattern'],
  44. isset($configuration['defaults']) ? $configuration['defaults'] : array('_controller'),
  45. isset($configuration['requirements']) ? $configuration['requirements'] : array(),
  46. isset($configuration['options']) ? $configuration['options'] : array()
  47. ));
  48. }
  49. $reflection = new \ReflectionObject($admin);
  50. $collection->addResource(new FileResource($reflection->getFileName()));
  51. }
  52. return $collection;
  53. }
  54. }