Browse Source

improving bundle workflow.

Taylor Otwell 13 years ago
parent
commit
a11318863a
5 changed files with 102 additions and 36 deletions
  1. 30 10
      application/config/application.php
  2. 58 25
      laravel/bundle.php
  3. 1 1
      laravel/core.php
  4. 12 0
      laravel/helpers.php
  5. 1 0
      storage/cache/.gitignore

+ 30 - 10
application/config/application.php

@@ -21,7 +21,6 @@ return array(
 	|--------------------------------------------------------------------------
 	|
 	| If you are including the "index.php" in your URLs, you can ignore this.
-	|
 	| However, if you are using mod_rewrite to get cleaner URLs, just set
 	| this option to an empty string and we'll take care of the rest.
 	|
@@ -73,13 +72,13 @@ return array(
 	| SSL Link Generation
 	|--------------------------------------------------------------------------
 	|
-	| Many sites use SSL to protect their users data. However, you may not
-	| always be able to use SSL on your development machine, meaning all HTTPS
-	| will be broken during development.
+	| Many sites use SSL to protect their users data. However, you may not be
+	| able to use SSL on your development machine, meaning all HTTPS will be
+	| broken during development.
 	|
 	| For this reason, you may wish to disable the generation of HTTPS links
-	| throughout your application. This option does just that. All attempts to
-	| generate HTTPS links will generate regular HTTP links instead.
+	| throughout your application. This option does just that. All attempts
+	| to generate HTTPS links will generate regular HTTP links instead.
 	|
 	*/
 
@@ -90,7 +89,7 @@ return array(
 	| Application Timezone
 	|--------------------------------------------------------------------------
 	|
-	| The default timezone of your application. This timezone will be used when
+	| The default timezone of your application. The timezone will be used when
 	| Laravel needs a date, such as when writing to a log file or travelling
 	| to a distant star at warp speed.
 	|
@@ -98,20 +97,41 @@ return array(
 
 	'timezone' => 'UTC',
 
+	/*
+	|--------------------------------------------------------------------------
+	| Bundle Options
+	|--------------------------------------------------------------------------
+	|
+	| Here you may specify options related to application bundles, such as the
+	| amount of time the bundle manifest is cached. Each option is detailed
+	| below with suggestions for sensible values.
+	|
+	| Cache:
+	|
+	| All bundles have a "bundle.info" file which contains information such
+	| as the name of a bundle and the URIs it responds to. This value is
+	| the number of that bundle info is cached.
+	|
+	*/
+
+	'bundle' => array(
+
+		'cache' => 0,
+
+	),
+
 	/*
 	|--------------------------------------------------------------------------
 	| Class Aliases
 	|--------------------------------------------------------------------------
 	|
 	| Here, you can specify any class aliases that you would like registered
-	| when Laravel loads. Aliases are lazy-loaded, so add as many as you want.
+	| when Laravel loads. Aliases are lazy-loaded, so feel free to add!
 	|
 	| Aliases make it more convenient to use namespaced classes. Instead of
 	| referring to the class using its full namespace, you may simply use
 	| the alias defined here.
 	|
-	| We have already aliased common Laravel classes to make life easier.
-	|
 	*/
 
 	'aliases' => array(

+ 58 - 25
laravel/bundle.php

@@ -1,5 +1,7 @@
 <?php namespace Laravel; defined('DS') or die('No direct script access.');
 
+use FilesystemIterator as fIterator;
+
 class Bundle {
 
 	/**
@@ -31,43 +33,74 @@ class Bundle {
 	public static $routed = array();
 
 	/**
-	 * Register a bundle for the application.
+	 * Detect all of the installed bundles from disk.
 	 *
-	 * @param  string  $bundle
-	 * @param  mixed   $config
-	 * @return void
+	 * @param  string  $path
+	 * @return array
 	 */
-	public static function register($bundle, $config = array())
+	public static function detect($path)
 	{
-		$defaults = array('handles' => null, 'auto' => false);
+		$bundles = array();
 
-		// 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);
-		}
+		$items = new fIterator($path);
 
-		if ( ! isset($config['location']))
+		foreach ($items as $item)
 		{
-			$config['location'] = $bundle;
+			// If the item is a directory, we'll search for a bundle.info file.
+			// If one exists, we will add it to the bundle array. We will set
+			// the location automatically since we know it.
+			if ($item->isDir())
+			{
+				$path = $item->getRealPath().DS.'bundle.info';
+
+				// If we found a file, we'll require in the array it contains
+				// and add it to the directory. The info array will contain
+				// basic info like the bundle name and any URIs it may
+				// handle incoming requests for.
+				if (file_exists($path))
+				{
+					$info = require $path;
+
+					$info['location'] = dirname($path).DS;
+
+					$bundles[$info['name']] = $info;
+
+					continue;
+				}
+				// If a bundle.info file doesn't exist within a directory,
+				// we'll recurse into the directory to keep searching in
+				// the bundle directory for nested bundles.
+				else
+				{
+					$recurse = static::detect($item->getRealPath());
+
+					$bundles = array_merge($bundles, $recurse);
+				}
+			}
 		}
 
-		// 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.
-		$config['location'] = path('bundle').rtrim($config['location'], DS).DS;
+		return $bundles;
+	}
+
+	/**
+	 * Register a bundle for the application.
+	 *
+	 * @param  array  $config
+	 * @return void
+	 */
+	public static function register($config)
+	{
+		$defaults = array('handles' => null, 'auto' => false);
 
-		// If the handles clause is set, we will append a trailing slash so
-		// that it is not ultra-greedy. Otherwise, bundles that handle "s"
-		// would handle all bundles that start with "s".
+		// If a handles clause has been specified, we will cap it with a trailing
+		// slash so the bundle is not extra greedy with its routes. Otherwise a
+		// bundle that handles "s" would handle all routes beginning with "s".
 		if (isset($config['handles']))
 		{
-			$config['handles'] = $config['handles'].'/';
+			$config['handles'] = str_finish($config['handles'], '/');
 		}
 
-		static::$bundles[$bundle] = array_merge($defaults, $config);
+		static::$bundles[$config['name']] = array_merge($defaults, $config);
 	}
 
 	/**
@@ -82,7 +115,7 @@ class Bundle {
 	{
 		if (static::started($bundle)) return;
 
-		if ($bundle !== DEFAULT_BUNDLE and ! static::exists($bundle))
+		if ( ! static::exists($bundle))
 		{
 			throw new \Exception("Bundle [$bundle] has not been installed.");
 		}

+ 1 - 1
laravel/core.php

@@ -53,7 +53,7 @@ Autoloader::namespaces(array('Laravel' => path('sys')));
  */
 $bundles = Cache::remember('laravel.bundle.manifest', function()
 {
-	return Bundle::detect();
+	return Bundle::detect(path('bundle'));
 
 }, Config::get('application.bundle.cache'));
 

+ 12 - 0
laravel/helpers.php

@@ -351,6 +351,18 @@ function str_contains($haystack, $needle)
 	return strpos($haystack, $needle) !== false;
 }
 
+/**
+ * Cap a string with a single instance of the given string.
+ *
+ * @param  string  $value
+ * @param  string  $cap
+ * @return string
+ */
+function str_finish($value, $cap)
+{
+	return rtrim($value, $cap).$cap;
+}
+
 /**
  * Return the value of the given item.
  *

+ 1 - 0
storage/cache/.gitignore

@@ -0,0 +1 @@
+laravel.bundle.manifest