From 7828f7d2fb0e37af468af304c329c7a4ca4df949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Lorenzo=20Rodri=CC=81guez?= Date: Tue, 7 Dec 2010 19:59:07 -0430 Subject: [PATCH] Lazy loading connections in ConnectionManager, changing some class names and imports --- lib/Cake/Model/ConnectionManager.php | 16 ++++++++-------- lib/Cake/Model/Datasource/CakeSession.php | 5 +---- lib/Cake/Model/Datasource/Database/Sqlite.php | 4 +++- lib/Cake/Utility/File.php | 4 +--- lib/Cake/View/pages/home.ctp | 11 ++++------- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/Cake/Model/ConnectionManager.php b/lib/Cake/Model/ConnectionManager.php index de3a4e90c..208d6ba9d 100644 --- a/lib/Cake/Model/ConnectionManager.php +++ b/lib/Cake/Model/ConnectionManager.php @@ -69,7 +69,6 @@ class ConnectionManager { include_once CONFIGS . 'database.php'; if (class_exists('DATABASE_CONFIG')) { self::$config = new DATABASE_CONFIG(); - self::_getConnectionObjects(); } register_shutdown_function('ConnectionManager::shutdown'); self::$_init = true; @@ -91,11 +90,16 @@ class ConnectionManager { return $return; } + if (empty(self::$_connectionsEnum[$name])) { + self::_getConnectionObject($name); + } + if (empty(self::$_connectionsEnum[$name])) { trigger_error(__("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR); $null = null; return $null; } + $conn = self::$_connectionsEnum[$name]; $class = $conn['classname']; @@ -222,13 +226,9 @@ class ConnectionManager { * * @return void */ - protected static function _getConnectionObjects() { - $connections = get_object_vars(self::$config); - - if ($connections != null) { - foreach ($connections as $name => $config) { - self::$_connectionsEnum[$name] = self::_connectionData($config); - } + protected static function _getConnectionObject($name) { + if (!empty(self::$config->{$name})) { + self::$_connectionsEnum[$name] = self::_connectionData(self::$config->{$name}); } else { throw new MissingConnectionException(array('class' => 'ConnectionManager')); } diff --git a/lib/Cake/Model/Datasource/CakeSession.php b/lib/Cake/Model/Datasource/CakeSession.php index fd67272ad..589153383 100644 --- a/lib/Cake/Model/Datasource/CakeSession.php +++ b/lib/Cake/Model/Datasource/CakeSession.php @@ -559,10 +559,7 @@ class CakeSession { */ protected static function _getHandler($handler) { list($plugin, $class) = pluginSplit($handler, true); - $found = App::import('Lib', $plugin . 'session/' . $class); - if (!$found) { - App::import('Core', 'session/' . $class); - } + App::uses($class, $plugin . 'Model/Datasource/Session'); if (!class_exists($class)) { throw new Exception(__('Could not load %s to handle the session.', $class)); } diff --git a/lib/Cake/Model/Datasource/Database/Sqlite.php b/lib/Cake/Model/Datasource/Database/Sqlite.php index e4a50b4c2..817e00cf1 100644 --- a/lib/Cake/Model/Datasource/Database/Sqlite.php +++ b/lib/Cake/Model/Datasource/Database/Sqlite.php @@ -18,6 +18,8 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::uses('DboSource', 'Model/Datasource'); + /** * DBO implementation for the SQLite3 DBMS. * @@ -26,7 +28,7 @@ * @package datasources * @subpackage cake.cake.libs.model.datasources.dbo */ -class DboSqlite extends DboSource { +class Sqlite extends DboSource { /** * Datasource Description diff --git a/lib/Cake/Utility/File.php b/lib/Cake/Utility/File.php index 663004840..2d9e268d7 100644 --- a/lib/Cake/Utility/File.php +++ b/lib/Cake/Utility/File.php @@ -22,9 +22,7 @@ * Included libraries. * */ -if (!class_exists('Folder')) { - require LIBS . 'folder.php'; -} +App::uses('File', 'Utility'); /** * Convenience class for reading, writing and appending to files. diff --git a/lib/Cake/View/pages/home.ctp b/lib/Cake/View/pages/home.ctp index 95ead2a18..81a6d5818 100644 --- a/lib/Cake/View/pages/home.ctp +++ b/lib/Cake/View/pages/home.ctp @@ -72,7 +72,7 @@ endif; ?>

'; __('PCRE has not been compiled with Unicode support.'); @@ -83,15 +83,12 @@ endif; ?> getDataSource('default'); + App::uses('ConnectionManager', 'Model'); + $connected = ConnectionManager::getDataSource('default'); ?>

isConnected()): + if ($connected && $connected->isConnected()): echo ''; echo __('Cake is able to connect to the database.'); echo '';