This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
Adding logic to check module dependencies at module loading time #3443
Closed
Ocramius
wants to merge
2
commits into
zendframework:master
from
Ocramius:feature/module-dependencies
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
library/Zend/ModuleManager/Exception/MissingDependencyModuleException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_ModuleManager | ||
*/ | ||
|
||
namespace Zend\ModuleManager\Exception; | ||
|
||
/** | ||
* Runtime Exception | ||
* | ||
* @category Zend | ||
* @package Zend_ModuleManager | ||
* @subpackage Exception | ||
*/ | ||
class MissingDependencyModuleException extends RuntimeException implements ExceptionInterface | ||
{ | ||
} |
26 changes: 26 additions & 0 deletions
26
library/Zend/ModuleManager/Feature/DependencyIndicatorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_ModuleManager | ||
*/ | ||
|
||
namespace Zend\ModuleManager\Feature; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_ModuleManager | ||
* @subpackage Feature | ||
*/ | ||
interface DependencyIndicatorInterface | ||
{ | ||
/** | ||
* Expected to return an array of modules on which the current one depends on | ||
* | ||
* @return array | ||
*/ | ||
public function getDependencyModules(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
library/Zend/ModuleManager/Listener/ModuleDependencyCheckerListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_ModuleManager | ||
*/ | ||
|
||
namespace Zend\ModuleManager\Listener; | ||
|
||
use Zend\ModuleManager\Exception; | ||
use Zend\ModuleManager\Feature\DependencyIndicatorInterface; | ||
use Zend\ModuleManager\ModuleEvent; | ||
|
||
/** | ||
* Module resolver listener | ||
* | ||
* @category Zend | ||
* @package Zend_ModuleManager | ||
* @subpackage Listener | ||
*/ | ||
class ModuleDependencyCheckerListener | ||
{ | ||
/** | ||
* @var array of already loaded modules, indexed by module name | ||
*/ | ||
protected $loaded = array(); | ||
|
||
/** | ||
* @param \Zend\ModuleManager\ModuleEvent $e | ||
* | ||
* @throws Exception\MissingDependencyModuleException | ||
*/ | ||
public function __invoke(ModuleEvent $e) | ||
{ | ||
$module = $e->getModule(); | ||
|
||
if ($module instanceof DependencyIndicatorInterface || method_exists($module, 'getDependencyModules')) { | ||
$dependencies = $module->getDependencyModules(); | ||
|
||
foreach ($dependencies as $dependencyModule) { | ||
if (!isset($this->loaded[$dependencyModule])) { | ||
throw new Exception\MissingDependencyModuleException( | ||
sprintf( | ||
'Module "%s" depends on module "%s", which was not initialized before it', | ||
$e->getModuleName(), | ||
$dependencyModule | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
$this->loaded[$e->getModuleName()] = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
tests/ZendTest/ModuleManager/Listener/ModuleDependencyCheckerListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_ModuleManager | ||
*/ | ||
|
||
namespace ZendTest\ModuleManager\Listener; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\ModuleManager\Listener\ModuleDependencyCheckerListener; | ||
use Zend\ModuleManager\ModuleManager; | ||
|
||
class ModuleDependencyCheckerListenerTest extends TestCase | ||
{ | ||
/** | ||
* @covers \Zend\ModuleManager\Listener\ModuleDependencyCheckerListener::__invoke | ||
*/ | ||
public function testCallsGetDependencyModulesOnModuleImplementingInterface() | ||
{ | ||
//$moduleManager = new ModuleManager(array()); | ||
//$moduleManager->getEventManager()->attach('loadModule', new ModuleDependencyCheckerListener(), 2000); | ||
|
||
$module = $this->getMock('Zend\ModuleManager\Feature\DependencyIndicatorInterface'); | ||
$module->expects($this->once())->method('getDependencyModules')->will($this->returnValue(array())); | ||
|
||
$event = $this->getMock('Zend\ModuleManager\ModuleEvent'); | ||
$event->expects($this->any())->method('getModule')->will($this->returnValue($module)); | ||
|
||
$listener = new ModuleDependencyCheckerListener(); | ||
$listener->__invoke($event); | ||
} | ||
|
||
/** | ||
* @covers \Zend\ModuleManager\Listener\ModuleDependencyCheckerListener::__invoke | ||
*/ | ||
public function testCallsGetDependencyModulesOnModuleNotImplementingInterface() | ||
{ | ||
$module = $this->getMock('stdClass', array('getDependencyModules')); | ||
$module->expects($this->once())->method('getDependencyModules')->will($this->returnValue(array())); | ||
|
||
$event = $this->getMock('Zend\ModuleManager\ModuleEvent'); | ||
$event->expects($this->any())->method('getModule')->will($this->returnValue($module)); | ||
|
||
$listener = new ModuleDependencyCheckerListener(); | ||
$listener->__invoke($event); | ||
} | ||
|
||
/** | ||
* @covers \Zend\ModuleManager\Listener\ModuleDependencyCheckerListener::__invoke | ||
*/ | ||
public function testNotFulfilledDependencyThrowsException() | ||
{ | ||
$module = $this->getMock('stdClass', array('getDependencyModules')); | ||
$module->expects($this->once())->method('getDependencyModules')->will($this->returnValue(array('OtherModule'))); | ||
|
||
$event = $this->getMock('Zend\ModuleManager\ModuleEvent'); | ||
$event->expects($this->any())->method('getModule')->will($this->returnValue($module)); | ||
|
||
$listener = new ModuleDependencyCheckerListener(); | ||
$this->setExpectedException('Zend\ModuleManager\Exception\MissingDependencyModuleException'); | ||
$listener->__invoke($event); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhabs rename the method to getDependendModules() ? It would be more clear, what you get.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that would be the modules that depend on this one.
dependency
is correct