Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Refactor commit into two commits to better retain history #7

Merged
merged 7 commits into from

2 participants

Evan Coury Matthew Weier O'Phinney
Evan Coury

No description provided.

EvanDotPro and others added some commits
Evan Coury EvanDotPro Revert "Refactor module manager/loader under Zend namespace"
This reverts commit a4c7c34.

Re-doing as two commits to retain file move history.
d3c86c1
Evan Coury EvanDotPro Moving files for re-organization under Zend namespace
Omitting changes to files so git retains full history. Next commit will have
changes.
4461cb2
Evan Coury EvanDotPro Refactor module manager/loader under Zend namespace
- Zf2Module\ModuleLoader -> Zend\Loader\ModuleAutoloader
- Zf2Module\ModuleManager -> Zend\Module\Manager
- Zf2Module\ModuleManagerOptions -> Zend\Module\ManagerOptions
- Removed ModuleResolver interface
- Tweaked unit testing loader to include Zend namespace and updated the one lone
unit test that's in there (full suite coming soon, promise!)
3639d28
Matthew Weier O'Phinney Added "LocatorAware" interface
- If controller pulled from locator is "LocatorAware", inject the
  locator into the controller prior to dispatching it
c6f50cc
Evan Coury EvanDotPro Module manager now caches merged config per application environment e7a94ee
Evan Coury EvanDotPro Remove module loader getter/setter from module manager c24d158
Evan Coury EvanDotPro Eliminate extra loop through modules and pass Zend\Module\Manager to …
…module init()

Instead of looping through the modules first to run their init()'s, then again
to merge the configs, it now merges the configs during the initial loop through
the modules if the cache is off. Otherwise the cached config is loaded up front
and no loading or merging of module configs takes place.

Also, the per Matthew's suggestion, the Module Manager now passes itself to the
module's init() method to allow the module classes better access as to the state
of module loading, should they need it.
0685950
Matthew Weier O'Phinney weierophinney merged commit 0685950 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 20, 2011
  1. Evan Coury

    Revert "Refactor module manager/loader under Zend namespace"

    EvanDotPro authored
    This reverts commit a4c7c34.
    
    Re-doing as two commits to retain file move history.
  2. Evan Coury

    Moving files for re-organization under Zend namespace

    EvanDotPro authored
    Omitting changes to files so git retains full history. Next commit will have
    changes.
  3. Evan Coury

    Refactor module manager/loader under Zend namespace

    EvanDotPro authored
    - Zf2Module\ModuleLoader -> Zend\Loader\ModuleAutoloader
    - Zf2Module\ModuleManager -> Zend\Module\Manager
    - Zf2Module\ModuleManagerOptions -> Zend\Module\ManagerOptions
    - Removed ModuleResolver interface
    - Tweaked unit testing loader to include Zend namespace and updated the one lone
    unit test that's in there (full suite coming soon, promise!)
  4. Evan Coury

    Added "LocatorAware" interface

    authored EvanDotPro committed
    - If controller pulled from locator is "LocatorAware", inject the
      locator into the controller prior to dispatching it
  5. Evan Coury
  6. Evan Coury
Commits on Sep 21, 2011
  1. Evan Coury

    Eliminate extra loop through modules and pass Zend\Module\Manager to …

    EvanDotPro authored
    …module init()
    
    Instead of looping through the modules first to run their init()'s, then again
    to merge the configs, it now merges the configs during the initial loop through
    the modules if the cache is off. Otherwise the cached config is loaded up front
    and no loading or merging of module configs takes place.
    
    Also, the per Matthew's suggestion, the Module Manager now passes itself to the
    module's init() method to allow the module classes better access as to the state
    of module loading, should they need it.
