Skip to content

Commit

Permalink
Merge pull request zendframework#617 from Freeaqingme/defaultsAutoloa…
Browse files Browse the repository at this point in the history
…derFactoryFactory

Adding default parameters for AutoloaderFactory::factory()
Thanks
  • Loading branch information
Bittarman committed Nov 19, 2011
2 parents cbd9acb + 8f7510c commit 0a759f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
48 changes: 28 additions & 20 deletions library/Zend/Loader/AutoloaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AutoloaderFactory
protected static $loaders = array();

/**
* @var StandardAutoloader StandardAutoloader instance for resolving
* @var StandardAutoloader StandardAutoloader instance for resolving
* autoloader classes via the include_path
*/
protected static $standardAutoloader;
Expand All @@ -52,35 +52,43 @@ abstract class AutoloaderFactory
* )
* </code>
*
* The factory will then loop through and instantiate each autoloader with
* The factory will then loop through and instantiate each autoloader with
* the specified options, and register each with the spl_autoloader.
*
* You may retrieve the concrete autoloader instances later using
* You may retrieve the concrete autoloader instances later using
* {@link getRegisteredAutoloaders()}.
*
* Note that the class names must be resolvable on the include_path or via
* the Zend library, using PSR-0 rules (unless the class has already been
* the Zend library, using PSR-0 rules (unless the class has already been
* loaded).
*
* @param array|Traversable $options
*
* @param array|Traversable $options (optional) options to use. Defaults to Zend\Loader\StandardAutoloader
* @return void
* @throws Exception\InvalidArgumentException for invalid options
* @throws Exception\InvalidArgumentException for unloadable autoloader classes
* @throws Exception\DomainException for autoloader classes not implementing SplAutoloader
*/
public static function factory($options)
public static function factory($options = null)
{
if (null === $options) {
$options = array('Zend\Loader\StandardAutoloader' => array());
}

if (!is_array($options) && !($options instanceof \Traversable)) {
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
throw new Exception\InvalidArgumentException('Options provided must be an array or Traversable');
throw new Exception\InvalidArgumentException(
'Options provided must be an array or Traversable'
);
}

foreach ($options as $class => $options) {
if (!isset(static::$loaders[$class])) {
$autoloader = static::getStandardAutoloader();
if (!class_exists($class) && !$autoloader->autoload($class)) {
require_once 'Exception/InvalidArgumentException.php';
throw new Exception\InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class));
throw new Exception\InvalidArgumentException(
sprintf('Autoloader class "%s" not loaded', $class)
);
}
if ($class === 'Zend\Loader\StandardAutoloader') {
$autoloader->setOptions($options);
Expand All @@ -99,7 +107,7 @@ public static function factory($options)
* Get an list of all autoloaders registered with the factory
*
* Returns an array of autoloader instances.
*
*
* @return array
*/
public static function getRegisteredAutoloaders()
Expand All @@ -108,9 +116,9 @@ public static function getRegisteredAutoloaders()
}

/**
* Retrieves an autoloader by class name
*
* @param string $class
* Retrieves an autoloader by class name
*
* @param string $class
* @return SplAutoloader
* @throws Exception\InvalidArgumentException for non-registered class
*/
Expand All @@ -124,9 +132,9 @@ public static function getRegisteredAutoloader($class)
}

/**
* Unregisters all autoloaders that have been registered via the factory.
* Unregisters all autoloaders that have been registered via the factory.
* This will NOT unregister autoloaders registered outside of the fctory.
*
*
* @return void
*/
public static function unregisterAutoloaders()
Expand All @@ -139,8 +147,8 @@ public static function unregisterAutoloaders()

/**
* Unregister a single autoloader by class name
*
* @param string $autoloaderClass
*
* @param string $autoloaderClass
* @return bool
*/
public static function unregisterAutoloader($autoloaderClass)
Expand All @@ -158,10 +166,10 @@ public static function unregisterAutoloader($autoloaderClass)
/**
* Get an instance of the standard autoloader
*
* Used to attempt to resolve autoloader classes, using the
* StandardAutoloader. The instance is marked as a fallback autoloader, to
* Used to attempt to resolve autoloader classes, using the
* StandardAutoloader. The instance is marked as a fallback autoloader, to
* allow resolving autoloaders not under the "Zend" namespace.
*
*
* @return SplAutoloader
*/
protected static function getStandardAutoloader()
Expand Down
8 changes: 8 additions & 0 deletions tests/Zend/Loader/AutoloaderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ public function testCanGetValidRegisteredAutoloader()
$this->assertInstanceOf('Zend\Loader\StandardAutoloader', $autoloader);
}

public function testDefaultAutoloader()
{
AutoloaderFactory::factory();
$autoloader = AutoloaderFactory::getRegisteredAutoloader('Zend\Loader\StandardAutoloader');
$this->assertInstanceOf('Zend\Loader\StandardAutoloader', $autoloader);
$this->assertEquals(1, count(AutoloaderFactory::getRegisteredAutoloaders()));
}

public function testGetInvalidAutoloaderThrowsException()
{
$this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException');
Expand Down

0 comments on commit 0a759f4

Please sign in to comment.