Permalink
Browse files

Renaming "library" to "namespace" in the method names of GenericLoader.

  • Loading branch information...
1 parent 0a846c0 commit d123f7461a62fa816ba58e290cc196aab41c6fb1 @zyxist zyxist committed Feb 1, 2011
Showing with 92 additions and 80 deletions.
  1. +2 −2 autoload.example.php
  2. +33 −33 src/Opl/Autoloader/GenericLoader.php
  3. +54 −42 tests/TestSuite/GenericLoaderTest.php
  4. +3 −3 tests/bootstrap.dist.php
View
@@ -18,8 +18,8 @@
// Please provide here the correct path to the Symfony as
// a secondary argument, if different than the default one.
-$loader->addLibrary('Opl');
+$loader->addNamespace('Opl');
// Please provide here the correct path to the Symfony as
// a secondary argument, if different than the default one.
-$loader->addLibrary('Symfony');
+$loader->addNamespace('Symfony');
$loader->register();
@@ -31,13 +31,13 @@ class GenericLoader
private $_defaultPath = '';
/**
- * The list of available libraries.
+ * The list of available top-level namespaces.
* @var array
*/
- private $_libraries = array();
+ private $_namespaces = array();
/**
- * The library extensions.
+ * The file extensions in the namespaces.
* @var array
*/
private $_extensions = array();
@@ -51,7 +51,7 @@ class GenericLoader
/**
* Constructs the autoloader.
*
- * @param string $defaultPath The default library path.
+ * @param string $defaultPath The default namespace path.
* @param string $namespaceSeparator The namespace separator used in this autoloader.
*/
public function __construct($defaultPath = './', $namespaceSeparator = '\\')
@@ -67,17 +67,17 @@ public function __construct($defaultPath = './', $namespaceSeparator = '\\')
} // end __construct();
/**
- * Registers a new library to match.
+ * Registers a new top-level namespace to match.
*
- * @param string $library The library name to add.
- * @param string $path The path to the library.
- * @param string $extension The library file extension.
+ * @param string $namespace The namespace name to add.
+ * @param string $path The path to the namespace (without the namespace name itself).
+ * @param string $extension The namespace file extension.
*/
- public function addLibrary($library, $path = null, $extension = '.php')
+ public function addNamespace($namespace, $path = null, $extension = '.php')
{
- if(isset($this->_libraries[(string)$library]))
+ if(isset($this->_namespaces[(string)$namespace]))
{
- throw new RuntimeException('Library '.$library.' is already added.');
+ throw new RuntimeException('The namespace '.$namespace.' is already added.');
}
if($path !== null)
{
@@ -86,39 +86,39 @@ public function addLibrary($library, $path = null, $extension = '.php')
{
$path .= '/';
}
- $this->_libraries[(string)$library] = $path;
+ $this->_namespaces[(string)$namespace] = $path;
}
else
{
- $this->_libraries[(string)$library] = $this->_defaultPath;
+ $this->_namespaces[(string)$namespace] = $this->_defaultPath;
}
- $this->_extensions[(string)$library] = $extension;
- } // end addLibrary();
+ $this->_extensions[(string)$namespace] = $extension;
+ } // end addNamespace();
/**
- * Checks if the specified library is available.
+ * Checks if the specified top-level namespace is available.
*
- * @param string $library The library name to check.
+ * @param string $namespace The namespace name to check.
*/
- public function hasLibrary($library)
+ public function hasNamespace($namespace)
{
- return isset($this->_libraries[(string)$library]);
- } // end hasLibrary();
+ return isset($this->_namespaces[(string)$namespace]);
+ } // end hasNamespace();
/**
- * Removes a recognized library.
+ * Removes a registered top-level namespace.
*
- * @param string $library The library name to remove.
+ * @param string $namespace The namespace name to remove.
*/
- public function removeLibrary($library)
+ public function removeNamespace($namespace)
{
- if(!isset($this->_libraries[(string)$library]))
+ if(!isset($this->_namespaces[(string)$namespace]))
{
- throw new RuntimeException('Library '.$library.' is not available.');
+ throw new RuntimeException('The namespace '.$namespace.' is not available.');
}
- unset($this->_libraries[(string)$library]);
- unset($this->_extensions[(string)$library]);
- } // end removeLibrary();
+ unset($this->_namespaces[(string)$namespace]);
+ unset($this->_extensions[(string)$namespace]);
+ } // end removeNamespace();
/**
* Sets the namespace separator used by classes in the namespace of this class loader.
@@ -141,8 +141,8 @@ public function getNamespaceSeparator()
} // end getNamespaceSeparator();
/**
- * Sets the default path used by the libraries. Note that it does not affect
- * the already added libraries.
+ * Sets the default path used by the namespaces. Note that it does not affect
+ * the already added namespaces.
*
* @param string $defaultPath The new default path.
*/
@@ -156,7 +156,7 @@ public function setDefaultPath($defaultPath)
} // end setDefaultPath();
/**
- * Returns the default path used by the libraries.
+ * Returns the default path used by the namespaces.
*
* @return string The current default path.
*/
@@ -192,7 +192,7 @@ public function loadClass($className)
$className = ltrim($className, $this->_namespaceSeparator);
$match = strstr($className, $this->_namespaceSeparator, true);
- if(false === $match || !isset($this->_libraries[$match]))
+ if(false === $match || !isset($this->_namespaces[$match]))
{
return false;
}
@@ -201,7 +201,7 @@ public function loadClass($className)
str_replace($this->_namespaceSeparator, '/', substr($className, 0, strlen($className) - strlen($rest))).
str_replace(array('_', $this->_namespaceSeparator), '/', $rest);
- require($this->_libraries[$match].$replacement.$this->_extensions[$match]);
+ require($this->_namespaces[$match].$replacement.$this->_extensions[$match]);
return true;
} // end loadClass();
} // end GenericLoader;
@@ -68,92 +68,92 @@ public function testSetNamespaceSeparator()
$this->assertEquals('bar', $loader->getNamespaceSeparator());
} // end testSetNamespaceSeparator();
- public function testAddingLibrary()
+ public function testAddingNamespace()
{
$loader = new GenericLoader('./foo/bar/');
- $this->assertFalse($loader->hasLibrary('Foo'));
- $this->assertFalse($loader->hasLibrary('Bar'));
+ $this->assertFalse($loader->hasNamespace('Foo'));
+ $this->assertFalse($loader->hasNamespace('Bar'));
- $loader->addLibrary('Foo');
+ $loader->addNamespace('Foo');
- $this->assertTrue($loader->hasLibrary('Foo'));
- $this->assertFalse($loader->hasLibrary('Bar'));
- } // end testAddingLibrary();
+ $this->assertTrue($loader->hasNamespace('Foo'));
+ $this->assertFalse($loader->hasNamespace('Bar'));
+ } // end testAddingNamespace();
- public function testAddLibrarySetsDefaultPathAndException()
+ public function testAddNamespaceSetsDefaultPathAndExtension()
{
$loader = new GenericLoader('./foo/bar/');
- $loader->addLibrary('Foo');
+ $loader->addNamespace('Foo');
$reflection = new \ReflectionObject($loader);
- $librariesProperty = $reflection->getProperty('_libraries');
- $librariesProperty->setAccessible(true);
+ $namespacesProperty = $reflection->getProperty('_namespaces');
+ $namespacesProperty->setAccessible(true);
$extensionsProperty = $reflection->getProperty('_extensions');
$extensionsProperty->setAccessible(true);
- $this->assertEquals(array('Foo' => './foo/bar/'), $librariesProperty->getValue($loader));
+ $this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader));
$this->assertEquals(array('Foo' => '.php'), $extensionsProperty->getValue($loader));
- } // end testAddLibrarySetsDefaultPathAndException();
+ } // end testAddNamespaceSetsDefaultPathAndExtension();
- public function testAddLibrarySetsCustomPathAndException()
+ public function testAddNamespaceSetsCustomPathAndExtension()
{
$loader = new GenericLoader('./foo/bar/');
- $loader->addLibrary('Foo', './bar/joe/', '.php5');
+ $loader->addNamespace('Foo', './bar/joe/', '.php5');
$reflection = new \ReflectionObject($loader);
- $librariesProperty = $reflection->getProperty('_libraries');
- $librariesProperty->setAccessible(true);
+ $namespacesProperty = $reflection->getProperty('_namespaces');
+ $namespacesProperty->setAccessible(true);
$extensionsProperty = $reflection->getProperty('_extensions');
$extensionsProperty->setAccessible(true);
- $this->assertEquals(array('Foo' => './bar/joe/'), $librariesProperty->getValue($loader));
+ $this->assertEquals(array('Foo' => './bar/joe/'), $namespacesProperty->getValue($loader));
$this->assertEquals(array('Foo' => '.php5'), $extensionsProperty->getValue($loader));
- } // end testAddLibrarySetsCustomPathAndException();
+ } // end testAddNamespaceSetsCustomPathAndExtension();
/**
* @expectedException RuntimeException
*/
- public function testAddLibraryThrowsExceptionWhenLibraryExists()
+ public function testAddNamespaceThrowsExceptionWhenNamespaceExists()
{
$loader = new GenericLoader('./foo/bar/');
- $loader->addLibrary('Foo');
- $this->assertTrue($loader->hasLibrary('Foo'));
- $loader->addLibrary('Foo');
- } // end testAddLibraryThrowsExceptionWhenLibraryExists();
+ $loader->addNamespace('Foo');
+ $this->assertTrue($loader->hasNamespace('Foo'));
+ $loader->addNamespace('Foo');
+ } // end testNamespaceThrowsExceptionWhenNamespaceExists();
- public function testRemoveLibrary()
+ public function testRemoveNamespace()
{
$loader = new GenericLoader('./foo/bar/');
- $loader->addLibrary('Foo');
- $this->assertTrue($loader->hasLibrary('Foo'));
+ $loader->addNamespace('Foo');
+ $this->assertTrue($loader->hasNamespace('Foo'));
$reflection = new \ReflectionObject($loader);
- $librariesProperty = $reflection->getProperty('_libraries');
- $librariesProperty->setAccessible(true);
+ $namespacesProperty = $reflection->getProperty('_namespaces');
+ $namespacesProperty->setAccessible(true);
$extensionsProperty = $reflection->getProperty('_extensions');
$extensionsProperty->setAccessible(true);
- $this->assertEquals(array('Foo' => './foo/bar/'), $librariesProperty->getValue($loader));
+ $this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader));
$this->assertEquals(array('Foo' => '.php'), $extensionsProperty->getValue($loader));
- $loader->removeLibrary('Foo');
- $this->assertFalse($loader->hasLibrary('Foo'));
+ $loader->removeNamespace('Foo');
+ $this->assertFalse($loader->hasNamespace('Foo'));
- $this->assertEquals(array(), $librariesProperty->getValue($loader));
+ $this->assertEquals(array(), $namespacesProperty->getValue($loader));
$this->assertEquals(array(), $extensionsProperty->getValue($loader));
} // end testRemoveLibrary();
/**
- * @depends testRemoveLibrary
+ * @depends testRemoveNamespace
* @expectedException RuntimeException
*/
- public function testRemoveLibraryThrowsExceptionWhenLibraryDoesNotExist()
+ public function testRemoveNamespaceThrowsExceptionWhenNamespaceDoesNotExist()
{
$loader = new GenericLoader('./foo/bar/');
- $this->assertFalse($loader->hasLibrary('Moo'));
- $loader->removeLibrary('Moo');
- } // end testRemoveLibraryThrowsExceptionWhenLibraryDoesNotExist();
+ $this->assertFalse($loader->hasNamespace('Moo'));
+ $loader->removeNamespace('Moo');
+ } // end testRemoveNamespaceThrowsExceptionWhenNamespaceDoesNotExist();
public function testRegisterWorks()
{
@@ -178,6 +178,9 @@ public function testUnregisterWorks()
$this->assertThat($functions, $this->logicalNot($this->contains(array($loader, 'loadClass'))));
} // end testUnregisterWorks();
+ /**
+ * @depends testAddingNamespace
+ */
public function testLoaderReplacesNSToSlashes()
{
$file = new \vfsStreamFile('Bar.php');
@@ -189,14 +192,17 @@ public function testLoaderReplacesNSToSlashes()
\vfsStreamWrapper::setRoot($topLevelDir);
$loader = new GenericLoader(\vfsStream::url(''));
- $loader->addLibrary('Foo');
+ $loader->addNamespace('Foo');
$loader->register();
ob_start();
spl_autoload_call('Foo\\Bar');
$this->assertEquals('FOO\\BAR.PHP', ob_get_clean());
} // end testLoaderReplacesNSToSlashes();
+ /**
+ * @depends testAddingNamespace
+ */
public function testLoaderReplacesUnderscoresToSlashesInClassNames()
{
$file = new \vfsStreamFile('Joe.php');
@@ -211,14 +217,17 @@ public function testLoaderReplacesUnderscoresToSlashesInClassNames()
\vfsStreamWrapper::setRoot($topLevelDir);
$loader = new GenericLoader(\vfsStream::url(''));
- $loader->addLibrary('Foo');
+ $loader->addNamespace('Foo');
$loader->register();
ob_start();
spl_autoload_call('Foo\\Bar_Joe');
$this->assertEquals('FOO\\BAR\\JOE.PHP', ob_get_clean());
} // end testLoaderReplacesUnderscoresToSlashesInClassNames();
+ /**
+ * @depends testAddingNamespace
+ */
public function testLoaderDoesNotReplaceUnderscoresToSlashesInNamespace()
{
$file = new \vfsStreamFile('Goo.php');
@@ -233,18 +242,21 @@ public function testLoaderDoesNotReplaceUnderscoresToSlashesInNamespace()
\vfsStreamWrapper::setRoot($topLevelDir);
$loader = new GenericLoader(\vfsStream::url(''));
- $loader->addLibrary('Foo');
+ $loader->addNamespace('Foo');
$loader->register();
ob_start();
spl_autoload_call('Foo\\Bar_Joe\\Goo');
$this->assertEquals('FOO\\BAR_JOE\\GOO.PHP', ob_get_clean());
} // end testLoaderDoesNotReplaceUnderscoresToSlashesInNamespace();
+ /**
+ * @depends testAddingNamespace
+ */
public function testSkippingUnknownLibraries()
{
$loader = new GenericLoader('./foo/bar/');
- $loader->addLibrary('Dummy');
+ $loader->addNamespace('Dummy');
$loader->register();
spl_autoload_register(function($name){ echo 'yey'; return true; });
View
@@ -24,9 +24,9 @@
use Opl\Autoloader\GenericLoader;
$loader = new GenericLoader(DIR_LIB);
// do not add "Opl" and "Symfony" at the end of the paths!
-$loader->addLibrary('TestSuite', './');
-$loader->addLibrary('Opl', '/your/path/here');
-$loader->addLibrary('Symfony', '/your/path/here');
+$loader->addNamespace('TestSuite', './');
+$loader->addNamespace('Opl', '/your/path/here');
+$loader->addNamespace('Symfony', '/your/path/here');
$loader->register();
/**

0 comments on commit d123f74

Please sign in to comment.