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
This commit is contained in:
mark_story 2013-03-12 21:56:08 -04:00
parent 73310f9bfd
commit 3d4ebc038c
3 changed files with 14 additions and 3 deletions

View file

@ -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';
}

View file

@ -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];
}

View file

@ -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>