Browse Source

make it possible to modify the dsn class map

Use a static property, and put a get/set method for it into the static
config trait.
AD7six 11 years ago
parent
commit
8f5398a1d6

+ 16 - 18
src/Cache/Cache.php

@@ -68,6 +68,22 @@ class Cache {
 	use StaticConfigTrait;
 
 /**
+ * An array mapping url schemes to fully qualified caching engine
+ * class names.
+ *
+ * @var array
+ */
+	protected static $_dsnClassMap = [
+		'apc' => 'Cake\Cache\Engine\ApcEngine',
+		'file' => 'Cake\Cache\Engine\FileEngine',
+		'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
+		'null' => 'Cake\Cache\Engine\NullEngine',
+		'redis' => 'Cake\Cache\Engine\RedisEngine',
+		'wincache' => 'Cake\Cache\Engine\WincacheEngine',
+		'xcache' => 'Cake\Cache\Engine\XcacheEngine',
+	];
+
+/**
  * Flag for tracking whether or not caching is enabled.
  *
  * @var bool
@@ -478,22 +494,4 @@ class Cache {
 		return $results;
 	}
 
-/**
- * Returns an array mapping url schemes to fully qualified caching engine
- * class names.
- *
- * @return array
- */
-	public static function getClassMap() {
-		return [
-			'apc' => 'Cake\Cache\Engine\ApcEngine',
-			'file' => 'Cake\Cache\Engine\FileEngine',
-			'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
-			'null' => 'Cake\Cache\Engine\NullEngine',
-			'redis' => 'Cake\Cache\Engine\RedisEngine',
-			'wincache' => 'Cake\Cache\Engine\WincacheEngine',
-			'xcache' => 'Cake\Cache\Engine\XcacheEngine',
-		];
-	}
-
 }

+ 14 - 2
src/Core/StaticConfigTrait.php

