Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/di' of github.com:ralphschindler/zf2 into featu…
Browse files Browse the repository at this point in the history
…re/rackspace
  • Loading branch information
ezimuel committed May 31, 2011
2 parents 4e037a4 + c993e85 commit b400e6b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 47 deletions.
3 changes: 2 additions & 1 deletion src/AutoloaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
36 changes: 22 additions & 14 deletions src/ResourceAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
}

/**
Expand All @@ -130,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);
}
Expand Down Expand Up @@ -268,7 +272,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)
{
Expand Down Expand Up @@ -370,14 +375,16 @@ 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)
{
$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);
Expand All @@ -386,7 +393,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, '\/');

Expand Down Expand Up @@ -451,16 +458,17 @@ protected function _addPrefixResource($type, $prefix)
* </code>
*
* @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;
Expand Down Expand Up @@ -568,18 +576,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()) {
Expand Down
51 changes: 19 additions & 32 deletions test/ResourceAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
*/

namespace ZendTest\Loader;
use Zend\Loader\ResourceAutoloader;

use Zend\Loader\ResourceAutoloader,
Zend\Config\Config;

/**
* @category Zend
Expand Down Expand Up @@ -52,6 +54,7 @@ public function setUp()
'namespace' => 'FooBar',
'basePath' => realpath(__DIR__ . '/_files/ResourceAutoloader'),
));
$this->loader->register();
}

public function tearDown()
Expand All @@ -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);
}

Expand All @@ -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');
}

Expand Down Expand Up @@ -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')));
}

Expand Down Expand Up @@ -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');
}

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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'),
));
Expand All @@ -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
*/
Expand Down

0 comments on commit b400e6b

Please sign in to comment.