BuilderAliasProvider.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Knp\Bundle\MenuBundle\Provider;
  3. use Knp\Menu\FactoryInterface;
  4. use Knp\Menu\ItemInterface;
  5. use Knp\Menu\Provider\MenuProviderInterface;
  6. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. /**
  11. * A menu provider that allows for an AcmeBundle:Builder:mainMenu shortcut syntax
  12. *
  13. * @author Ryan Weaver <ryan@knplabs.com>
  14. */
  15. class BuilderAliasProvider implements MenuProviderInterface
  16. {
  17. private $kernel;
  18. private $container;
  19. private $menuFactory;
  20. private $builders = array();
  21. public function __construct(KernelInterface $kernel, ContainerInterface $container, FactoryInterface $menuFactory)
  22. {
  23. $this->kernel = $kernel;
  24. $this->container = $container;
  25. $this->menuFactory = $menuFactory;
  26. }
  27. /**
  28. * Looks for a menu with the bundle:class:method format
  29. *
  30. * For example, AcmeBundle:Builder:mainMenu would create and instantiate
  31. * an Acme\DemoBundle\Menu\Builder class and call the mainMenu() method
  32. * on it. The method is passed the menu factory.
  33. *
  34. * @param string $name The alias name of the menu
  35. * @param array $options
  36. * @return \Knp\Menu\ItemInterface
  37. * @throws \InvalidArgumentException
  38. */
  39. public function get($name, array $options = array())
  40. {
  41. if (!$this->has($name)) {
  42. throw new \InvalidArgumentException(sprintf('Invalid pattern passed to AliasProvider - expected "bundle:class:method", got "%s".', $name));
  43. }
  44. list($bundleName, $className, $methodName) = explode(':', $name);
  45. $builder = $this->getBuilder($bundleName, $className);
  46. if (!method_exists($builder, $methodName)) {
  47. throw new \InvalidArgumentException(sprintf('Method "%s" was not found on class "%s" when rendering the "%s" menu.', $methodName, $className, $name));
  48. }
  49. $menu = $builder->$methodName($this->menuFactory, $options);
  50. if (!$menu instanceof ItemInterface) {
  51. throw new \InvalidArgumentException(sprintf('Method "%s" did not return an ItemInterface menu object for menu "%s"', $methodName, $name));
  52. }
  53. return $menu;
  54. }
  55. /**
  56. * Verifies if the given name follows the bundle:class:method alias syntax.
  57. *
  58. * @param string $name The alias name of the menu
  59. * @param array $options
  60. * @return Boolean
  61. */
  62. public function has($name, array $options = array())
  63. {
  64. return 2 == substr_count($name, ':');
  65. }
  66. /**
  67. * Creates and returns the builder that lives in the given bundle
  68. *
  69. * The convention is to look in the Menu namespace of the bundle for
  70. * this class, to instantiate it with no arguments, and to inject the
  71. * container if the class is ContainerAware.
  72. *
  73. * @param string $bundleName
  74. * @param string $className The class name of the builder
  75. * @throws \InvalidArgumentException If the class does not exist
  76. */
  77. protected function getBuilder($bundleName, $className)
  78. {
  79. $name = sprintf('%s:%s', $bundleName, $className);
  80. if (!isset($this->builders[$name])) {
  81. $class = null;
  82. $logs = array();
  83. $bundles = array();
  84. foreach ($this->kernel->getBundle($bundleName, false) as $bundle) {
  85. $try = $bundle->getNamespace().'\\Menu\\'.$className;
  86. if (class_exists($try)) {
  87. $class = $try;
  88. break;
  89. }
  90. $logs[] = sprintf('Class "%s" does not exist for menu builder "%s".', $try, $name);
  91. $bundles[] = $bundle->getName();
  92. }
  93. if (null === $class) {
  94. if (1 === count($logs)) {
  95. throw new \InvalidArgumentException($logs[0]);
  96. }
  97. throw new \InvalidArgumentException(sprintf('Unable to find menu builder "%s" in bundles %s.', $name, implode(', ', $bundles)));
  98. }
  99. $builder = new $class();
  100. if ($builder instanceof ContainerAwareInterface) {
  101. $builder->setContainer($this->container);
  102. }
  103. $this->builders[$name] = $builder;
  104. }
  105. return $this->builders[$name];
  106. }
  107. }