Skip to content

Commit

Permalink
Add another naming strategy for BC purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
korstiaan committed Jul 29, 2012
1 parent 127abf0 commit f53eff7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -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.
26 changes: 14 additions & 12 deletions module/Nsautoload/Nsautoload.php
Expand Up @@ -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;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Nsautoload/Tests/AutoloadTest.php
Expand Up @@ -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'),
);
Expand Down
7 changes: 7 additions & 0 deletions tests/nsautoloadtest/class/crux.class.inc
@@ -0,0 +1,7 @@
<?php

namespace NsautoloadTest;

class Crux
{
}
7 changes: 7 additions & 0 deletions tests/nsautoloadtest/class/crux2.class.inc
@@ -0,0 +1,7 @@
<?php

namespace NsautoloadTest;

class Crux2
{
}

0 comments on commit f53eff7

Please sign in to comment.