Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 337 lines (300 sloc) 8.03 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
3 class Bundle {
4
5 /**
6 * All of the application's bundles.
7 *
8 * @var array
9 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
10 public static $bundles = array();
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
11
12 /**
13 * A cache of the parsed bundle elements.
14 *
15 * @var array
16 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
17 public static $elements = array();
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
18
19 /**
20 * All of the bundles that have been started.
21 *
22 * @var array
23 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
24 public static $started = array();
25
26 /**
27 * Register a bundle for the application.
28 *
29 * @param string $bundle
62072e5 @sparksp PHPDoc fixes
sparksp authored
30 * @param mixed $config Array of 'location', 'handles' and 'auto'; or string of location.
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
31 * @return void
32 */
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
33 public static function register($bundle, $config = array())
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
34 {
733392f @sparksp Really default location to bundle name.
sparksp authored
35 $defaults = array('handles' => null, 'auto' => false);
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
36
7008250 @taylorotwell improve bundle configuration and registration.
taylorotwell authored
37 // If the given config is actually a string, we will assume it is a location
38 // and convert it to an array so that the developer may conveniently add
39 // bundles to the configuration without making an array for each one.
40 if (is_string($config))
41 {
42 $config = array('location' => $config);
43 }
44
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
45 if ( ! isset($config['location']))
46 {
733392f @sparksp Really default location to bundle name.
sparksp authored
47 $config['location'] = $bundle;
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
48 }
49
7008250 @taylorotwell improve bundle configuration and registration.
taylorotwell authored
50 // We will trim the trailing slash from the location and add it back so
51 // we don't have to worry about the developer adding or not adding it
52 // to the location path for the bundle.
97fcea1 @taylorotwell cleaned up paths.
taylorotwell authored
53 $config['location'] = path('bundle').rtrim($config['location'], DS).DS;
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
54
55 static::$bundles[$bundle] = array_merge($defaults, $config);
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
56 }
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
57
58 /**
59 * Load a bundle by running it's start-up script.
60 *
61 * If the bundle has already been started, no action will be taken.
62 *
63 * @param string $bundle
64 * @return void
65 */
66 public static function start($bundle)
67 {
68 if (static::started($bundle)) return;
69
70 if ($bundle !== DEFAULT_BUNDLE and ! static::exists($bundle))
71 {
72 throw new \Exception("Bundle [$bundle] has not been installed.");
73 }
74
75 // Each bundle may have a "start" script which is responsible for preparing
76 // the bundle for use by the application. The start script may register any
77 // classes the bundle uses with the auto-loader, or perhaps will start any
78 // dependent bundles so that they are available.
79 if (file_exists($path = static::path($bundle).'bundle'.EXT))
80 {
2750291 @taylorotwell fix bundle dependency error possibility. fix WSOD in view rendering.
taylorotwell authored
81 require_once $path;
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
82 }
83
84 // Each bundle may also have a "routes" file which is responsible for
85 // registering the bundle's routes. This is kept separate from the
86 // start script for reverse routing efficiency purposes.
87 static::routes($bundle);
67696d8 @taylorotwell index names may be specified on fluent setup.
taylorotwell authored
88
b5f9f9b @taylorotwell added event firing when bundle is started.
taylorotwell authored
89 Event::fire("started: {$bundle}");
90
67696d8 @taylorotwell index names may be specified on fluent setup.
taylorotwell authored
91 static::$started[] = strtolower($bundle);
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
92 }
93
94 /**
95 * Load the "routes" file for a given bundle.
96 *
97 * @param string $bundle
98 * @return void
99 */
100 public static function routes($bundle)
101 {
102 if (file_exists($path = static::path($bundle).'routes'.EXT))
103 {
2750291 @taylorotwell fix bundle dependency error possibility. fix WSOD in view rendering.
taylorotwell authored
104 require_once $path;
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
105 }
106 }
107
108 /**
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
109 * Determine which bundle handles the given URI.
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
110 *
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
111 * If no bundle is assigned to handle the URI, the default bundle is returned.
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
112 *
62072e5 @sparksp PHPDoc fixes
sparksp authored
113 * @param string $uri
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
114 * @return string
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
115 */
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
116 public static function handles($uri)
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
117 {
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
118 foreach (static::$bundles as $key => $value)
119 {
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
120 if (starts_with($uri, $value['handles'])) return $key;
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
121 }
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
122
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
123 return DEFAULT_BUNDLE;
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
124 }
125
126 /**
127 * Deteremine if a bundle exists within the bundles directory.
128 *
129 * @param string $bundle
130 * @return bool
131 */
132 public static function exists($bundle)
133 {
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
134 return in_array(strtolower($bundle), static::names());
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
135 }
136
137 /**
138 * Determine if a given bundle has been started for the request.
139 *
140 * @param string $bundle
141 * @return void
142 */
143 public static function started($bundle)
144 {
145 return in_array(strtolower($bundle), static::$started);
146 }
147
148 /**
149 * Get the identifier prefix for the bundle.
150 *
151 * @param string $bundle
152 * @return string
153 */
154 public static function prefix($bundle)
155 {
156 return ($bundle !== DEFAULT_BUNDLE) ? "{$bundle}::" : '';
157 }
158
159 /**
160 * Get the class prefix for a given bundle.
161 *
162 * @param string $bundle
163 * @return string
164 */
165 public static function class_prefix($bundle)
166 {
167 return ($bundle !== DEFAULT_BUNDLE) ? Str::classify($bundle).'_' : '';
168 }
169
170 /**
171 * Return the root bundle path for a given bundle.
172 *
173 * <code>
174 * // Returns the bundle path for the "admin" bundle
175 * $path = Bundle::path('admin');
176 *
97fcea1 @taylorotwell cleaned up paths.
taylorotwell authored
177 * // Returns the path('app') constant as the default bundle
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
178 * $path = Bundle::path('application');
179 * </code>
180 *
181 * @param string $bundle
182 * @return string
183 */
184 public static function path($bundle)
185 {
97fcea1 @taylorotwell cleaned up paths.
taylorotwell authored
186 return ($bundle == DEFAULT_BUNDLE) ? path('app') : static::$bundles[$bundle]['location'];
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
187 }
188
189 /**
190 * Return the root asset path for the given bundle.
191 *
192 * @param string $bundle
193 * @return string
194 */
195 public static function assets($bundle)
196 {
9e690c8 @taylorotwell fix problem with bundle assets.
taylorotwell authored
197 return ($bundle != DEFAULT_BUNDLE) ? URL::base()."/bundles/{$bundle}/" : URL::base().'/';
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
198 }
199
200 /**
201 * Get the bundle name from a given identifier.
202 *
203 * <code>
204 * // Returns "admin" as the bundle name for the identifier
205 * $bundle = Bundle::name('admin::home.index');
206 * </code>
207 *
208 * @param string $identifier
209 * @return string
210 */
211 public static function name($identifier)
212 {
213 list($bundle, $element) = static::parse($identifier);
214
215 return $bundle;
216 }
217
218 /**
219 * Get the element name from a given identifier.
220 *
221 * <code>
222 * // Returns "home.index" as the element name for the identifier
223 * $bundle = Bundle::bundle('admin::home.index');
224 * </code>
225 *
226 * @param string $identifier
227 * @return string
228 */
229 public static function element($identifier)
230 {
231 list($bundle, $element) = static::parse($identifier);
232
233 return $element;
234 }
235
236 /**
237 * Reconstruct an identifier from a given bundle and element.
238 *
239 * <code>
240 * // Returns "admin::home.index"
241 * $identifier = Bundle::identifier('admin', 'home.index');
242 *
243 * // Returns "home.index"
244 * $identifier = Bundle::identifier('application', 'home.index');
245 * </code>
246 *
247 * @param string $bundle
248 * @param string $element
249 * @return string
250 */
251 public static function identifier($bundle, $element)
252 {
253 return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
254 }
255
256 /**
257 * Return the bundle name if it exists, else return the default bundle.
258 *
259 * @param string $bundle
260 * @return string
261 */
262 public static function resolve($bundle)
263 {
264 return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
265 }
266
267 /**
268 * Parse a element identifier and return the bundle name and element.
269 *
270 * <code>
271 * // Returns array(null, 'admin.user')
272 * $element = Bundle::parse('admin.user');
273 *
274 * // Parses "admin::user" and returns array('admin', 'user')
275 * $element = Bundle::parse('admin::user');
276 * </code>
277 *
278 * @param string $identifier
279 * @return array
280 */
281 public static function parse($identifier)
282 {
283 // The parsed elements are cached so we don't have to reparse them on each
284 // subsequent request for the parsed element. So, if we've already parsed
285 // the given element, we'll just return the cached copy.
286 if (isset(static::$elements[$identifier]))
287 {
288 return static::$elements[$identifier];
289 }
290
291 if (strpos($identifier, '::') !== false)
292 {
293 $element = explode('::', strtolower($identifier));
294 }
295 // If no bundle is in the identifier, we will insert the default bundle
296 // since classes like Config and Lang organize their items by bundle.
297 // The "application" folder essentially behaves as a bundle.
298 else
299 {
300 $element = array(DEFAULT_BUNDLE, strtolower($identifier));
301 }
302
303 return static::$elements[$identifier] = $element;
304 }
305
306 /**
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
307 * Get the information for a given bundle.
308 *
309 * @param string $bundle
310 * @return object
311 */
312 public static function get($bundle)
313 {
314 return (object) array_get(static::$bundles, $bundle);
315 }
316
317 /**
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
318 * Get all of the installed bundles for the application.
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
319 *
320 * @return array
321 */
322 public static function all()
323 {
d76cf4b @taylorotwell bundle improvements.
taylorotwell authored
324 return static::$bundles;
325 }
326
327 /**
328 * Get all of the installed bundle names.
329 *
330 * @return array
331 */
332 public static function names()
333 {
8dfb50c @taylorotwell opened up bundles to live anywhere and respond to any URI.'
taylorotwell authored
334 return array_keys(static::$bundles);
b5442c6 @taylorotwell merged skunkworks into develop.
taylorotwell authored
335 }
336
337 }
Something went wrong with that request. Please try again.