Skip to content

Commit

Permalink
Merge branch 'develop' into feature-universalloader
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxist committed Jun 14, 2011
2 parents f9f22b1 + 27c4e49 commit b86badc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 65 deletions.
44 changes: 22 additions & 22 deletions src/Opl/Autoloader/ClassMapLoader.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ class ClassMapLoader
* @static * @static
* @var string * @var string
*/ */
private $_defaultPath = ''; private $defaultPath = '';


/** /**
* The list of available top-level namespaces. * The list of available top-level namespaces.
* @var array * @var array
*/ */
private $_namespaces = array(); private $namespaces = array();
/** /**
* The loaded class map. * The loaded class map.
* @var array * @var array
* @internal * @internal
*/ */
protected $_classMap; protected $classMap;


/** /**
* The location where the class map is stored. * The location where the class map is stored.
* @var string * @var string
* @internal * @internal
*/ */
protected $_classMapLocation; protected $classMapLocation;


/** /**
* Creates the class map loader and loads the map into the memory. * Creates the class map loader and loads the map into the memory.
Expand All @@ -61,15 +61,15 @@ class ClassMapLoader
public function __construct($defaultPath, $classMapLocation, Cache $cache = null) public function __construct($defaultPath, $classMapLocation, Cache $cache = null)
{ {
$this->setDefaultPath($defaultPath); $this->setDefaultPath($defaultPath);
$this->_classMapLocation = $classMapLocation; $this->classMapLocation = $classMapLocation;


if(null !== $cache) if(null !== $cache)
{ {
$this->_classMap = $cache->get('classMap'); $this->classMap = $cache->get('classMap');
if(null === $this->_classMap) if(null === $this->classMap)
{ {
$this->_loadMap(); $this->_loadMap();
$cache->set('classMap', $this->_classMap); $cache->set('classMap', $this->classMap);
} }
} }
else else
Expand All @@ -86,13 +86,13 @@ public function __construct($defaultPath, $classMapLocation, Cache $cache = null
*/ */
protected function _loadMap() protected function _loadMap()
{ {
if(!file_exists($this->_classMapLocation)) if(!file_exists($this->classMapLocation))
{ {
throw new RuntimeException('Cannot find a class map under the specified location.'); throw new RuntimeException('Cannot find a class map under the specified location.');
} }
$this->_classMap = unserialize(file_get_contents($this->_classMapLocation)); $this->classMap = unserialize(file_get_contents($this->classMapLocation));


if(!is_array($this->_classMap)) if(!is_array($this->classMap))
{ {
throw new RuntimeException('The loaded file does not contain a valid class map.'); throw new RuntimeException('The loaded file does not contain a valid class map.');
} }
Expand All @@ -108,7 +108,7 @@ protected function _loadMap()
*/ */
public function addNamespace($namespace, $path = null) public function addNamespace($namespace, $path = null)
{ {
if(isset($this->_namespaces[(string)$namespace])) if(isset($this->namespaces[(string)$namespace]))
{ {
throw new RuntimeException('The namespace '.$namespace.' is already added.'); throw new RuntimeException('The namespace '.$namespace.' is already added.');
} }
Expand All @@ -119,11 +119,11 @@ public function addNamespace($namespace, $path = null)
{ {
$path .= '/'; $path .= '/';
} }
$this->_namespaces[(string)$namespace] = $path; $this->namespaces[(string)$namespace] = $path;
} }
else else
{ {
$this->_namespaces[(string)$namespace] = $this->_defaultPath; $this->namespaces[(string)$namespace] = $this->defaultPath;
} }
} // end addNamespace(); } // end addNamespace();


Expand All @@ -134,7 +134,7 @@ public function addNamespace($namespace, $path = null)
*/ */
public function hasNamespace($namespace) public function hasNamespace($namespace)
{ {
return isset($this->_namespaces[(string)$namespace]); return isset($this->namespaces[(string)$namespace]);
} // end hasNamespace(); } // end hasNamespace();


/** /**
Expand All @@ -145,11 +145,11 @@ public function hasNamespace($namespace)
*/ */
public function removeNamespace($namespace) public function removeNamespace($namespace)
{ {
if(!isset($this->_namespaces[(string)$namespace])) if(!isset($this->namespaces[(string)$namespace]))
{ {
throw new RuntimeException('The namespace '.$namespace.' is not available.'); throw new RuntimeException('The namespace '.$namespace.' is not available.');
} }
unset($this->_namespaces[(string)$namespace]); unset($this->namespaces[(string)$namespace]);
} // end removeNamespace(); } // end removeNamespace();


/** /**
Expand All @@ -165,7 +165,7 @@ public function setDefaultPath($defaultPath)
{ {
$defaultPath .= '/'; $defaultPath .= '/';
} }
$this->_defaultPath = $defaultPath; $this->defaultPath = $defaultPath;
} // end setDefaultPath(); } // end setDefaultPath();


/** /**
Expand All @@ -175,7 +175,7 @@ public function setDefaultPath($defaultPath)
*/ */
public function getDefaultPath() public function getDefaultPath()
{ {
return $this->_defaultPath; return $this->defaultPath;
} // end getDefaultPath(); } // end getDefaultPath();


/** /**
Expand All @@ -185,7 +185,7 @@ public function getDefaultPath()
*/ */
public function getClassMapLocation() public function getClassMapLocation()
{ {
return $this->_classMapLocation; return $this->classMapLocation;
} // end getClassMapLocation(); } // end getClassMapLocation();


/** /**
Expand All @@ -212,11 +212,11 @@ public function unregister()
*/ */
public function loadClass($className) public function loadClass($className)
{ {
if(!isset($this->_classMap[$className])) if(!isset($this->classMap[$className]))
{ {
return false; return false;
} }
require($this->_namespaces[$this->_classMap[$className][0]].$this->_classMap[$className][1]); require($this->namespaces[$this->classMap[$className][0]].$this->classMap[$className][1]);
return true; return true;
} // end loadClass(); } // end loadClass();
} // end ClassMapLoader; } // end ClassMapLoader;
56 changes: 28 additions & 28 deletions src/Opl/Autoloader/GenericLoader.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* and other contributors. See website for details. * and other contributors. See website for details.
*/ */
namespace Opl\Autoloader; namespace Opl\Autoloader;
use RuntimeException; use DomainException;


/** /**
* The generic class autoloader is a slightly enhanced version of the * The generic class autoloader is a slightly enhanced version of the
Expand All @@ -28,25 +28,25 @@ class GenericLoader
* @static * @static
* @var string * @var string
*/ */
private $_defaultPath = ''; private $defaultPath = '';


/** /**
* The list of available top-level namespaces. * The list of available top-level namespaces.
* @var array * @var array
*/ */
private $_namespaces = array(); private $namespaces = array();


/** /**
* The file extensions in the namespaces. * The file extensions in the namespaces.
* @var array * @var array
*/ */
private $_extensions = array(); private $extensions = array();


/** /**
* The namespace separator * The namespace separator
* @var string * @var string
*/ */
private $_namespaceSeparator = '\\'; private $namespaceSeparator = '\\';


/** /**
* Constructs the autoloader. * Constructs the autoloader.
Expand All @@ -56,14 +56,14 @@ class GenericLoader
*/ */
public function __construct($defaultPath = './', $namespaceSeparator = '\\') public function __construct($defaultPath = './', $namespaceSeparator = '\\')
{ {
$this->_namespaceSeparator = $namespaceSeparator; $this->namespaceSeparator = $namespaceSeparator;


$length = strlen($defaultPath); $length = strlen($defaultPath);
if($length == 0 || $defaultPath[$length - 1] != '/') if($length == 0 || $defaultPath[$length - 1] != '/')
{ {
$defaultPath .= '/'; $defaultPath .= '/';
} }
$this->_defaultPath = $defaultPath; $this->defaultPath = $defaultPath;
} // end __construct(); } // end __construct();


/** /**
Expand All @@ -75,9 +75,9 @@ public function __construct($defaultPath = './', $namespaceSeparator = '\\')
*/ */
public function addNamespace($namespace, $path = null, $extension = '.php') public function addNamespace($namespace, $path = null, $extension = '.php')
{ {
if(isset($this->_namespaces[(string)$namespace])) if(isset($this->namespaces[(string)$namespace]))
{ {
throw new RuntimeException('The namespace '.$namespace.' is already added.'); throw new DomainException('The namespace '.$namespace.' is already added.');
} }
if($path !== null) if($path !== null)
{ {
Expand All @@ -86,13 +86,13 @@ public function addNamespace($namespace, $path = null, $extension = '.php')
{ {
$path .= '/'; $path .= '/';
} }
$this->_namespaces[(string)$namespace] = $path; $this->namespaces[(string)$namespace] = $path;
} }
else else
{ {
$this->_namespaces[(string)$namespace] = $this->_defaultPath; $this->namespaces[(string)$namespace] = $this->defaultPath;
} }
$this->_extensions[(string)$namespace] = $extension; $this->extensions[(string)$namespace] = $extension;
} // end addNamespace(); } // end addNamespace();


/** /**
Expand All @@ -102,7 +102,7 @@ public function addNamespace($namespace, $path = null, $extension = '.php')
*/ */
public function hasNamespace($namespace) public function hasNamespace($namespace)
{ {
return isset($this->_namespaces[(string)$namespace]); return isset($this->namespaces[(string)$namespace]);
} // end hasNamespace(); } // end hasNamespace();


/** /**
Expand All @@ -112,12 +112,12 @@ public function hasNamespace($namespace)
*/ */
public function removeNamespace($namespace) public function removeNamespace($namespace)
{ {
if(!isset($this->_namespaces[(string)$namespace])) if(!isset($this->namespaces[(string)$namespace]))
{ {
throw new RuntimeException('The namespace '.$namespace.' is not available.'); throw new DomainException('The namespace '.$namespace.' is not available.');
} }
unset($this->_namespaces[(string)$namespace]); unset($this->namespaces[(string)$namespace]);
unset($this->_extensions[(string)$namespace]); unset($this->extensions[(string)$namespace]);
} // end removeNamespace(); } // end removeNamespace();


/** /**
Expand All @@ -127,7 +127,7 @@ public function removeNamespace($namespace)
*/ */
public function setNamespaceSeparator($sep) public function setNamespaceSeparator($sep)
{ {
$this->_namespaceSeparator = $sep; $this->namespaceSeparator = $sep;
} // end setNamespaceSeparator(); } // end setNamespaceSeparator();


/** /**
Expand All @@ -137,7 +137,7 @@ public function setNamespaceSeparator($sep)
*/ */
public function getNamespaceSeparator() public function getNamespaceSeparator()
{ {
return $this->_namespaceSeparator; return $this->namespaceSeparator;
} // end getNamespaceSeparator(); } // end getNamespaceSeparator();


/** /**
Expand All @@ -152,7 +152,7 @@ public function setDefaultPath($defaultPath)
{ {
$defaultPath .= '/'; $defaultPath .= '/';
} }
$this->_defaultPath = $defaultPath; $this->defaultPath = $defaultPath;
} // end setDefaultPath(); } // end setDefaultPath();


/** /**
Expand All @@ -162,7 +162,7 @@ public function setDefaultPath($defaultPath)
*/ */
public function getDefaultPath() public function getDefaultPath()
{ {
return $this->_defaultPath; return $this->defaultPath;
} // end getDefaultPath(); } // end getDefaultPath();


/** /**
Expand All @@ -189,19 +189,19 @@ public function unregister()
*/ */
public function loadClass($className) public function loadClass($className)
{ {
$className = ltrim($className, $this->_namespaceSeparator); $className = ltrim($className, $this->namespaceSeparator);
$match = strstr($className, $this->_namespaceSeparator, true); $match = strstr($className, $this->namespaceSeparator, true);


if(false === $match || !isset($this->_namespaces[$match])) if(false === $match || !isset($this->namespaces[$match]))
{ {
return false; return false;
} }
$rest = strrchr($className, $this->_namespaceSeparator); $rest = strrchr($className, $this->namespaceSeparator);
$replacement = $replacement =
str_replace($this->_namespaceSeparator, '/', substr($className, 0, strlen($className) - strlen($rest))). str_replace($this->namespaceSeparator, '/', substr($className, 0, strlen($className) - strlen($rest))).
str_replace(array('_', $this->_namespaceSeparator), '/', $rest); str_replace(array('_', $this->namespaceSeparator), '/', $rest);


require($this->_namespaces[$match].$replacement.$this->_extensions[$match]); require($this->namespaces[$match].$replacement.$this->extensions[$match]);
return true; return true;
} // end loadClass(); } // end loadClass();
} // end GenericLoader; } // end GenericLoader;
8 changes: 4 additions & 4 deletions src/Opl/Autoloader/PHARLoader.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PHARLoader
* @var array * @var array
* @internal * @internal
*/ */
protected $_classMap; protected $classMap;


/** /**
* Creates the class map loader and loads the map into the memory. * Creates the class map loader and loads the map into the memory.
Expand All @@ -37,7 +37,7 @@ class PHARLoader
*/ */
public function __construct(array $classMap) public function __construct(array $classMap)
{ {
$this->_classMap = $classMap; $this->classMap = $classMap;
} // end __construct(); } // end __construct();


/** /**
Expand All @@ -64,11 +64,11 @@ public function unregister()
*/ */
public function loadClass($className) public function loadClass($className)
{ {
if(!isset($this->_classMap[$className])) if(!isset($this->classMap[$className]))
{ {
return false; return false;
} }
require('phar://'.__FILE__.'/'.$this->_classMap[$className][1]); require('phar://'.__FILE__.'/'.$this->classMap[$className][1]);
return true; return true;
} // end loadClass(); } // end loadClass();
} // end PHARLoader; } // end PHARLoader;
6 changes: 3 additions & 3 deletions tests/TestSuite/ClassMapLoaderTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testAddNamespaceSetsDefaultPath()
$loader->addNamespace('Foo'); $loader->addNamespace('Foo');


$reflection = new \ReflectionObject($loader); $reflection = new \ReflectionObject($loader);
$namespacesProperty = $reflection->getProperty('_namespaces'); $namespacesProperty = $reflection->getProperty('namespaces');
$namespacesProperty->setAccessible(true); $namespacesProperty->setAccessible(true);


$this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader)); $this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader));
Expand All @@ -89,7 +89,7 @@ public function testAddNamespaceSetsCustomPath()
$loader->addNamespace('Foo', './bar/joe/'); $loader->addNamespace('Foo', './bar/joe/');


$reflection = new \ReflectionObject($loader); $reflection = new \ReflectionObject($loader);
$namespacesProperty = $reflection->getProperty('_namespaces'); $namespacesProperty = $reflection->getProperty('namespaces');
$namespacesProperty->setAccessible(true); $namespacesProperty->setAccessible(true);


$this->assertEquals(array('Foo' => './bar/joe/'), $namespacesProperty->getValue($loader)); $this->assertEquals(array('Foo' => './bar/joe/'), $namespacesProperty->getValue($loader));
Expand All @@ -113,7 +113,7 @@ public function testRemoveNamespace()
$this->assertTrue($loader->hasNamespace('Foo')); $this->assertTrue($loader->hasNamespace('Foo'));


$reflection = new \ReflectionObject($loader); $reflection = new \ReflectionObject($loader);
$namespacesProperty = $reflection->getProperty('_namespaces'); $namespacesProperty = $reflection->getProperty('namespaces');
$namespacesProperty->setAccessible(true); $namespacesProperty->setAccessible(true);


$this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader)); $this->assertEquals(array('Foo' => './foo/bar/'), $namespacesProperty->getValue($loader));
Expand Down
Loading

0 comments on commit b86badc

Please sign in to comment.