Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 424 lines (372 sloc) 9.693 kb
97fcea1 @taylorotwell cleaned up paths.
taylorotwell authored
1 <?php namespace Laravel; defined('DS') or die('No direct script access.');
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
2
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
3 use FilesystemIterator as fIterator;
4
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
5 class Bundle {
6
7 /**
8 * All of the application's bundles.
9 *
10 * @var array
11 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
12 public static $bundles = array();
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
13
14 /**
15 * A cache of the parsed bundle elements.
16 *
17 * @var array
18 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
19 public static $elements = array();
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
20
21 /**
22 * All of the bundles that have been started.
23 *
24 * @var array
25 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
26 public static $started = array();
27
28 /**
78dc452 @taylorotwell added routed array.
taylorotwell authored
29 * All of the bundles that have their routes files loaded.
30 *
31 * @var array
32 */
33 public static $routed = array();
34
35 /**
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
36 * Detect all of the installed bundles from disk.
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
37 *
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
38 * @param string $path
39 * @return array
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
40 */
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
41 public static function detect($path)
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
42 {
fd1b63e @taylorotwell removing bundles.php file.
taylorotwell authored
43 return static::search($path);
4282577 @taylorotwell clean up base directory of phpunit stuff.
taylorotwell authored
44 }
45
46 /**
47 * Detect all of the installed bundles from disk.
48 *
49 * @param string $path
50 * @return array
51 */
52 protected static function search($path)
53 {
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
54 $bundles = array();
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
55
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
56 $items = new fIterator($path);
7008250 @taylorotwell improve bundle configuration and registration.
taylorotwell authored
57
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
58 foreach ($items as $item)
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
59 {
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
60 // If the item is a directory, we'll search for a bundle.info file.
61 // If one exists, we will add it to the bundle array. We will set
62 // the location automatically since we know it.
63 if ($item->isDir())
64 {
65 $path = $item->getRealPath().DS.'bundle.info';
66
67 // If we found a file, we'll require in the array it contains
68 // and add it to the directory. The info array will contain
69 // basic info like the bundle name and any URIs it may
70 // handle incoming requests for.
71 if (file_exists($path))
72 {
73 $info = require $path;
74
75 $info['location'] = dirname($path).DS;
76
77 $bundles[$info['name']] = $info;
78
79 continue;
80 }
81 // If a bundle.info file doesn't exist within a directory,
82 // we'll recurse into the directory to keep searching in
83 // the bundle directory for nested bundles.
84 else
85 {
86 $recurse = static::detect($item->getRealPath());
87
88 $bundles = array_merge($bundles, $recurse);
89 }
90 }
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
91 }
92
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
93 return $bundles;
94 }
95
96 /**
97 * Register a bundle for the application.
98 *
99 * @param array $config
100 * @return void
101 */
102 public static function register($config)
103 {
104 $defaults = array('handles' => null, 'auto' => false);
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
105
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
106 // If a handles clause has been specified, we will cap it with a trailing
107 // slash so the bundle is not extra greedy with its routes. Otherwise a
108 // bundle that handles "s" would handle all routes beginning with "s".
37c06a5 @taylorotwell fix bundle handles greediness.
taylorotwell authored
109 if (isset($config['handles']))
110 {
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
111 $config['handles'] = str_finish($config['handles'], '/');
37c06a5 @taylorotwell fix bundle handles greediness.
taylorotwell authored
112 }
113
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
114 static::$bundles[$config['name']] = array_merge($defaults, $config);
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
115 }
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
116
117 /**
e4d55cb @taylorotwell added bundle::disable method.
taylorotwell authored
118 * Disable a bundle for the current request.
119 *
120 * @param string $bundle
121 * @return void
122 */
123 public static function disable($bundle)
124 {
125 unset(static::$bundles[$bundle]);
126 }
127
128 /**
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
129 * Load a bundle by running it's start-up script.
130 *
131 * If the bundle has already been started, no action will be taken.
132 *
133 * @param string $bundle
134 * @return void
135 */
136 public static function start($bundle)
137 {
138 if (static::started($bundle)) return;
139
a113188 @taylorotwell improving bundle workflow.
taylorotwell authored
140 if ( ! static::exists($bundle))
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
141 {
142 throw new \Exception("Bundle [$bundle] has not been installed.");
143 }
144
145 // Each bundle may have a "start" script which is responsible for preparing
146 // the bundle for use by the application. The start script may register any
147 // classes the bundle uses with the auto-loader, or perhaps will start any
148 // dependent bundles so that they are available.
149 if (file_exists($path = static::path($bundle).'bundle'.EXT))
150 {
a4acb1c @taylorotwell require routes as many times as necessary.
taylorotwell authored
151 require $path;
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
152 }
153
154 // Each bundle may also have a "routes" file which is responsible for
155 // registering the bundle's routes. This is kept separate from the
156 // start script for reverse routing efficiency purposes.
157 static::routes($bundle);
67696d8 @taylorotwell index names may be specified on fluent setup.
taylorotwell authored
158
b5f9f9b @taylorotwell added event firing when bundle is started.
taylorotwell authored
159 Event::fire("started: {$bundle}");
160
67696d8 @taylorotwell index names may be specified on fluent setup.
taylorotwell authored
161 static::$started[] = strtolower($bundle);
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
162 }
163
164 /**
165 * Load the "routes" file for a given bundle.
166 *
167 * @param string $bundle
168 * @return void
169 */
170 public static function routes($bundle)
171 {
78dc452 @taylorotwell added routed array.
taylorotwell authored
172 $path = static::path($bundle).'routes'.EXT;
173
174 if ( ! static::routed($bundle) and file_exists($path))
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
175 {
a4acb1c @taylorotwell require routes as many times as necessary.
taylorotwell authored
176 require $path;
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
177 }
78dc452 @taylorotwell added routed array.
taylorotwell authored
178
179 static::$routed[] = $bundle;
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
180 }
181
182 /**
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
183 * Determine which bundle handles the given URI.
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
184 *
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
185 * If no bundle is assigned to handle the URI, the default bundle is returned.
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
186 *
62072e5 @sparksp PHPDoc fixes
sparksp authored
187 * @param string $uri
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
188 * @return string
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
189 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
190 public static function handles($uri)
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
191 {
37c06a5 @taylorotwell fix bundle handles greediness.
taylorotwell authored
192 $uri = rtrim($uri, '/').'/';
193
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
194 foreach (static::$bundles as $key => $value)
195 {
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
196 if (starts_with($uri, $value['handles'])) return $key;
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
197 }
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
198
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
199 return DEFAULT_BUNDLE;
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
200 }
201
202 /**
203 * Deteremine if a bundle exists within the bundles directory.
204 *
205 * @param string $bundle
206 * @return bool
207 */
208 public static function exists($bundle)
209 {
643b440 @taylorotwell bundle::exists should return true for default bundle.
taylorotwell authored
210 return $bundle == DEFAULT_BUNDLE or in_array(strtolower($bundle), static::names());
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
211 }
212
213 /**
214 * Determine if a given bundle has been started for the request.
215 *
216 * @param string $bundle
217 * @return void
218 */
219 public static function started($bundle)
220 {
221 return in_array(strtolower($bundle), static::$started);
222 }
223
224 /**
78dc452 @taylorotwell added routed array.
taylorotwell authored
225 * Determine if a given bundle has its routes file loaded.
226 *
227 * @param string $bundle
228 * @return void
229 */
230 public static function routed($bundle)
231 {
232 return in_array(strtolower($bundle), static::$routed);
233 }
234
235 /**
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
236 * Get the identifier prefix for the bundle.
237 *
238 * @param string $bundle
239 * @return string
240 */
241 public static function prefix($bundle)
242 {
243 return ($bundle !== DEFAULT_BUNDLE) ? "{$bundle}::" : '';
244 }
245
246 /**
247 * Get the class prefix for a given bundle.
248 *
249 * @param string $bundle
250 * @return string
251 */
252 public static function class_prefix($bundle)
253 {
254 return ($bundle !== DEFAULT_BUNDLE) ? Str::classify($bundle).'_' : '';
255 }
256
257 /**
258 * Return the root bundle path for a given bundle.
259 *
260 * <code>
261 * // Returns the bundle path for the "admin" bundle
262 * $path = Bundle::path('admin');
263 *
97fcea1 @taylorotwell cleaned up paths.
taylorotwell authored
264 * // Returns the path('app') constant as the default bundle
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
265 * $path = Bundle::path('application');
266 * </code>
267 *
268 * @param string $bundle
269 * @return string
270 */
271 public static function path($bundle)
272 {
97fcea1 @taylorotwell cleaned up paths.
taylorotwell authored
273 return ($bundle == DEFAULT_BUNDLE) ? path('app') : static::$bundles[$bundle]['location'];
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
274 }
275
276 /**
277 * Return the root asset path for the given bundle.
278 *
279 * @param string $bundle
280 * @return string
281 */
282 public static function assets($bundle)
283 {
9e690c8 @taylorotwell fix problem with bundle assets.
taylorotwell authored
284 return ($bundle != DEFAULT_BUNDLE) ? URL::base()."/bundles/{$bundle}/" : URL::base().'/';
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
285 }
286
287 /**
288 * Get the bundle name from a given identifier.
289 *
290 * <code>
291 * // Returns "admin" as the bundle name for the identifier
292 * $bundle = Bundle::name('admin::home.index');
293 * </code>
294 *
295 * @param string $identifier
296 * @return string
297 */
298 public static function name($identifier)
299 {
300 list($bundle, $element) = static::parse($identifier);
301
302 return $bundle;
303 }
304
305 /**
306 * Get the element name from a given identifier.
307 *
308 * <code>
309 * // Returns "home.index" as the element name for the identifier
310 * $bundle = Bundle::bundle('admin::home.index');
311 * </code>
312 *
313 * @param string $identifier
314 * @return string
315 */
316 public static function element($identifier)
317 {
318 list($bundle, $element) = static::parse($identifier);
319
320 return $element;
321 }
322
323 /**
324 * Reconstruct an identifier from a given bundle and element.
325 *
326 * <code>
327 * // Returns "admin::home.index"
328 * $identifier = Bundle::identifier('admin', 'home.index');
329 *
330 * // Returns "home.index"
331 * $identifier = Bundle::identifier('application', 'home.index');
332 * </code>
333 *
334 * @param string $bundle
335 * @param string $element
336 * @return string
337 */
338 public static function identifier($bundle, $element)
339 {
340 return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
341 }
342
343 /**
344 * Return the bundle name if it exists, else return the default bundle.
345 *
346 * @param string $bundle
347 * @return string
348 */
349 public static function resolve($bundle)
350 {
351 return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
352 }
353
354 /**
355 * Parse a element identifier and return the bundle name and element.
356 *
357 * <code>
358 * // Returns array(null, 'admin.user')
359 * $element = Bundle::parse('admin.user');
360 *
361 * // Parses "admin::user" and returns array('admin', 'user')
362 * $element = Bundle::parse('admin::user');
363 * </code>
364 *
365 * @param string $identifier
366 * @return array
367 */
368 public static function parse($identifier)
369 {
370 // The parsed elements are cached so we don't have to reparse them on each
371 // subsequent request for the parsed element. So, if we've already parsed
372 // the given element, we'll just return the cached copy.
373 if (isset(static::$elements[$identifier]))
374 {
375 return static::$elements[$identifier];
376 }
377
378 if (strpos($identifier, '::') !== false)
379 {
380 $element = explode('::', strtolower($identifier));
381 }
382 // If no bundle is in the identifier, we will insert the default bundle
383 // since classes like Config and Lang organize their items by bundle.
384 // The "application" folder essentially behaves as a bundle.
385 else
386 {
387 $element = array(DEFAULT_BUNDLE, strtolower($identifier));
388 }
389
390 return static::$elements[$identifier] = $element;
391 }
392
393 /**
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
394 * Get the information for a given bundle.
395 *
396 * @param string $bundle
397 * @return object
398 */
399 public static function get($bundle)
400 {
401 return (object) array_get(static::$bundles, $bundle);
402 }
403
404 /**
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
405 * Get all of the installed bundles for the application.
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
406 *
407 * @return array
408 */
409 public static function all()
410 {
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
411 return static::$bundles;
412 }
413
414 /**
415 * Get all of the installed bundle names.
416 *
417 * @return array
418 */
419 public static function names()
420 {
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
421 return array_keys(static::$bundles);
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
422 }
423
424 }
Something went wrong with that request. Please try again.