AdminPoolLoader.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Config\Loader\FileLoader;
  14. use Symfony\Component\Config\Resource\FileResource;
  15. use Sonata\BaseApplicationBundle\Admin\Pool;
  16. class AdminPoolLoader extends FileLoader
  17. {
  18. /**
  19. * @var Bundle\Sonata\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 ($type == 'sonata_base_application') {
  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. $defaults = isset($configuration['defaults']) ? $configuration['defaults'] : array();
  39. if(!isset($defaults['_controller'])) {
  40. $defaults['_controller'] = sprintf('%s:%s', $admin->getBaseControllerName(), $this->actionify($action));
  41. }
  42. $defaults['_bab_action'] = sprintf('%s.%s', $admin->getCode(), $action);
  43. $collection->add($configuration['name'], new Route(
  44. $configuration['pattern'],
  45. $defaults,
  46. isset($configuration['requirements']) ? $configuration['requirements'] : array(),
  47. isset($configuration['options']) ? $configuration['options'] : array()
  48. ));
  49. }
  50. $reflection = new \ReflectionObject($admin);
  51. $collection->addResource(new FileResource($reflection->getFileName()));
  52. }
  53. return $collection;
  54. }
  55. /**
  56. * Convert a word in to the format for a symfony action action_name => actionName
  57. *
  58. * @param string $word Word to actionify
  59. * @return string $word Actionified word
  60. */
  61. public static function actionify($word)
  62. {
  63. return lcfirst(str_replace(' ', '', ucwords(strtr($word, '_-', ' '))));
  64. }
  65. }