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

Commit

Permalink
Merge branch 'develop' of https://github.com/zendframework/zf2 into h…
Browse files Browse the repository at this point in the history
…otfix/zend-session-global-session-storage
  • Loading branch information
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/ModuleAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ public function setOptions($options)
return $this;
}

/**
* Retrieves the class map for all loaded modules.
*
* @return array
*/
public function getModuleClassMap()
{
return $this->moduleClassMap;
}

/**
* Sets the class map used to speed up the module autoloading.
*
* @param array $classmap
* @return ModuleLoader
*/
public function setModuleClassMap(array $classmap)
{
$this->moduleClassMap = $classmap;

return $this;
}

/**
* Autoload a class
*
Expand All @@ -113,6 +136,11 @@ public function autoload($class)
return false;
}

if (isset($this->moduleClassMap[$class])) {
require_once $this->moduleClassMap[$class];
return $class;
}

$moduleName = substr($class, 0, -7);
if (isset($this->explicitPaths[$moduleName])) {
$classLoaded = $this->loadModuleFromDir($this->explicitPaths[$moduleName], $class);
Expand Down Expand Up @@ -295,7 +323,8 @@ public function unregister()
public function registerPaths($paths)
{
if (!is_array($paths) && !$paths instanceof Traversable) {
throw new \InvalidArgumentException(
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
throw new Exception\InvalidArgumentException(
'Parameter to \\Zend\\Loader\\ModuleAutoloader\'s '
. 'registerPaths method must be an array or '
. 'implement the \\Traversable interface'
Expand Down Expand Up @@ -324,7 +353,8 @@ public function registerPaths($paths)
public function registerPath($path, $moduleName = false)
{
if (!is_string($path)) {
throw new \InvalidArgumentException(sprintf(
require_once __DIR__ . '/Exception/InvalidArgumentException.php';
throw new Exception\InvalidArgumentException(sprintf(
'Invalid path provided; must be a string, received %s',
gettype($path)
));
Expand Down
14 changes: 13 additions & 1 deletion test/ModuleAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ public function testCanLoadModulesFromExplicitLocation()
$this->assertTrue(class_exists('PharModuleExplicit\Module'));
}

public function testCanLoadModulesFromClassMap()
{
$loader = new ModuleAutoloader();
$loader->setModuleClassMap(array(
'BarModule\Module' => __DIR__ . '/_files/BarModule/Module.php',
'PharModuleMap\Module' => __DIR__ . '/_files/PharModuleMap.phar',
));
$loader->register();

$this->assertTrue(class_exists('BarModule\Module'));
$this->assertTrue(class_exists('PharModuleMap\Module'));
}

public function testCanLoadModulesFromNamespace()
{
$loader = new ModuleAutoloader(array(
Expand All @@ -208,5 +221,4 @@ public function testCanLoadModulesFromNamespace()
$this->assertTrue(class_exists('FooModule\SubModule\Module'));
$this->assertTrue(class_exists('FooModule\Module'));
}

}
15 changes: 15 additions & 0 deletions test/_files/BarModule/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Loader
*/

namespace BarModule;

class Module
{
}
Binary file added test/_files/PharModuleMap.phar
Binary file not shown.
1 change: 1 addition & 0 deletions test/_files/_buildPhars.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Executable
// .phar
buildModulePhar('PharModule');
buildModulePhar('PharModuleMap');
// .phar.gz
buildModulePhar('PharModuleGz', Phar::PHAR, Phar::GZ);
// .phar.bz2
Expand Down

0 comments on commit 1262a33

Please sign in to comment.