1: <?php
2: namespace Menu;
3:
4: use Illuminate\Config\FileLoader;
5: use Illuminate\Config\Repository;
6: use Illuminate\Container\Container;
7: use Illuminate\Http\Request;
8: use Menu\Items\ItemList;
9:
10: /**
11: * Basic interface to different components within the package
12: */
13: class Menu
14: {
15:
16: /**
17: * The current IoC container
18: * @var Container
19: */
20: protected static $container;
21:
22: /**
23: * All the registered names and the associated ItemLists
24: *
25: * @var array
26: */
27: protected static $itemLists = array();
28:
29: /**
30: * Get a MenuHandler.
31: *
32: * This method will retrieve ItemLists by name,
33: * If an ItemList doesn't already exist, it will
34: * be registered and added to the handler.
35: *
36: * <code>
37: * // Get the menu handler that handles the default name
38: * $handler = Menu::handler();
39: *
40: * // Get a named menu handler for a single name
41: * $handler = Menu::handler('backend');
42: *
43: * // Get a menu handler that handles multiple names
44: * $handler = Menu::handler(array('admin', 'sales'));
45: * </code>
46: *
47: * @param string|array $names The name this handler should respond to
48: * @param array $attributes Its attributes
49: * @param string $element Its element
50: *
51: * @return MenuHandler
52: */
53: public static function handler($names = '', $attributes = array(), $element = 'ul')
54: {
55: $names = (array) $names;
56:
57: $itemLists = array();
58: // Create a new ItemList instance for the names that don't exist yet
59: foreach ($names as $name) {
60: if (!array_key_exists($name, static::$itemLists)) {
61: $itemList = new ItemList(array(), $name, $attributes, $element);
62: static::setItemList($name, $itemList);
63: }
64: else {
65: $itemList = static::getItemList($name);
66: }
67:
68: $itemLists[] = $itemList;
69: }
70:
71: // Return a Handler for the item lists
72: return new MenuHandler($itemLists);
73: }
74:
75: /**
76: * Get a MenuHandler for all registered ItemLists
77: *
78: * @return MenuHandler
79: */
80: public static function allHandlers()
81: {
82: return new MenuHandler(static::$itemLists);
83: }
84:
85: /**
86: * Erase all menus in memory
87: */
88: public static function reset()
89: {
90: static::$itemLists = array();
91: }
92:
93: ////////////////////////////////////////////////////////////////////
94: //////////////////////// ITEM LISTS MANAGING ///////////////////////
95: ////////////////////////////////////////////////////////////////////
96:
97: /**
98: * Create a new ItemList
99: *
100: * @param string $name The name of the ItemList
101: * @param array $attributes The HTML attributes for the list element
102: * @param string $element The HTML element for the list (ul or dd)
103: *
104: * @return ItemList
105: */
106: public static function items($name = null, $attributes = array(), $element = 'ul')
107: {
108: return new ItemList(array(), $name, $attributes, $element);
109: }
110:
111: /**
112: * Store an ItemList in memory
113: *
114: * @param string $name The handle to store it to
115: * @param ItemList $itemList
116: *
117: * @return ItemList
118: */
119: public static function setItemList($name, $itemList)
120: {
121: static::$itemLists[$name] = $itemList;
122:
123: return $itemList;
124: }
125:
126: /**
127: * Get an ItemList from the memory
128: *
129: * @param string $name The ItemList handle
130: *
131: * @return ItemList
132: */
133: public static function getItemList($name = null)
134: {
135: if (is_null($name)) return static::$itemLists;
136: return static::$itemLists[$name];
137: }
138:
139: ////////////////////////////////////////////////////////////////////
140: /////////////////////////// MAGIC METHODS //////////////////////////
141: ////////////////////////////////////////////////////////////////////
142:
143: /**
144: * Magic Method for calling methods on the default handler.
145: *
146: * <code>
147: * // Call the "render" method on the default handler
148: * echo Menu::render();
149: *
150: * // Call the "add" method on the default handler
151: * Menu::add('home', 'Home');
152: * </code>
153: *
154: * @param string $method
155: * @param array $parameters
156: *
157: * @return mixed
158: */
159: public static function __callStatic($method, $parameters = array())
160: {
161: return call_user_func_array(array(static::handler(), $method), $parameters);
162: }
163:
164: ////////////////////////////////////////////////////////////////////
165: /////////////////////// DEPENDENCY INJECTIONS //////////////////////
166: ////////////////////////////////////////////////////////////////////
167:
168: /**
169: * Get the current dependencies
170: *
171: * @param string $dependency A dependency to make on the fly
172: *
173: * @return Container
174: */
175: public static function getContainer($dependency = null)
176: {
177: if (!static::$container) {
178: $container = new Container;
179:
180: // Create HTML
181: $container->bindIf('html', 'LaravelBook\Laravel4Powerpack\HTML');
182:
183: // Create basic Request instance to use
184: $container->alias('Symfony\Component\HttpFoundation\Request', 'request');
185: $container->bindIf('Symfony\Component\HttpFoundation\Request', function() {
186: return Request::createFromGlobals();
187: });
188:
189: static::setContainer($container);
190: }
191:
192: // Shortcut for getting a dependency
193: if ($dependency) {
194: return static::$container->make($dependency);
195: }
196:
197: return static::$container;
198: }
199:
200: /**
201: * Set the Container to use
202: *
203: * @param Container $container
204: */
205: public static function setContainer($container)
206: {
207: static::$container = $container;
208: }
209:
210: /**
211: * Get an option from the options array
212: *
213: * @param string $option The option key
214: *
215: * @return mixed Its value
216: */
217: public static function getOption($option = null)
218: {
219: if ($option == null) {
220:
221: return static::getContainer('config')->get('menu');
222: }
223:
224: return static::getContainer('config')->get('menu.'.$option);
225: }
226:
227: /**
228: * Set a global option
229: *
230: * @param key $option The option
231: * @param mixed $value Its value
232: */
233: public static function setOption($option, $value)
234: {
235: if ($option == null) {
236: $option = 'config';
237: }
238: static::getContainer('config')->set('menu.'.$option, $value);
239: }
240:
241: }
242: