Skip to content
Permalink
Browse files

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.
  • Loading branch information...
beporter committed Aug 11, 2014
1 parent 543e520 commit 9b9600fe18c1acd2caf4d853d88f2e4ef7bc25f4
Showing with 23 additions and 24 deletions.
  1. +22 −5 lib/Cake/Model/CakeSchema.php
  2. +1 −19 lib/Cake/Test/Case/Model/CakeSchemaTest.php
@@ -163,11 +163,10 @@ public function load($options = array()) {
$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 @@ protected function _noPrefixTable($prefix, $table) {
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;
}
}
@@ -436,25 +436,7 @@ public function tearDown() {
*/
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);
}
/**

0 comments on commit 9b9600f

Please sign in to comment.
You can’t perform that action at this time.