AdminPoolLoader.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. public function __construct(Pool $pool, $adminServiceIds)
  27. {
  28. $this->pool = $pool;
  29. $this->adminServiceIds = $adminServiceIds;
  30. }
  31. function supports($resource, $type = null)
  32. {
  33. if ($type == 'sonata_admin') {
  34. return true;
  35. }
  36. return false;
  37. }
  38. function load($resource, $type = null)
  39. {
  40. $collection = new RouteCollection;
  41. foreach ($this->adminServiceIds as $id) {
  42. $admin = $this->pool->getInstance($id);
  43. foreach ($admin->getUrls() as $action => $configuration) {
  44. $defaults = isset($configuration['defaults']) ? $configuration['defaults'] : array();
  45. if (!isset($defaults['_controller'])) {
  46. $defaults['_controller'] = sprintf('%s:%s', $admin->getBaseControllerName(), $this->actionify($action));
  47. }
  48. if (!isset($defaults['_sonata_admin'])) {
  49. $defaults['_sonata_admin'] = $admin->getBaseCodeRoute();
  50. }
  51. $collection->add($configuration['name'], new Route(
  52. $configuration['pattern'],
  53. $defaults,
  54. isset($configuration['requirements']) ? $configuration['requirements'] : array(),
  55. isset($configuration['options']) ? $configuration['options'] : array()
  56. ));
  57. }
  58. $reflection = new \ReflectionObject($admin);
  59. $collection->addResource(new FileResource($reflection->getFileName()));
  60. }
  61. return $collection;
  62. }
  63. /**
  64. * Convert a word in to the format for a symfony action action_name => actionName
  65. *
  66. * @param string $word Word to actionify
  67. * @return string $word Actionified word
  68. */
  69. public static function actionify($action)
  70. {
  71. if(($pos = strrpos($action, '.')) !== false) {
  72. $action = substr($action, $pos + 1);
  73. }
  74. return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', ' '))));
  75. }
  76. }