bundle.php 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');

class Bundle {

	/**
	 * All of the application's bundles.
	 *
	 * @var array
	 */
10
	public static $bundles = array();
11 12 13 14 15 16

	/**
	 * A cache of the parsed bundle elements.
	 *
	 * @var array
	 */
17
	public static $elements = array();
18 19 20 21 22 23

	/**
	 * All of the bundles that have been started.
	 *
	 * @var array
	 */
24 25 26 27 28 29 30 31 32 33
	public static $started = array();

	/**
	 * Register a bundle for the application.
	 *
	 * @param  string  $bundle
	 * @param  string  $location
	 * @param  string  $handles
	 * @return void
	 */
Taylor Otwell committed
34
	public static function register($bundle, $config = array())
35
	{
Taylor Otwell committed
36
		$defaults = array('handles' => null, 'auto' => false);
37

38 39 40 41 42 43 44 45
		// If the given config is actually a string, we will assume it is a location
		// and convert it to an array so that the developer may conveniently add
		// bundles to the configuration without making an array for each one.
		if (is_string($config))
		{
			$config = array('location' => $config);
		}

Taylor Otwell committed
46 47 48 49 50
		if ( ! isset($config['location']))
		{
			throw new \Exception("Location not set for bundle [$bundle]");
		}

51 52 53
		// We will trim the trailing slash from the location and add it back so
		// we don't have to worry about the developer adding or not adding it
		// to the location path for the bundle.
Taylor Otwell committed
54 55 56
		$config['location'] = BUNDLE_PATH.rtrim($config['location'], DS).DS;

		static::$bundles[$bundle] = array_merge($defaults, $config);
57
	}
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

	/**
	 * Load a bundle by running it's start-up script.
	 *
	 * If the bundle has already been started, no action will be taken.
	 *
	 * @param  string  $bundle
	 * @return void
	 */
	public static function start($bundle)
	{
		if (static::started($bundle)) return;

		if ($bundle !== DEFAULT_BUNDLE and ! static::exists($bundle))
		{
			throw new \Exception("Bundle [$bundle] has not been installed.");
		}

		// Each bundle may have a "start" script which is responsible for preparing
		// the bundle for use by the application. The start script may register any
		// classes the bundle uses with the auto-loader, or perhaps will start any
		// dependent bundles so that they are available.
		if (file_exists($path = static::path($bundle).'bundle'.EXT))
		{
82
			require_once $path;
83 84 85 86 87 88
		}

		// Each bundle may also have a "routes" file which is responsible for
		// registering the bundle's routes. This is kept separate from the
		// start script for reverse routing efficiency purposes.
		static::routes($bundle);
89 90

		static::$started[] = strtolower($bundle);
91 92 93 94 95 96 97 98 99 100 101 102
	}

	/**
	 * Load the "routes" file for a given bundle.
	 *
	 * @param  string  $bundle
	 * @return void
	 */
	public static function routes($bundle)
	{
		if (file_exists($path = static::path($bundle).'routes'.EXT))
		{
103
			require_once $path;
104 105 106 107
		}
	}

	/**
108
	 * Determine which bundle handles the given URI.
109
	 *
110
	 * If no bundle is assigned to handle the URI, the default bundle is returned.
111 112
	 *
	 * @param  string  $bundle
113
	 * @return string
114
	 */
115
	public static function handles($uri)
116
	{
117 118
		foreach (static::$bundles as $key => $value)
		{
Taylor Otwell committed
119
			if (starts_with($uri, $value['handles'])) return $key;
120
		}
121

122
		return DEFAULT_BUNDLE;
123 124 125 126 127 128 129 130 131 132
	}

	/**
	 * Deteremine if a bundle exists within the bundles directory.
	 *
	 * @param  string  $bundle
	 * @return bool
	 */
	public static function exists($bundle)
	{
Taylor Otwell committed
133
		return in_array(strtolower($bundle), static::names());
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	}

	/**
	 * Determine if a given bundle has been started for the request.
	 *
	 * @param  string  $bundle
	 * @return void
	 */
	public static function started($bundle)
	{
		return in_array(strtolower($bundle), static::$started);
	}

	/**
	 * Get the identifier prefix for the bundle.
	 *
	 * @param  string  $bundle
	 * @return string
	 */
	public static function prefix($bundle)
	{
		return ($bundle !== DEFAULT_BUNDLE) ? "{$bundle}::" : '';
	}

