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

Commit

Permalink
Merge branch 'hotfix/72-change-order-of-resolver-lookups'
Browse files Browse the repository at this point in the history
Close #72
Fixes #73
  • Loading branch information
weierophinney committed Nov 1, 2017
2 parents 25c4bc2 + df3689d commit 710c133
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.8.1 - TBD
## 2.8.1 - 2017-11-01

### Added

- Nothing.

### Changed

- [#73](https://github.com/zendframework/zend-modulemanager/pull/73) modifies
the `ModuleResolverListener` slightly. In
[#5](https://github.com/zendframework/zend-modulemanager/pull/5),
released in 2.8.0, we added the ability to use classes named after the module
itself as a module class. However, in some specific cases, primarily when the
module is a top-level namespace, this can lead to conflicts with
globally-scoped classes. The patch in this release modifies the logic to first
check if a `Module` class exists under the module namespace, and will use
that; otherwise, it will then check if a class named after the namespace
exists. Additionally, the class now implements a blacklist of specific classes
known to be non-instantiable, including the `Generator` class shipped with the
PHP language itself.

### Deprecated

- Nothing.
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
},
"autoload-dev": {
"files": [
"test/autoload.php"
"test/autoload.php",
"test/TestAsset/ModuleAsClass.php"
],
"psr-4": {
"ListenerTestModule\\": "test/TestAsset/ListenerTestModule/",
"ModuleAsClass\\": "test/TestAsset/ModuleAsClass/",
"ZendTest\\ModuleManager\\": "test/"
}
},
Expand Down
5 changes: 5 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
<!-- Paths to check -->
<file>src</file>
<file>test</file>

<!-- Exclusions -->
<rule ref="PSR1.Classes.ClassDeclaration">
<exclude-pattern>*/test/TestAsset/ModuleAsClass.php</exclude-pattern>
</rule>
</ruleset>
25 changes: 18 additions & 7 deletions src/Listener/ModuleResolverListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@

namespace Zend\ModuleManager\Listener;

use Generator;
use Zend\ModuleManager\ModuleEvent;

/**
* Module resolver listener
*/
class ModuleResolverListener extends AbstractListener
{
/**
* Class names that are invalid as module classes, due to inability to instantiate.
*
* @var string[]
*/
protected $invalidClassNames = [
Generator::class,
];

/**
* @param ModuleEvent $e
* @return object|false False if module class does not exist
Expand All @@ -24,16 +34,17 @@ public function __invoke(ModuleEvent $e)
{
$moduleName = $e->getModuleName();

if (class_exists($moduleName)) {
return new $moduleName;
$class = sprintf('%s\Module', $moduleName);
if (class_exists($class)) {
return new $class;
}

$class = $moduleName . '\Module';

if (! class_exists($class)) {
return false;
if (class_exists($moduleName)
&& ! in_array($moduleName, $this->invalidClassNames, true)
) {
return new $moduleName;
}

return new $class;
return false;
}
}
19 changes: 19 additions & 0 deletions test/Listener/ModuleResolverListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\ModuleManager\Listener;

use ListenerTestModule;
use ModuleAsClass;
use Zend\ModuleManager\Listener\ModuleResolverListener;
use Zend\ModuleManager\ModuleEvent;

Expand Down Expand Up @@ -49,4 +50,22 @@ public function testModuleResolverListenerReturnFalseIfCannotResolveModuleClasse
$e->setModuleName('DoesNotExist');
$this->assertFalse($moduleResolver($e));
}

public function testModuleResolverListenerPrefersModuleClassesInModuleNamespaceOverNamedClasses()
{
$moduleResolver = new ModuleResolverListener;
$e = new ModuleEvent;

$e->setModuleName('ModuleAsClass');
$this->assertInstanceOf(ModuleAsClass\Module::class, $moduleResolver($e));
}

public function testModuleResolverListenerWillNotAttemptToResolveModuleAsClassNameGenerator()
{
$moduleResolver = new ModuleResolverListener;
$e = new ModuleEvent;

$e->setModuleName('Generator');
$this->assertFalse($moduleResolver($e));
}
}
10 changes: 10 additions & 0 deletions test/TestAsset/ModuleAsClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* @see https://github.com/zendframework/zend-modulemanager for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-modulemanager/blob/master/LICENSE.md New BSD License
*/

class ModuleAsClass
{
}
12 changes: 12 additions & 0 deletions test/TestAsset/ModuleAsClass/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @see https://github.com/zendframework/zend-modulemanager for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-modulemanager/blob/master/LICENSE.md New BSD License
*/

namespace ModuleAsClass;

class Module
{
}

0 comments on commit 710c133

Please sign in to comment.