From 9b9600fe18c1acd2caf4d853d88f2e4ef7bc25f4 Mon Sep 17 00:00:00 2001 From: Brian Porter Date: Mon, 11 Aug 2014 10:40:57 -0500 Subject: [PATCH] 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. --- lib/Cake/Model/CakeSchema.php | 27 +++++++++++++++++---- lib/Cake/Test/Case/Model/CakeSchemaTest.php | 20 +-------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/lib/Cake/Model/CakeSchema.php b/lib/Cake/Model/CakeSchema.php index c392f5a36..39e1fae3f 100644 --- a/lib/Cake/Model/CakeSchema.php +++ b/lib/Cake/Model/CakeSchema.php @@ -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; + } + } diff --git a/lib/Cake/Test/Case/Model/CakeSchemaTest.php b/lib/Cake/Test/Case/Model/CakeSchemaTest.php index 0ede91eb5..67c4d8bd5 100644 --- a/lib/Cake/Test/Case/Model/CakeSchemaTest.php +++ b/lib/Cake/Test/Case/Model/CakeSchemaTest.php @@ -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); } /**