KyleMassacre /
laravel-menus
| 1 | <?php |
||
| 2 | |||
| 3 | namespace KyleMassacre\Menus; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Countable; |
||
| 7 | use Illuminate\Contracts\Config\Repository; |
||
| 8 | use Illuminate\View\Factory; |
||
| 9 | |||
| 10 | class Menu implements Countable |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The menus collections. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $menus = array(); |
||
| 18 | /** |
||
| 19 | * @var Repository |
||
| 20 | */ |
||
| 21 | private $config; |
||
| 22 | /** |
||
| 23 | * @var Factory |
||
| 24 | */ |
||
| 25 | private $views; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The constructor. |
||
| 29 | * |
||
| 30 | * @param Factory $views |
||
| 31 | * @param Repository $config |
||
| 32 | */ |
||
| 33 | 33 | public function __construct(Factory $views, Repository $config) |
|
| 34 | { |
||
| 35 | 33 | $this->views = $views; |
|
| 36 | 33 | $this->config = $config; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Make new menu. |
||
| 41 | * |
||
| 42 | * @param string $name |
||
| 43 | * @param Closure $callback |
||
| 44 | * |
||
| 45 | * @return \KyleMassacre\Menus\MenuBuilder |
||
| 46 | */ |
||
| 47 | 2 | public function make($name, \Closure $callback) |
|
| 48 | { |
||
| 49 | 2 | return $this->create($name, $callback); |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Create new menu. |
||
| 54 | * |
||
| 55 | * @param string $name |
||
| 56 | * @param Callable $resolver |
||
| 57 | * |
||
| 58 | * @return \KyleMassacre\Menus\MenuBuilder |
||
| 59 | */ |
||
| 60 | 11 | public function create($name, Closure $resolver) |
|
| 61 | { |
||
| 62 | 11 | $builder = new MenuBuilder($name, $this->config); |
|
| 63 | |||
| 64 | 11 | $builder->setViewFactory($this->views); |
|
| 65 | |||
| 66 | 11 | $this->menus[$name] = $builder; |
|
| 67 | |||
| 68 | 11 | return $resolver($builder); |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Check if the menu exists. |
||
| 73 | * |
||
| 74 | * @param string $name |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | 7 | public function has($name) |
|
| 79 | { |
||
| 80 | 7 | return array_key_exists($name, $this->menus); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get instance of the given menu if exists. |
||
| 85 | * |
||
| 86 | * @param string $name |
||
| 87 | * |
||
| 88 | * @return string|null |
||
| 89 | */ |
||
| 90 | 2 | public function instance($name) |
|
| 91 | { |
||
| 92 | 2 | return $this->has($name) ? $this->menus[$name] : null; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Modify a specific menu. |
||
| 97 | * |
||
| 98 | * @param string $name |
||
| 99 | * @param Closure $callback |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | 1 | public function modify($name, Closure $callback) |
|
| 103 | { |
||
| 104 | 1 | $menu = collect($this->menus)->filter(function ($menu) use ($name) { |
|
| 105 | 1 | return $menu->getName() == $name; |
|
| 106 | 1 | })->first(); |
|
| 107 | |||
| 108 | 1 | $callback($menu); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Render the menu tag by given name. |
||
| 113 | * |
||
| 114 | * @param string $name |
||
| 115 | * @param string $presenter |
||
| 116 | * |
||
| 117 | * @return string|null |
||
| 118 | */ |
||
| 119 | 5 | public function get($name, $presenter = null, $bindings = array()) |
|
| 120 | { |
||
| 121 | 5 | return $this->has($name) ? |
|
| 122 | 5 | $this->menus[$name]->setBindings($bindings)->render($presenter) : null; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Render the menu tag by given name. |
||
| 127 | * |
||
| 128 | * @param $name |
||
| 129 | * @param null $presenter |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | 1 | public function render($name, $presenter = null, $bindings = array()) |
|
| 134 | { |
||
| 135 | 1 | return $this->get($name, $presenter, $bindings); |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get a stylesheet for enable multilevel menu. |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | 1 | public function style() |
|
| 144 | { |
||
| 145 | 1 | return $this->views->make('menus::style')->render(); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get all menus. |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | 2 | public function all() |
|
| 154 | { |
||
| 155 | 2 | return $this->menus; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get count from all menus. |
||
| 160 | * |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | 1 | public function count() |
|
| 164 | { |
||
| 165 | 1 | return count($this->menus); |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Empty the current menus. |
||
| 170 | */ |
||
| 171 | 1 | public function destroy() |
|
| 172 | { |
||
| 173 | 1 | $this->menus = array(); |
|
| 174 | } |
||
| 175 | } |
||
| 176 |