Browse Source

Throw exceptions when non Datasource classes are used as Datasources.

Using models as datasources can cause segmentation faults. Guard against
that by checking types and raising exceptions early.

Fixes #3694
mark_story 13 years ago
parent
commit
3d4ebc038c

+ 1 - 1
lib/Cake/Error/exceptions.php

@@ -441,7 +441,7 @@ class MissingDatasourceConfigException extends CakeException {
  */
 class MissingDatasourceException extends CakeException {
 
-	protected $_messageTemplate = 'Datasource class %s could not be found.';
+	protected $_messageTemplate = 'Datasource class %s could not be found. %s';
 
 }
 

+ 10 - 2
lib/Cake/Model/ConnectionManager.php

@@ -99,9 +99,17 @@ class ConnectionManager {
 		$conn = self::$_connectionsEnum[$name];
 		$class = $conn['classname'];
 
-		self::$_dataSources[$name] = new $class(self::$config->{$name});
-		self::$_dataSources[$name]->configKeyName = $name;
+		$instance = new $class(self::$config->{$name});
+		$instance->configKeyName = $name;
 
+		if (!$instance instanceof Datasource) {
+			throw new MissingDatasourceException(array(
+				'class' => $class,
+				'plugin' => null,
+				'message' => 'Only classes extending Datasource can be used as datasources.'
+			));
+		}
+		self::$_dataSources[$name] = $instance;
 		return self::$_dataSources[$name];
 	}
 

+ 3 - 0
lib/Cake/View/Errors/missing_datasource.ctp

@@ -22,6 +22,9 @@ $pluginDot = empty($plugin) ? null : $plugin . '.';
 <p class="error">
 	<strong><?php echo __d('cake_dev', 'Error'); ?>: </strong>
 	<?php echo __d('cake_dev', 'Datasource class %s could not be found.', '<em>' . $pluginDot . $class . '</em>'); ?>
+	<?php if (isset($message)):  ?>
+		<?php echo h($message); ?>
+	<?php endif; ?>
 </p>
 <p class="notice">
 	<strong><?php echo __d('cake_dev', 'Notice'); ?>: </strong>