@@ -216,8 +216,8 @@ trait StaticConfigTrait {
 		unset($parsed['pass'], $parsed['user']);
 		$parsed = $queryArgs + $parsed;
 
-		if (empty($parsed['className']) && method_exists(get_called_class(), 'getClassMap')) {
-			$classMap = static::getClassMap();
+		if (empty($parsed['className'])) {
+			$classMap = static::dsnClassMap();
 
 			$parsed['className'] = $parsed['scheme'];
 			if (isset($classMap[$parsed['scheme']])) {
@@ -228,4 +228,16 @@ trait StaticConfigTrait {
 		return $parsed;
 	}
 
+/**
+ * return or update the dsn class map for this class
+ *
+ * @param mixed $map
+ * @return array
+ */
+	public static function dsnClassMap($map = null) {
+		if ($map) {
+			static::$_dsnClassMap = $map + $_dsnClassMap;
+		}
+		return static::$_dsnClassMap;
+	}
 }

+ 12 - 14
src/Datasource/ConnectionManager.php

@@ -42,6 +42,18 @@ class ConnectionManager {
 	protected static $_aliasMap = [];
 
 /**
+ * An array mapping url schemes to fully qualified driver class names
+ *
+ * @return array
+ */
+	protected static $_dsnClassMap = [
+		'mysql' => 'Cake\Database\Driver\Mysql',
+		'postgres' => 'Cake\Database\Driver\Postgres',
+		'sqlite' => 'Cake\Database\Driver\Sqlite',
+		'sqlserver' => 'Cake\Database\Driver\Sqlserver',
+	];
+
+/**
  * The ConnectionRegistry used by the manager.
  *
  * @var \Cake\Datasource\ConnectionRegistry
@@ -108,20 +120,6 @@ class ConnectionManager {
 	}
 
 /**
- * Returns an array mapping url schemes to fully qualified driver class names
- *
- * @return array
- */
-	public static function getClassMap() {
-		return [
-			'mysql' => 'Cake\Database\Driver\Mysql',
-			'postgres' => 'Cake\Database\Driver\Postgres',
-			'sqlite' => 'Cake\Database\Driver\Sqlite',
-			'sqlserver' => 'Cake\Database\Driver\Sqlserver',
-		];
-	}
-
-/**
  * Set one or more connection aliases.
  *
  * Connection aliases allow you to rename active connections without overwriting

+ 11 - 12
src/Log/Log.php

@@ -108,6 +108,17 @@ class Log {
 	}
 
 /**
+ * An array mapping url schemes to fully qualified Log engine class names
+ *
+ * @var array
+ */
+	protected static $_dsnClassMap = [
+		'console' => 'Cake\Log\Engine\ConsoleLog',
+		'file' => 'Cake\Log\Engine\FileLog',
+		'syslog' => 'Cake\Log\Engine\SyslogLog',
+	];
+
+/**
  * Internal flag for tracking whether or not configuration has been changed.
  *
  * @var bool
@@ -481,16 +492,4 @@ class Log {
 		return static::write(static::$_levelMap['info'], $message, $context);
 	}
 
-/**
- * Returns an array mapping url schemes to fully qualified Log engine class names
- *
- * @return array
- */
-	public static function getClassMap() {
-		return [
-			'console' => 'Cake\Log\Engine\ConsoleLog',
-			'file' => 'Cake\Log\Engine\FileLog',
-			'syslog' => 'Cake\Log\Engine\SyslogLog',
-		];
-	}
 }

+ 11 - 13
src/Network/Email/Email.php

@@ -304,6 +304,17 @@ class Email {
 	protected $_boundary = null;
 
 /**
+ * An array mapping url schemes to fully qualified Transport class names
+ *
+ * @var array
+ */
+	protected static $_dsnClassMap = [
+		'debug' => 'Cake\Network\Email\DebugTransport',
+		'mail' => 'Cake\Network\Email\MailTransport',
+		'smtp' => 'Cake\Network\Email\SmtpTransport',
+	];
+
+/**
  * Configuration profiles for transports.
  *
  * @var array
@@ -1817,17 +1828,4 @@ class Email {
 		return strtoupper($this->charset);
 	}
 
-/**
- * Returns an array mapping url schemes to fully qualified Transport class names
- *
- * @return array
- */
-	public static function getClassMap() {
-		return [
-			'debug' => 'Cake\Network\Email\DebugTransport',
-			'mail' => 'Cake\Network\Email\MailTransport',
-			'smtp' => 'Cake\Network\Email\SmtpTransport',
-		];
-	}
-
 }

+ 29 - 37
tests/TestCase/Core/StaticConfigTraitTest.php

@@ -51,16 +51,14 @@ class TestConnectionManagerStaticConfig {
 /**
  * Database driver class map.
  *
- * @return array
+ * @var array
  */
-	public static function getClassMap() {
-		return [
-			'mysql' => 'Cake\Database\Driver\Mysql',
-			'postgres' => 'Cake\Database\Driver\Postgres',
-			'sqlite' => 'Cake\Database\Driver\Sqlite',
-			'sqlserver' => 'Cake\Database\Driver\Sqlserver',
-		];
-	}
+	protected static $_dsnClassMap = [
+		'mysql' => 'Cake\Database\Driver\Mysql',
+		'postgres' => 'Cake\Database\Driver\Postgres',
+		'sqlite' => 'Cake\Database\Driver\Sqlite',
+		'sqlserver' => 'Cake\Database\Driver\Sqlserver',
+	];
 
 }
 
@@ -74,19 +72,17 @@ class TestCacheStaticConfig {
 /**
  * Cache driver class map.
  *
- * @return array
+ * @var array
  */
-	public static function getClassMap() {
-		return [
-			'apc' => 'Cake\Cache\Engine\ApcEngine',
-			'file' => 'Cake\Cache\Engine\FileEngine',
-			'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
-			'null' => 'Cake\Cache\Engine\NullEngine',
-			'redis' => 'Cake\Cache\Engine\RedisEngine',
-			'wincache' => 'Cake\Cache\Engine\WincacheEngine',
-			'xcache' => 'Cake\Cache\Engine\XcacheEngine',
-		];
-	}
+	protected static $_dsnClassMap = [
+		'apc' => 'Cake\Cache\Engine\ApcEngine',
+		'file' => 'Cake\Cache\Engine\FileEngine',
+		'memcached' => 'Cake\Cache\Engine\MemcachedEngine',
+		'null' => 'Cake\Cache\Engine\NullEngine',
+		'redis' => 'Cake\Cache\Engine\RedisEngine',
+		'wincache' => 'Cake\Cache\Engine\WincacheEngine',
+		'xcache' => 'Cake\Cache\Engine\XcacheEngine',
+	];
 
 }
 
@@ -100,15 +96,13 @@ class TestEmailStaticConfig {
 /**
  * Email driver class map.
  *
- * @return array
+ * @var array
  */
-	public static function getClassMap() {
-		return [
-			'debug' => 'Cake\Network\Email\DebugTransport',
-			'mail' => 'Cake\Network\Email\MailTransport',
-			'smtp' => 'Cake\Network\Email\SmtpTransport',
-		];
-	}
+	protected static $_dsnClassMap = [
+		'debug' => 'Cake\Network\Email\DebugTransport',
+		'mail' => 'Cake\Network\Email\MailTransport',
+		'smtp' => 'Cake\Network\Email\SmtpTransport',
+	];
 
 }
 
@@ -122,15 +116,13 @@ class TestLogStaticConfig {
 /**
  * Log engine class map.
  *
- * @return array
+ * @var array
  */
-	public static function getClassMap() {
-		return [
-			'console' => 'Cake\Log\Engine\ConsoleLog',
-			'file' => 'Cake\Log\Engine\FileLog',
-			'syslog' => 'Cake\Log\Engine\SyslogLog',
-		];
-	}
+	protected static $_dsnClassMap = [
+		'console' => 'Cake\Log\Engine\ConsoleLog',
+		'file' => 'Cake\Log\Engine\FileLog',
+		'syslog' => 'Cake\Log\Engine\SyslogLog',
+	];
 
 }