From 0eb8cd1c6b2b25c07e8ba2e9585f0d52c2e40378 Mon Sep 17 00:00:00 2001 From: Shawn Stratton Date: Sun, 24 Apr 2011 20:56:32 -0500 Subject: [PATCH 1/4] Fixed a race condition in which the StandardAutoloader was required twice --- src/AutoloaderFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AutoloaderFactory.php b/src/AutoloaderFactory.php index fc9559b..29e1855 100644 --- a/src/AutoloaderFactory.php +++ b/src/AutoloaderFactory.php @@ -86,7 +86,8 @@ public static function factory($options) foreach ($options as $class => $opts) { if (!class_exists($class)) { - if (!self::getStandardAutoloader()->autoload($class)) { + $autoloader = self::getStandardAutoloader(); + if (!class_exists($class) && !$autoloader->autoload($class)) require_once 'Exception/InvalidArgumentException.php'; throw new Exception\InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class)); } From be69400c459096a514ff4b24f7de3e1ae29c1e48 Mon Sep 17 00:00:00 2001 From: Shawn Stratton Date: Wed, 27 Apr 2011 10:06:00 -0400 Subject: [PATCH 2/4] Fixed some Namespace issues (referencing wrong namespace on exception --- src/ResourceAutoloader.php | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/ResourceAutoloader.php b/src/ResourceAutoloader.php index 50ac173..97ac0db 100644 --- a/src/ResourceAutoloader.php +++ b/src/ResourceAutoloader.php @@ -65,6 +65,7 @@ class ResourceAutoloader implements SplAutoloader * Constructor * * @param array|Traversable $options Configuration options for resource autoloader + * @throws Exception\InvalidArgumentException * @return void */ public function __construct($options = null) @@ -102,23 +103,24 @@ public function __construct($options = null) * @param string $method * @param array $args * @return mixed - * @throws Zend_Loader_Exception if method not beginning with 'get' or not matching a valid resource type is called + * @throws Exception\InvalidArgumentException if method not beginning with 'get' or not matching a valid resource type is called + * @throws Exception\BadMethodCallException */ public function __call($method, $args) { if ('get' == substr($method, 0, 3)) { $type = strtolower(substr($method, 3)); if (!$this->hasResourceType($type)) { - throw new InvalidArgumentException("Invalid resource type $type; cannot load resource"); + throw new Exception\InvalidArgumentException("Invalid resource type $type; cannot load resource"); } if (empty($args)) { - throw new InvalidArgumentException("Cannot load resources; no resource specified"); + throw new Exception\InvalidArgumentException("Cannot load resources; no resource specified"); } $resource = array_shift($args); return $this->load($resource, $type); } - throw new BadMethodCallException("Method '$method' is not supported"); + throw new Exception\BadMethodCallException("Method '$method' is not supported"); } /** @@ -268,7 +270,8 @@ public function register() * Set class state from options * * @param array $options - * @return Zend_Loader_Autoloader_Resource + * @throws Exception\InvalidArgumentExceptions + * @return \Zend\Loader\Autoloader\Resource */ public function setOptions($options) { @@ -370,6 +373,8 @@ public function getBasePath() * @param string $type identifier for the resource type being loaded * @param string $path path relative to resource base path containing the resource types * @param null|string $namespace sub-component namespace to append to base namespace that qualifies this resource type + * @throws Exception\MissingResourceNamespaceException + * @throws Exception\InvalidPathException * @return Zend_Loader_Autoloader_Resource */ public function addResourceType($type, $path, $namespace = null) @@ -377,7 +382,7 @@ public function addResourceType($type, $path, $namespace = null) $type = strtolower($type); if (!isset($this->_resourceTypes[$type])) { if (null === $namespace) { - throw new MissingResourceNamespaceException('Initial definition of a resource type must include a namespace'); + throw new Exception\MissingResourceNamespaceException('Initial definition of a resource type must include a namespace'); } if (null !== $this->getNamespace()) { $this->_addNamespaceResource($type, $namespace); @@ -386,7 +391,7 @@ public function addResourceType($type, $path, $namespace = null) } } if (!is_string($path)) { - throw new InvalidPathException('Invalid path specification provided; must be string'); + throw new Exception\InvalidPathException('Invalid path specification provided; must be string'); } $this->_resourceTypes[$type]['path'] = $this->getBasePath() . '/' . rtrim($path, '\/'); @@ -451,16 +456,17 @@ protected function _addPrefixResource($type, $prefix) * * * @param array $types - * @return Zend_Loader_Autoloader_Resource + * @throws Exception\InvalidArgumentException + * @return \Zend\Loader\Autoloader\Resource */ public function addResourceTypes(array $types) { foreach ($types as $type => $spec) { if (!is_array($spec)) { - throw new InvalidArgumentException('addResourceTypes() expects an array of arrays'); + throw new Exception\InvalidArgumentException('addResourceTypes() expects an array of arrays'); } if (!isset($spec['path'])) { - throw new InvalidArgumentException('addResourceTypes() expects each array to include a paths element'); + throw new Exception\InvalidArgumentException('addResourceTypes() expects each array to include a paths element'); } $paths = $spec['path']; $namespace = null; @@ -568,18 +574,18 @@ public function getDefaultResourceType() * @param string $resource * @param string $type * @return object - * @throws Zend_Loader_Exception if resource type not specified or invalid + * @throws Exception\InvalidArgumentException if resource type not specified or invalid */ public function load($resource, $type = null) { if (null === $type) { $type = $this->getDefaultResourceType(); if (empty($type)) { - throw new InvalidArgumentException('No resource type specified'); + throw new Exception\InvalidArgumentException('No resource type specified'); } } if (!$this->hasResourceType($type)) { - throw new InvalidArgumentException('Invalid resource type specified'); + throw new Exception\InvalidArgumentException('Invalid resource type specified'); } $namespace = $this->_resourceTypes[$type]['namespace']; if (null !== $this->getNamespace()) { From 64de91308de693d8927f6b819d5e4fa24e6263f1 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 28 Apr 2011 16:20:40 -0500 Subject: [PATCH 3/4] Fixed test issues - Exceptions have moved to a new namespace - Loaders must call register() to be active --- src/ResourceAutoloader.php | 4 ++- test/ResourceAutoloaderTest.php | 51 ++++++++++++--------------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/ResourceAutoloader.php b/src/ResourceAutoloader.php index 585e81c..c8d4f90 100644 --- a/src/ResourceAutoloader.php +++ b/src/ResourceAutoloader.php @@ -132,7 +132,9 @@ public function __call($method, $args) public function getClassPath($class) { if (null !== $this->getNamespace()) { - return $this->getNamespacedClassPath($class); + if (false !== ($path = $this->getNamespacedClassPath($class))) { + return $path; + } } return $this->getPrefixedClassPath($class); } diff --git a/test/ResourceAutoloaderTest.php b/test/ResourceAutoloaderTest.php index a6dd831..70b2029 100644 --- a/test/ResourceAutoloaderTest.php +++ b/test/ResourceAutoloaderTest.php @@ -20,7 +20,9 @@ */ namespace ZendTest\Loader; -use Zend\Loader\ResourceAutoloader; + +use Zend\Loader\ResourceAutoloader, + Zend\Config\Config; /** * @category Zend @@ -52,6 +54,7 @@ public function setUp() 'namespace' => 'FooBar', 'basePath' => realpath(__DIR__ . '/_files/ResourceAutoloader'), )); + $this->loader->register(); } public function tearDown() @@ -72,25 +75,25 @@ public function tearDown() public function testAutoloaderInstantiationShouldRaiseExceptionWithoutNamespace() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException'); $loader = new ResourceAutoloader(array('basePath' => __DIR__)); } public function testAutoloaderInstantiationShouldRaiseExceptionWithoutBasePath() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException'); $loader = new ResourceAutoloader(array('namespace' => 'Foo')); } public function testAutoloaderInstantiationShouldRaiseExceptionWhenInvalidOptionsTypeProvided() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException'); $loader = new ResourceAutoloader('foo'); } public function testAutoloaderConstructorShouldAcceptZendConfigObject() { - $config = new \Zend_Config(array('namespace' => 'Foo', 'basePath' => __DIR__)); + $config = new Config(array('namespace' => 'Foo', 'basePath' => __DIR__)); $loader = new ResourceAutoloader($config); } @@ -113,13 +116,13 @@ public function testNoResourceTypesShouldBeRegisteredByDefault() public function testInitialResourceTypeDefinitionShouldRequireNamespace() { - $this->setExpectedException('\\Zend\\Loader\\MissingResourceNamespaceException'); + $this->setExpectedException('Zend\Loader\Exception\MissingResourceNamespaceException'); $this->loader->addResourceType('foo', 'foo'); } public function testPassingNonStringPathWhenAddingResourceTypeShouldRaiseAnException() { - $this->setExpectedException('\\Zend\\Loader\\InvalidPathException'); + $this->setExpectedException('Zend\Loader\Exception\InvalidPathException'); $this->loader->addResourceType('foo', array('foo'), 'Foo'); } @@ -178,13 +181,13 @@ public function testAutoloaderShouldSupportAddingMultipleResourceTypesAtOnceUsin public function testAddingMultipleResourceTypesShouldRaiseExceptionWhenReceivingNonArrayItem() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException', 'expects an array'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException', 'expects an array'); $this->loader->addResourceTypes(array('foo' => 'bar')); } public function testAddingMultipleResourceTypesShouldRaiseExceptionWhenMissingResourcePath() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException', 'include a paths'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException', 'include a paths'); $this->loader->addResourceTypes(array('model' => array('namespace' => 'Model'))); } @@ -250,13 +253,13 @@ public function testSettingDefaultResourceTypeToUndefinedTypeShouldHaveNoEffect( public function testLoadShouldRaiseExceptionWhenNotTypePassedAndNoDefaultSpecified() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException', 'No resource type'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException', 'No resource type'); $this->loader->load('Foo'); } public function testLoadShouldRaiseExceptionWhenResourceTypeDoesNotExist() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException', 'Invalid resource type'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException', 'Invalid resource type'); $this->loader->load('Foo', 'model'); } @@ -278,6 +281,7 @@ public function testLoadShouldReturnObjectOfExpectedClassUsingPrefixes() $loader->addResourceTypes(array( 'model' => array('path' => 'models', 'namespace' => 'Model'), )); + $loader->register(); $object = $loader->load('ZendLoaderAutoloaderResourcePrefixTest', 'model'); $this->assertTrue($object instanceof \FooBar_Model_ZendLoaderAutoloaderResourcePrefixTest); } @@ -302,6 +306,7 @@ public function testAutoloadShouldAllowEmptyNamespacing() $loader->addResourceTypes(array( 'service' => array('path' => 'services', 'namespace' => 'Service'), )); + $loader->register(); $test = $loader->load('ZendLoaderAutoloaderResourceTest', 'service'); $this->assertTrue($test instanceof \Service\ZendLoaderAutoloaderResourceTest); } @@ -350,19 +355,19 @@ public function testPassingPrefixedClassWithUnmatchedResourceTypeToAutoloadShoul public function testMethodOverloadingShouldRaiseExceptionForNonGetterMethodCalls() { - $this->setExpectedException('\\Zend\\Loader\\BadMethodCallException'); + $this->setExpectedException('Zend\Loader\Exception\BadMethodCallException'); $this->loader->lalalalala(); } public function testMethodOverloadingShouldRaiseExceptionWhenRequestedResourceDoesNotExist() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException', 'Invalid resource'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException', 'Invalid resource'); $this->loader->getModel('Foo'); } public function testMethodOverloadingShouldRaiseExceptionWhenNoArgumentPassed() { - $this->setExpectedException('\\Zend\\Loader\\InvalidArgumentException', 'no resourc'); + $this->setExpectedException('Zend\Loader\Exception\InvalidArgumentException', 'no resourc'); $this->loader->addResourceTypes(array( 'model' => array('path' => 'models', 'namespace' => 'Model'), )); @@ -378,24 +383,6 @@ public function testMethodOverloadingShouldReturnObjectOfExpectedType() $this->assertTrue($test instanceof \FooBar\Model\ZendLoaderAutoloaderResourceMethodOverloading); } - /** - * @group ZF-7473 - */ - public function testAutoloaderShouldReceiveNamespaceWithTrailingUnderscore() - { - $this->loader = new ResourceAutoloader(array( - 'prefix' => 'FooBar', - 'basePath' => realpath(__DIR__ . '/_files/ResourceAutoloader'), - )); - $al = Autoloader::getInstance(); - $loaders = $al->getPrefixAutoloaders('FooBar'); - $this->assertTrue(empty($loaders)); - $loaders = $al->getPrefixAutoloaders('FooBar_'); - $this->assertFalse(empty($loaders)); - $loader = array_shift($loaders); - $this->assertSame($this->loader, $loader); - } - /** * @group ZF-7501 */ From c993e85c72d44009656c01aa4cf5641feed4fc75 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 2 May 2011 17:07:13 -0500 Subject: [PATCH 4/4] Fix parse error - Added in missing opening brace --- src/AutoloaderFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AutoloaderFactory.php b/src/AutoloaderFactory.php index b5ecef4..a29b096 100644 --- a/src/AutoloaderFactory.php +++ b/src/AutoloaderFactory.php @@ -87,7 +87,7 @@ public static function factory($options) foreach ($options as $class => $opts) { if (!class_exists($class)) { $autoloader = self::getStandardAutoloader(); - if (!class_exists($class) && !$autoloader->autoload($class)) + if (!class_exists($class) && !$autoloader->autoload($class)) { require_once 'Exception/InvalidArgumentException.php'; throw new Exception\InvalidArgumentException(sprintf('Autoloader class "%s" not loaded', $class)); }