Menu.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Spatie\Menu\Laravel;
  3. use Illuminate\Contracts\Auth\Access\Gate;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Support\Traits\Macroable;
  6. use Spatie\Menu\Item;
  7. use Spatie\Menu\Menu as BaseMenu;
  8. class Menu extends BaseMenu implements Htmlable
  9. {
  10. use Macroable;
  11. /**
  12. * Set all relevant children active based on the current request's URL.
  13. *
  14. * /, /about, /contact => request to /about will set the about link active.
  15. *
  16. * /en, /en/about, /en/contact => request to /en won't set /en active if the
  17. * request root is set to /en.
  18. *
  19. * @param string $requestRoot If the link's URL is an exact match with the
  20. * request root, the link won't be set active.
  21. * This behavior is to avoid having home links
  22. * active on every request.
  23. *
  24. * @return $this
  25. */
  26. public function setActiveFromRequest(string $requestRoot = '/'): self
  27. {
  28. return $this->setActive(app('request')->url(), $requestRoot);
  29. }
  30. public function url(string $path, string $text, mixed $parameters = [], bool | null $secure = null): self
  31. {
  32. return $this->add(Link::toUrl($path, $text, $parameters, $secure));
  33. }
  34. public function action(string | array $action, string $text, mixed $parameters = [], bool $absolute = true): self
  35. {
  36. return $this->add(Link::toAction($action, $text, $parameters, $absolute));
  37. }
  38. public function route(string $name, string $text, mixed $parameters = [], bool $absolute = true): self
  39. {
  40. return $this->add(Link::toRoute($name, $text, $parameters, $absolute));
  41. }
  42. public function view(string $name, array $data = []): self
  43. {
  44. return $this->add(View::create($name, $data));
  45. }
  46. public function urlIf(bool $condition, string $path, string $text, array $parameters = [], bool | null $secure = null): self
  47. {
  48. return $this->addIf($condition, Link::toUrl($path, $text, $parameters, $secure));
  49. }
  50. public function actionIf(bool $condition, string | array $action, string $text, array $parameters = [], bool $absolute = true): self
  51. {
  52. return $this->addIf($condition, Link::toAction($action, $text, $parameters, $absolute));
  53. }
  54. public function routeIf(bool $condition, string $name, string $text, array $parameters = [], bool $absolute = true): self
  55. {
  56. return $this->addIf($condition, Link::toRoute($name, $text, $parameters, $absolute));
  57. }
  58. public function viewIf($condition, string $name, array | null $data = null): self
  59. {
  60. return $this->addIf($condition, View::create($name, $data));
  61. }
  62. public function addIfCan(string | array $authorization, Item $item): self
  63. {
  64. $abilityArguments = is_array($authorization) ? $authorization : [$authorization];
  65. $ability = array_shift($abilityArguments);
  66. return $this->addIf(app(Gate::class)->allows($ability, $abilityArguments), $item);
  67. }
  68. public function linkIfCan(string | array $authorization, string $url, string $text): self
  69. {
  70. return $this->addIfCan($authorization, Link::to($url, $text));
  71. }
  72. public function htmlIfCan(string | array $authorization, string $html): Menu
  73. {
  74. return $this->addIfCan($authorization, Html::raw($html));
  75. }
  76. public function submenuIfCan(string | array $authorization, callable | BaseMenu | Item $header, callable | BaseMenu | null $menu = null): self
  77. {
  78. [$authorization, $header, $menu] = $this->parseSubmenuIfCanArgs(...func_get_args());
  79. $menu = $this->createSubmenuMenu($menu);
  80. $header = $this->createSubmenuHeader($header);
  81. return $this->addIfCan($authorization, $menu->prependIf($header, $header));
  82. }
  83. protected function parseSubmenuIfCanArgs($authorization, ...$args): array
  84. {
  85. return array_merge([$authorization], $this->parseSubmenuArgs($args));
  86. }
  87. public function urlIfCan(string | array $authorization, string $path, string $text, array $parameters = [], bool | null $secure = null): self
  88. {
  89. return $this->addIfCan($authorization, Link::toUrl($path, $text, $parameters, $secure));
  90. }
  91. public function actionIfCan(string | array $authorization, string | array $action, string $text, array $parameters = [], bool $absolute = true): self
  92. {
  93. return $this->addIfCan($authorization, Link::toAction($action, $text, $parameters, $absolute));
  94. }
  95. public function routeIfCan(string | array $authorization, string $name, string $text, array $parameters = [], bool $absolute = true): self
  96. {
  97. return $this->addIfCan($authorization, Link::toRoute($name, $text, $parameters, $absolute));
  98. }
  99. /**
  100. * @internal param $condition
  101. */
  102. public function viewIfCan(string | array $authorization, string $name, array | null $data = null): self
  103. {
  104. return $this->addIfCan($authorization, View::create($name, $data));
  105. }
  106. public function toHtml(): string
  107. {
  108. return $this->render();
  109. }
  110. }