	/**
	 * Get the class prefix for a given bundle.
	 *
	 * @param  string  $bundle
	 * @return string
	 */
	public static function class_prefix($bundle)
	{
		return ($bundle !== DEFAULT_BUNDLE) ? Str::classify($bundle).'_' : '';
	}

	/**
	 * Return the root bundle path for a given bundle.
	 *
	 * <code>
	 *		// Returns the bundle path for the "admin" bundle
	 *		$path = Bundle::path('admin');
	 *
	 *		// Returns the APP_PATH constant as the default bundle
	 *		$path = Bundle::path('application');
	 * </code>
	 *
	 * @param  string  $bundle
	 * @return string
	 */
	public static function path($bundle)
	{
185
		return ($bundle == DEFAULT_BUNDLE) ? APP_PATH : static::$bundles[$bundle]['location'];
186 187 188 189 190 191 192 193 194 195
	}

	/**
	 * Return the root asset path for the given bundle.
	 *
	 * @param  string  $bundle
	 * @return string
	 */
	public static function assets($bundle)
	{
196
		return ($bundle != DEFAULT_BUNDLE) ? URL::base()."/bundles/{$bundle}/" : URL::base().'/';
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	}

	/**
	 * Get the bundle name from a given identifier.
	 *
	 * <code>
	 *		// Returns "admin" as the bundle name for the identifier
	 *		$bundle = Bundle::name('admin::home.index');
	 * </code>
	 *
	 * @param  string  $identifier
	 * @return string
	 */
	public static function name($identifier)
	{
		list($bundle, $element) = static::parse($identifier);

		return $bundle;
	}

	/**
	 * Get the element name from a given identifier.
	 *
	 * <code>
	 *		// Returns "home.index" as the element name for the identifier
	 *		$bundle = Bundle::bundle('admin::home.index');
	 * </code>
	 *
	 * @param  string  $identifier
	 * @return string
	 */
	public static function element($identifier)
	{
		list($bundle, $element) = static::parse($identifier);

		return $element;
	}

	/**
	 * Reconstruct an identifier from a given bundle and element.
	 *
	 * <code>
	 *		// Returns "admin::home.index"
	 *		$identifier = Bundle::identifier('admin', 'home.index');
	 *
	 *		// Returns "home.index"
	 *		$identifier = Bundle::identifier('application', 'home.index');
	 * </code>
	 *
	 * @param  string  $bundle
	 * @param  string  $element
	 * @return string
	 */
	public static function identifier($bundle, $element)
	{
		return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
	}

	/**
	 * Return the bundle name if it exists, else return the default bundle.
	 *
	 * @param  string  $bundle
	 * @return string
	 */
	public static function resolve($bundle)
	{
		return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
	}

	/**
	 * Parse a element identifier and return the bundle name and element.
	 *
	 * <code>
	 *		// Returns array(null, 'admin.user')
	 *		$element = Bundle::parse('admin.user');
	 *
	 *		// Parses "admin::user" and returns array('admin', 'user')
	 *		$element = Bundle::parse('admin::user');
	 * </code>
	 *
	 * @param  string  $identifier
	 * @return array
	 */
	public static function parse($identifier)
	{
		// The parsed elements are cached so we don't have to reparse them on each
		// subsequent request for the parsed element. So, if we've already parsed
		// the given element, we'll just return the cached copy.
		if (isset(static::$elements[$identifier]))
		{
			return static::$elements[$identifier];
		}

		if (strpos($identifier, '::') !== false)
		{
			$element = explode('::', strtolower($identifier));
		}
		// If no bundle is in the identifier, we will insert the default bundle
		// since classes like Config and Lang organize their items by bundle.
		// The "application" folder essentially behaves as a bundle.
		else
		{
			$element = array(DEFAULT_BUNDLE, strtolower($identifier));
		}

		return static::$elements[$identifier] = $element;
	}

	/**
306 307 308 309 310 311 312 313 314 315 316
	 * Get the information for a given bundle.
	 *
	 * @param  string  $bundle
	 * @return object
	 */
	public static function get($bundle)
	{
		return (object) array_get(static::$bundles, $bundle);
	}

	/**
Taylor Otwell committed
317
	 * Get all of the installed bundles for the application.
318 319 320 321 322
	 *
	 * @return array
	 */
	public static function all()
	{
Taylor Otwell committed
323 324 325 326 327 328 329 330 331 332
		return static::$bundles;
	}

	/**
	 * Get all of the installed bundle names.
	 *
	 * @return array
	 */
	public static function names()
	{
333
		return array_keys(static::$bundles);
334 335 336
	}

}