Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'hotfix/2867'

Close #2867
  • Loading branch information...
commit 26b7cee0f0a3d35a4f9c5b5a5e5916163616ea99 2 parents 1743bb5 + e643107
Matthew Weier O'Phinney weierophinney authored
34 library/Zend/Mvc/View/Http/InjectTemplateListener.php
View
@@ -14,6 +14,7 @@
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Filter\Word\CamelCaseToDash as CamelCaseToDashFilter;
use Zend\Mvc\MvcEvent;
+use Zend\Mvc\ModuleRouteListener;
use Zend\View\Model\ModelInterface as ViewModel;
class InjectTemplateListener implements ListenerAggregateInterface
@@ -89,9 +90,21 @@ public function injectTemplate(MvcEvent $e)
}
$module = $this->deriveModuleNamespace($controller);
- $controller = $this->deriveControllerClass($controller);
+ if ($namespace = $routeMatch->getParam(ModuleRouteListener::MODULE_NAMESPACE)) {
+ $controllerSubNs = $this->deriveControllerSubNamespace($namespace);
+ if (!empty($controllerSubNs)) {
+ if (!empty($module)) {
+ $module .= '/' . $controllerSubNs;
+ } else {
+ $module = $controllerSubNs;
+ }
+ }
+ }
+
+ $controller = $this->deriveControllerClass($controller);
$template = $this->inflectName($module);
+
if (!empty($template)) {
$template .= '/';
}
@@ -135,6 +148,25 @@ protected function deriveModuleNamespace($controller)
}
/**
+ * @param $namespace
+ * @return string
+ */
+ protected function deriveControllerSubNamespace($namespace)
+ {
+ if (!strstr($namespace, '\\')) {
+ return '';
+ }
+ $nsArray = explode('\\', $namespace);
+
+ // Remove the first two elements representing the module and controller directory.
+ $subNsArray = array_slice($nsArray, 2);
+ if (empty($subNsArray)) {
+ return '';
+ }
+ return implode('/', $subNsArray);
+ }
+
+ /**
* Determine the name of the controller
*
* Strip the namespace, and the suffix "Controller" if present.
29 tests/ZendTest/Mvc/View/InjectTemplateListenerTest.php
View
@@ -12,6 +12,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\EventManager;
+use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\View\Http\InjectTemplateListener;
@@ -102,6 +103,34 @@ public function testBypassesTemplateInjectionIfResultViewModelAlreadyHasATemplat
$this->assertEquals('custom', $model->getTemplate());
}
+ public function testMapsSubNamespaceToSubDirectoryWithControllerFromRouteMatch()
+ {
+ $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'Aj\Controller\SweetAppleAcres\Reports');
+ $this->routeMatch->setParam('controller', 'CiderSales');
+ $this->routeMatch->setParam('action', 'PinkiePieRevenue');
+
+ $model = new ViewModel();
+ $this->event->setResult($model);
+ $this->listener->injectTemplate($this->event);
+
+ $this->assertEquals('sweet-apple-acres/reports/cider-sales/pinkie-pie-revenue', $model->getTemplate());
+ }
+
+ public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTarget()
+ {
+ $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'ZendTest\Mvc\Controller\TestAsset');
+ $this->routeMatch->setParam('action', 'test');
+
+ $myViewModel = new ViewModel();
+ $myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
+
+ $this->event->setTarget($myController);
+ $this->event->setResult($myViewModel);
+ $this->listener->injectTemplate($this->event);
+
+ $this->assertEquals('zend-test/controller/test-asset/sample/test', $myViewModel->getTemplate());
+ }
+
public function testAttachesListenerAtExpectedPriority()
{
$events = new EventManager();
Please sign in to comment.
Something went wrong with that request. Please try again.