Skip to content
Browse files

Brought up-to-date with latest ZF2 revisions

- Based on r3937d09 of ZF2 master
  • Loading branch information...
1 parent 3937d09 commit 192c689568d5c5415b2a7ab1818602159d02425e @weierophinney committed
View
50 library/ZendX/Loader/AutoloaderFactory.php
@@ -20,6 +20,8 @@
require_once dirname(__FILE__) . '/SplAutoloader.php';
+if (class_exists('ZendX_Loader_AutoloaderFactory')) return;
+
/**
* @package ZendX_Loader
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
@@ -27,6 +29,8 @@
*/
abstract class ZendX_Loader_AutoloaderFactory
{
+ const STANDARD_AUTOLOADER = 'ZendX_Loader_StandardAutoloader';
+
/**
* @var array All autoloaders registered using the factory
*/
@@ -66,13 +70,20 @@
public static function factory($options = null)
{
if (null === $options) {
- $options = array('ZendX_Loader_StandardAutoloader' => array());
+ if (!isset(self::$loaders[self::STANDARD_AUTOLOADER])) {
+ $autoloader = self::getStandardAutoloader();
+ $autoloader->register();
+ self::$loaders[self::STANDARD_AUTOLOADER] = $autoloader;
+ }
+
+ // Return so we don't hit the next check's exception (we're done here anyway)
+ return;
}
if (!is_array($options) && !($options instanceof Traversable)) {
- require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
+ require_once 'Exception/InvalidArgumentException.php';
throw new ZendX_Loader_Exception_InvalidArgumentException(
- 'Options provided must be an array or Traversable'
+ 'Options provided must be an array or Traversable'
);
}
@@ -80,12 +91,26 @@ public static function factory($options = null)
if (!isset(self::$loaders[$class])) {
$autoloader = self::getStandardAutoloader();
if (!class_exists($class) && !$autoloader->autoload($class)) {
- require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
- throw new ZendX_Loader_Exception_InvalidArgumentException(
- sprintf('Autoloader class "%s" not loaded', $class)
- );
+ require_once 'Exception/InvalidArgumentException.php';
+ throw new ZendX_Loader_Exception_InvalidArgumentException(sprintf(
+ 'Autoloader class "%s" not loaded',
+ $class
+ ));
+ }
+
+ // unfortunately is_subclass_of is broken on some 5.3 versions
+ // additionally instanceof is also broken for this use case
+ if (version_compare(PHP_VERSION, '5.3.7', '>=')) {
+ if (!is_subclass_of($class, 'ZendX_Loader_SplAutoloader')) {
+ require_once 'Exception/InvalidArgumentException.php';
+ throw new ZendX_Loader_Exception_InvalidArgumentException(sprintf(
+ 'Autoloader class %s must implement Zend\\Loader\\SplAutoloader',
+ $class
+ ));
+ }
}
- if ($class === 'ZendX_Loader_StandardAutoloader') {
+
+ if ($class === self::STANDARD_AUTOLOADER) {
$autoloader->setOptions($options);
} else {
$autoloader = new $class($options);
@@ -120,7 +145,7 @@ public static function getRegisteredAutoloaders()
public static function getRegisteredAutoloader($class)
{
if (!isset(self::$loaders[$class])) {
- require_once dirname(__FILE__) . '/Exception/InvalidArgumentException.php';
+ require_once 'Exception/InvalidArgumentException.php';
throw new ZendX_Loader_Exception_InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class));
}
return self::$loaders[$class];
@@ -173,8 +198,11 @@ protected static function getStandardAutoloader()
return self::$standardAutoloader;
}
- if (!class_exists('ZendX_Loader_StandardAutoloader')) {
- require_once dirname(__FILE__) . '/StandardAutoloader.php';
+ // Extract the filename from the classname
+ $stdAutoloader = substr(strrchr(self::STANDARD_AUTOLOADER, '_'), 1);
+
+ if (!class_exists(self::STANDARD_AUTOLOADER)) {
+ require_once dirname(__FILE__) . "/$stdAutoloader.php";
}
$loader = new ZendX_Loader_StandardAutoloader();
self::$standardAutoloader = $loader;
View
6 library/ZendX/Loader/ClassMapAutoloader.php
@@ -156,7 +156,11 @@ public function autoload($class)
*/
public function register()
{
- spl_autoload_register(array($this, 'autoload'), true);
+ if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
+ spl_autoload_register(array($this, 'autoload'), true, true);
+ } else {
+ spl_autoload_register(array($this, 'autoload'), true);
+ }
}
/**
View
9 library/ZendX/Loader/StandardAutoloader.php
@@ -265,10 +265,15 @@ protected function transformClassNameToFilename($class, $directory)
{
// $class may contain a namespace portion, in which case we need
// to preserve any underscores in that portion.
+ $matches = array();
preg_match('/(?P<namespace>.+\\\)?(?P<class>[^\\\]+$)/', $class, $matches);
+
+ $class = (isset($matches['class'])) ? $matches['class'] : '';
+ $namespace = (isset($matches['namespace'])) ? $matches['namespace'] : '';
+
return $directory
- . str_replace(self::NS_SEPARATOR, '/', $matches['namespace'])
- . str_replace(self::PREFIX_SEPARATOR, '/', $matches['class'])
+ . str_replace(self::NS_SEPARATOR, '/', $namespace)
+ . str_replace(self::PREFIX_SEPARATOR, '/', $class)
. '.php';
}
View
35 tests/ZendX/Loader/AutoloaderFactoryTest.php
@@ -92,6 +92,22 @@ public function testRegisteringValidMapFilePopulatesAutoloader()
$this->assertEquals(2, count($map));
}
+ /**
+ * This tests checks if invalid autoloaders cause exceptions
+ *
+ * @expectedException InvalidArgumentException
+ */
+ public function testFactoryCatchesInvalidClasses()
+ {
+ if (!version_compare(PHP_VERSION, '5.3.7', '>=')) {
+ $this->markTestSkipped('Cannot test invalid interface loader with versions less than 5.3.7');
+ }
+ include dirname(__FILE__) . '/_files/InvalidInterfaceAutoloader.php';
+ ZendX_Loader_AutoloaderFactory::factory(array(
+ 'InvalidInterfaceAutoloader' => array()
+ ));
+ }
+
public function testFactoryDoesNotRegisterDuplicateAutoloaders()
{
ZendX_Loader_AutoloaderFactory::factory(array(
@@ -185,4 +201,23 @@ public function testCannotBeInstantiatedViaConstructor()
$constructor = $reflection->getConstructor();
$this->assertNull($constructor);
}
+
+ public function testPassingNoArgumentsToFactoryInstantiatesAndRegistersStandardAutoloader()
+ {
+ ZendX_Loader_AutoloaderFactory::factory();
+ $loaders = ZendX_Loader_AutoloaderFactory::getRegisteredAutoloaders();
+ $this->assertEquals(1, count($loaders));
+ $loader = array_shift($loaders);
+ $this->assertInstanceOf('ZendX_Loader_StandardAutoloader', $loader);
+
+ $test = array($loader, 'autoload');
+ $found = false;
+ foreach (spl_autoload_functions() as $function) {
+ if ($function === $test) {
+ $found = true;
+ break;
+ }
+ }
+ $this->assertTrue($found, 'StandardAutoloader not registered with spl_autoload');
+ }
}
View
2 tests/ZendX/Loader/ClassMapAutoloaderTest.php
@@ -173,7 +173,7 @@ public function testRegisterRegistersCallbackWithSplAutoload()
$this->loader->register();
$loaders = spl_autoload_functions();
$this->assertTrue(count($this->loaders) < count($loaders));
- $test = array_pop($loaders);
+ $test = array_shift($loaders);
$this->assertEquals(array($this->loader, 'autoload'), $test);
}
View
7 tests/ZendX/Loader/StandardAutoloaderTest.php
@@ -164,6 +164,13 @@ public function testReturnsFalseForUnresolveableClassNames()
$this->assertFalse($loader->autoload('Some\Fake\Classname'));
}
+ public function testReturnsFalseForInvalidClassNames()
+ {
+ $loader = new ZendX_Loader_StandardAutoloader();
+ $loader->setFallbackAutoloader(true);
+ $this->assertFalse($loader->autoload('Some_Invalid_Classname_'));
+ }
+
public function testRegisterRegistersCallbackWithSplAutoload()
{
$loader = new ZendX_Loader_StandardAutoloader();
View
4 tests/ZendX/Loader/_files/InvalidInterfaceAutoloader.php
@@ -0,0 +1,4 @@
+<?php
+class InvalidInterfaceAutoloader
+{
+}

0 comments on commit 192c689

Please sign in to comment.
Something went wrong with that request. Please try again.