Skip to content

Commit

Permalink
Added phpdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
korstiaan committed Jul 28, 2012
1 parent fc21208 commit 03f3a37
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions module/Nsautoload/Nsautoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@

class Nsautoload
{
/**
* Registers this instance as an autoloader
*
* @param bool $prepend
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}

/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}

/**
* Tries to load given class by locating and including its file.
*
* @param string $class The name of the class
* @return bool|null True if loaded
*/
public function loadClass($class)
{
$file = $this->findFile($class);
Expand All @@ -24,14 +38,26 @@ public function loadClass($class)
}
}

/**
* Tries to locate the file where the class is defined.
*
* @param string $class The name of the class
* @return string|null Location of the file, or null if not found
*/
public function findFile($class)
{
$class = ltrim($class,'\\');

$expl = explode('\\',$class);
$module = strtolower(preg_replace('/(?<=\\w)(?=[A-Z])/','_$1', $expl[0]));

$module = strtolower(preg_replace('/(?<=\\w)(?=[A-Z])/','_$1', reset($expl)));

if (!module_exists($module)) {
return;
}

if (2 === count($expl)) {
// Locate Foo\Bar in foo/class/bar.class.inc
// Locate Foo\Bar in foo/class/bar.class.inc (for BC purposes)
$className = strtolower(end($expl));
$file =
DRUPAL_ROOT.
Expand Down

0 comments on commit 03f3a37

Please sign in to comment.