From f861cc6e090aaabf92d602d4053aee6bcc4da0f5 Mon Sep 17 00:00:00 2001
From: Yosuke Basuke Suzuki <basuke@mac.com>
Date: Thu, 20 Oct 2011 17:19:08 +0900
Subject: [PATCH] Proposal. Adding new package using App::build().

For instance, adding new 'Service' package to app search paths.
---
 lib/Cake/Core/App.php               | 25 +++++++++++++++++++++++++
 lib/Cake/Test/Case/Core/AppTest.php | 25 +++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php
index c76008ab4..c2234deba 100644
--- a/lib/Cake/Core/App.php
+++ b/lib/Cake/Core/App.php
@@ -76,6 +76,13 @@ class App {
  */
 	const PREPEND = 'prepend';
 
+/**
+ * Register package
+ *
+ * @constant REGISTER
+ */
+	const REGISTER = 'register';
+
 /**
  * Reset paths instead of merging
  *
@@ -281,6 +288,24 @@ class App {
 
 		$packageFormat = self::_packageFormat();
 
+		if ($mode === App::REGISTER) {
+			if (empty($paths)) {
+				self::$_packageFormat = null;
+				$packageFormat = self::_packageFormat();
+			} else {
+				foreach ($paths as $package => $formats) {
+					if (!empty($packageFormat[$package])) {
+						$formats = array_merge($packageFormat[$package], $formats);
+					}
+
+					$packageFormat[$package] = array_values(array_unique($formats));
+				}
+
+				self::$_packageFormat = $packageFormat;
+				$paths = array();
+			}
+		}
+
 		$defaults = array();
 		foreach ($packageFormat as $package => $format) {
 			foreach ($format as $f) {
diff --git a/lib/Cake/Test/Case/Core/AppTest.php b/lib/Cake/Test/Case/Core/AppTest.php
index 3520fd461..6ef7bcd20 100644
--- a/lib/Cake/Test/Case/Core/AppTest.php
+++ b/lib/Cake/Test/Case/Core/AppTest.php
@@ -200,6 +200,31 @@ class AppTest extends CakeTestCase {
 		$this->assertEqual($old, $defaults);
 	}
 
+/**
+ * test package build() with App::REGISTER.
+ *
+ * @return void
+ */
+	public function testBuildPackage() {
+		$paths = App::path('Service');
+		$this->assertEqual(array(), $paths);
+
+		App::build(array(
+			'Service' => array(
+				'%s' . 'Service' . DS,
+			),
+		), App::REGISTER);
+
+		$expected = array(
+			APP . 'Service' . DS,
+		);
+
+		$result = App::path('Service');
+		$this->assertEquals($expected, $result);
+
+		App::build(array(), App::REGISTER);
+	}
+
 /**
  * test path() with a plugin.
  *