CakeSchema naming fallback.

Abstracts the `require_once` of the schema file so it can be done twice. The added second call is a fallback for the previous APP_DIR-based naming to provide backwards compatibility.

Removes now-obsolete CakeSchema tests that involved `Configure::read('App.dir')`. The CakeSchema::name is now always static (and predictable) in the default case.
This commit is contained in:
Brian Porter 2014-08-11 10:40:57 -05:00
parent 543e520ccb
commit 9b9600fe18
2 changed files with 23 additions and 24 deletions

View file

@ -163,11 +163,10 @@ class CakeSchema extends Object {
$class = $name . 'Schema';
if (!class_exists($class)) {
if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
require_once $path . DS . $file;
} elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
require_once $path . DS . 'schema.php';
if (!class_exists($class) && !$this->_requireFile($path, $file)) {
$class = Inflector::camelize(Inflector::slug(Configure::read('App.dir'))) . 'Schema';
if (!class_exists($class)) {
$this->_requireFile($path, $file);
}
}
@ -716,4 +715,22 @@ class CakeSchema extends Object {
return preg_replace('/^' . preg_quote($prefix) . '/', '', $table);
}
/**
* Attempts to require the schema file specified
*
* @param string $path filesystem path to the file
* @param string $file filesystem basename of the file
* @return bool true when a file was successfully included, false on failure
*/
protected function _requireFile($path, $file) {
if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
require_once $path . DS . $file;
return true;
} elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
require_once $path . DS . 'schema.php';
return true;
}
return false;
}
}

View file

@ -436,25 +436,7 @@ class CakeSchemaTest extends CakeTestCase {
*/
public function testSchemaName() {
$Schema = new CakeSchema();
$this->assertEquals(Inflector::camelize(Inflector::slug(APP_DIR)), $Schema->name);
Configure::write('App.dir', 'Some.name.with.dots');
$Schema = new CakeSchema();
$this->assertEquals('SomeNameWithDots', $Schema->name);
Configure::write('App.dir', 'Some-name-with-dashes');
$Schema = new CakeSchema();
$this->assertEquals('SomeNameWithDashes', $Schema->name);
Configure::write('App.dir', 'Some name with spaces');
$Schema = new CakeSchema();
$this->assertEquals('SomeNameWithSpaces', $Schema->name);
Configure::write('App.dir', 'Some,name;with&weird=characters');
$Schema = new CakeSchema();
$this->assertEquals('SomeNameWithWeirdCharacters', $Schema->name);
Configure::write('App.dir', 'app');
$this->assertEquals('App', $Schema->name);
}
/**