From f53eff7c8aa89d083348ca96e3571c08b0875057 Mon Sep 17 00:00:00 2001 From: Korstiaan de Ridder Date: Sun, 29 Jul 2012 20:05:53 +0200 Subject: [PATCH] Add another naming strategy for BC purposes --- README.md | 5 +++++ module/Nsautoload/Nsautoload.php | 26 ++++++++++++---------- tests/Nsautoload/Tests/AutoloadTest.php | 2 ++ tests/nsautoloadtest/class/crux.class.inc | 7 ++++++ tests/nsautoloadtest/class/crux2.class.inc | 7 ++++++ 5 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 tests/nsautoloadtest/class/crux.class.inc create mode 100644 tests/nsautoloadtest/class/crux2.class.inc diff --git a/README.md b/README.md index 1593131..7f1bac8 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,11 @@ When this convention is followed, a class is located at `module_name/class/_clas * `MyModule\Foo_Bar` is located at `/path/to/my_module/class/foo_bar.class.inc` * `MyModule\Foo\Bar` can't be mapped with this convention. +This convention also adds another namespace naming strategy for BC purposes. Next to a `under_score` to `CamelCase` conversion, it also allows you to randomly add capitals in your namespace. +This allows the following mapping: + +* `MyOldModule\Foo` to `/path/to/myoldmodule/class/foo.class.inc` + ## License Nsautoload is licensed under the MIT license. diff --git a/module/Nsautoload/Nsautoload.php b/module/Nsautoload/Nsautoload.php index d8eac25..b066bac 100644 --- a/module/Nsautoload/Nsautoload.php +++ b/module/Nsautoload/Nsautoload.php @@ -60,18 +60,20 @@ public function findFile($class) if (2 === count($expl)) { // Locate Foo\Bar in foo/class/bar.class.inc (for BC purposes) - $className = strtolower(end($expl)); - $file = - DRUPAL_ROOT. - DIRECTORY_SEPARATOR. - drupal_get_path('module', $module). - DIRECTORY_SEPARATOR. - 'class'. - DIRECTORY_SEPARATOR. - "{$className}.class.inc"; - - if (file_exists($file)) { - return $file; + foreach (array($module, strtolower(reset($expl))) as $mod) { + $className = strtolower(end($expl)); + $file = + DRUPAL_ROOT. + DIRECTORY_SEPARATOR. + drupal_get_path('module', $mod). + DIRECTORY_SEPARATOR. + 'class'. + DIRECTORY_SEPARATOR. + "{$className}.class.inc"; + + if (file_exists($file)) { + return $file; + } } } diff --git a/tests/Nsautoload/Tests/AutoloadTest.php b/tests/Nsautoload/Tests/AutoloadTest.php index 2976fed..6797594 100644 --- a/tests/Nsautoload/Tests/AutoloadTest.php +++ b/tests/Nsautoload/Tests/AutoloadTest.php @@ -50,6 +50,8 @@ public function getLoaderTests() array('\\Nsautoloadtest\\Foo2', $root.'/nsautoloadtest/Nsautoloadtest/Foo2.php'), array('Nsautoloadtest\\Bar', $root.'/nsautoloadtest/class/bar.class.inc'), array('\\Nsautoloadtest\\Bar2', $root.'/nsautoloadtest/class/bar2.class.inc'), + array('NsautoloadTest\\Crux', $root.'/nsautoloadtest/class/crux.class.inc'), + array('\\NsautoloadTest\\Crux2', $root.'/nsautoloadtest/class/crux2.class.inc'), array('Nsautoloadtest\\Foo\Bar', $root.'/nsautoloadtest/Nsautoloadtest/Foo/Bar.php'), array('\\Nsautoloadtest\\Foo_Bar2', $root.'/nsautoloadtest/Nsautoloadtest/Foo/Bar2.php'), ); diff --git a/tests/nsautoloadtest/class/crux.class.inc b/tests/nsautoloadtest/class/crux.class.inc new file mode 100644 index 0000000..cf49d68 --- /dev/null +++ b/tests/nsautoloadtest/class/crux.class.inc @@ -0,0 +1,7 @@ +