This page is out of date. Refresh to see the latest.
123 modules/Zf2Module/src/Zend/Module/Manager.php
View
@@ -10,11 +10,6 @@
class Manager
{
/**
- * @var ModuleResolver
- */
- protected $loader;
-
- /**
* @var array An array of Module classes of loaded modules
*/
protected $loadedModules = array();
@@ -30,6 +25,16 @@ class Manager
protected $options;
/**
+ * @var Zend\Config\Config
+ */
+ protected $mergedConfig;
+
+ /**
+ * @var bool
+ */
+ protected $skipConfig = false;
+
+ /**
* __construct
*
* @param array|Traversable $modules
@@ -43,32 +48,13 @@ public function __construct($modules, ManagerOptions $options = null)
} else {
$this->setOptions($options);
}
- $this->loadModules($modules);
- }
-
- /**
- * getLoader
- *
- * @return ModuleResolver
- */
- public function getLoader()
- {
- if (!$this->loader instanceof ModuleResolver) {
- $this->setLoader(new ModuleLoader);
+ if ($this->hasCachedConfig()) {
+ $this->skipConfig = true;
+ $this->setMergedConfig($this->getCachedConfig());
}
- return $this->loader;
- }
-
- /**
- * setLoader
- *
- * @param ModuleResolver $loader
- * @return Manager
- */
- public function setLoader(ModuleResolver $loader)
- {
- $this->loader = $loader;
- return $this;
+ $this->loadModules($modules);
+ $this->updateCache();
+ $this->events()->trigger('init.post', $this);
}
/**
@@ -90,7 +76,6 @@ public function loadModules($modules)
. 'implement the \\Traversable interface'
);
}
- $this->events()->trigger('init.post', $this);
return $this;
}
@@ -105,9 +90,8 @@ public function loadModule($moduleName)
if (!isset($this->loadedModules[$moduleName])) {
$class = $moduleName . '\Module';
$module = new $class;
- if (is_callable(array($module, 'init'))) {
- $module->init($this->events());
- }
+ $this->runModuleInit($module);
+ $this->mergeModuleConfig($module);
$this->loadedModules[$moduleName] = $module;
}
return $this->loadedModules[$moduleName];
@@ -169,30 +153,59 @@ public function setOptions(ManagerOptions $options)
*
* @return Zend\Config\Config
*/
- public function getMergedConfig()
+ public function getMergedConfig($readOnly = true)
{
- if (($config = $this->getCachedConfig()) !== false) {
- return $config;
+ if (null === $this->mergedConfig) {
+ $this->setMergedConfig(new Config(array(), true));
}
- $config = new Config(array(), true);
- foreach ($this->loadedModules as $module) {
- if (is_callable(array($module, 'getConfig'))) {
- $config->merge($module->getConfig(defined('APPLICATION_ENV') ? APPLICATION_ENV : NULL));
- }
+ if (true === $readOnly) {
+ $this->mergedConfig->setReadOnly();
+ }
+ return $this->mergedConfig;
+ }
+
+ /**
+ * setMergedConfig
+ *
+ * @param Config $config
+ * @return Manager
+ */
+ public function setMergedConfig(Config $config)
+ {
+ $this->mergedConfig = $config;
+ return $this;
+ }
+
+ /**
+ * mergeModuleConfig
+ *
+ * @param mixed $module
+ * @return Manager
+ */
+ public function mergeModuleConfig($module)
+ {
+ if ((false === $this->skipConfig)
+ && (is_callable(array($module, 'getConfig')))
+ ) {
+ $this->getMergedConfig(false)->merge($module->getConfig($this->getOptions()->getApplicationEnv()));
}
- $config->setReadOnly();
- if ($this->getOptions()->getCacheConfig()) {
- $this->saveConfigCache($config);
+ return $this;
+ }
+
+ protected function runModuleInit($module)
+ {
+ if (is_callable(array($module, 'init'))) {
+ $module->init($this);
}
- return $config;
+ return $this;
}
protected function hasCachedConfig()
{
- if($this->getOptions()->getCacheConfig()) {
- if (file_exists($this->getOptions()->getCacheFilePath())) {
- return true;
- }
+ if (($this->getOptions()->getCacheConfig())
+ && (file_exists($this->getOptions()->getCacheFilePath()))
+ ) {
+ return true;
}
return false;
}
@@ -205,6 +218,16 @@ protected function getCachedConfig()
return false;
}
+ protected function updateCache()
+ {
+ if (($this->getOptions()->getCacheConfig())
+ && (false === $this->skipConfig)
+ ) {
+ $this->saveConfigCache($this->getMergedConfig());
+ }
+ return $this;
+ }
+
protected function saveConfigCache($config)
{
$content = "<?php\nreturn " . var_export($config->toArray(), 1) . ';';
10 modules/Zf2Module/src/Zend/Module/ManagerOptions.php
View
@@ -38,7 +38,7 @@ public function setCacheConfig($cacheConfig)
$this->cacheConfig = $cacheConfig;
return $this;
}
-
+
/**
* Get cacheDir.
*
@@ -71,7 +71,12 @@ public function setCacheDir($cacheDir)
*/
public function getCacheFilePath()
{
- return $this->getCacheDir() . '/merged-module-config.php';
+ return $this->getCacheDir() . '/module-config-cache.'.$this->getApplicationEnv().'.php';
+ }
+
+ public function getApplicationEnv()
+ {
+ return defined('APPLICATION_ENV') ? APPLICATION_ENV : NULL;
}
/**
@@ -162,4 +167,5 @@ public function __unset($key)
);
}
}
+
}
1  modules/Zf2Mvc/classmap.php
View
@@ -16,6 +16,7 @@
'Zf2Mvc\\Controller\\RestfulController' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Zf2Mvc' . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR . 'RestfulController.php',
'Zf2Mvc\\Application' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Zf2Mvc' . DIRECTORY_SEPARATOR . 'Application.php',
'Zf2Mvc\\MvcEvent' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Zf2Mvc' . DIRECTORY_SEPARATOR . 'MvcEvent.php',
+ 'Zf2Mvc\\LocatorAware' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Zf2Mvc' . DIRECTORY_SEPARATOR . 'LocatorAware.php',
'Zf2Mvc\\Exception' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Zf2Mvc' . DIRECTORY_SEPARATOR . 'Exception.php',
'Zf2Mvc\\PhpEnvironment\\GetContainer' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Zf2Mvc' . DIRECTORY_SEPARATOR . 'PhpEnvironment' . DIRECTORY_SEPARATOR . 'GetContainer.php',
'Zf2Mvc\\PhpEnvironment\\PostContainer' => __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Zf2Mvc' . DIRECTORY_SEPARATOR . 'PhpEnvironment' . DIRECTORY_SEPARATOR . 'PostContainer.php',
4 modules/Zf2Mvc/src/Zf2Mvc/Application.php
View
@@ -297,6 +297,10 @@ public function dispatch(MvcEvent $e)
goto complete;
}
+ if ($controller instanceof LocatorAware) {
+ $controller->setLocator($locator);
+ }
+
$request = $e->getRequest();
$response = $this->getResponse();
$event = clone $e;
11 modules/Zf2Mvc/src/Zf2Mvc/LocatorAware.php
View
@@ -0,0 +1,11 @@
+<?php
+
+namespace Zf2Mvc;
+
+use Zend\Di\Locator;
+
+interface LocatorAware
+{
+ public function setLocator(Locator $locator);
+ public function getLocator();
+}
47 modules/Zf2Mvc/tests/Zf2Mvc/ApplicationTest.php
View
@@ -2,16 +2,23 @@
namespace Zf2Mvc;
-use PHPUnit_Framework_TestCase as TestCase,
+use ArrayObject,
+ PHPUnit_Framework_TestCase as TestCase,
stdClass,
Zend\Di\DependencyInjector,
Zend\Di\ServiceLocator,
+ Zend\EventManager\StaticEventManager,
Zend\Http\Request,
Zend\Http\Response,
Zend\Uri\UriFactory;
class ApplicationTest extends TestCase
{
+ public function setUp()
+ {
+ StaticEventManager::resetInstance();
+ }
+
public function testEventManagerIsLazyLoaded()
{
$app = new Application();
@@ -318,4 +325,42 @@ public function testPostDispatchResultIsPassedByReferenceToEventListeners()
$this->assertTrue(isset($response->foo), var_export($response, 1));
$this->assertEquals('bar', $response->foo);
}
+
+ public function testDispatchingInjectsLocatorInLocatorAwareControllers()
+ {
+ $app = new Application();
+
+ $request = new Request();
+ $uri = UriFactory::factory('http://example.local/locator-aware');
+ $request->setUri($uri);
+ $app->setRequest($request);
+
+ $route = new Router\Http\Literal(array(
+ 'route' => '/locator-aware',
+ 'defaults' => array(
+ 'controller' => 'locator-aware',
+ ),
+ ));
+ $router = $app->getRouter();
+ $router->addRoute('locator-aware', $route);
+
+ $locator = new TestAsset\Locator();
+ $locator->add('locator-aware', function() {
+ return new TestAsset\LocatorAwareController;
+ });
+ $app->setLocator($locator);
+
+ $storage = new ArrayObject();
+ $events = StaticEventManager::getInstance();
+ $events->attach('Zf2Mvc\TestAsset\LocatorAwareController', 'dispatch.pre', function ($e) use ($storage) {
+ $controller = $e->getTarget();
+ $storage['locator'] = $controller->getLocator();
+ return $e->getResponse();
+ }, 100);
+
+ $app->run();
+
+ $this->assertTrue(isset($storage['locator']));
+ $this->assertSame($locator, $storage['locator']);
+ }
}
23 modules/Zf2Mvc/tests/Zf2Mvc/TestAsset/LocatorAwareController.php
View
@@ -0,0 +1,23 @@
+<?php
+
+namespace Zf2Mvc\TestAsset;
+
+use Zend\Di\Locator as DiLocator,
+ Zf2Mvc\Controller\ActionController,
+ Zf2Mvc\LocatorAware;
+
+class LocatorAwareController extends ActionController implements LocatorAware
+{
+ protected $locator;
+
+ public function setLocator(DiLocator $locator)
+ {
+ $this->locator = $locator;
+ return $this;
+ }
+
+ public function getLocator()
+ {
+ return $this->locator;
+ }
+}
Something went wrong with that request. Please try again.