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

Commit

Permalink
Merge branch 'hotfix/2867' into develop
Browse files Browse the repository at this point in the history
Forward port #2867
  • Loading branch information
weierophinney committed Oct 31, 2012
2 parents b9f4612 + ae07fef commit 86a4554
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
34 changes: 33 additions & 1 deletion library/Zend/Mvc/View/Http/InjectTemplateListener.php
Expand Up @@ -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
Expand Down Expand Up @@ -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 .= '/';
}
Expand Down Expand Up @@ -134,6 +147,25 @@ protected function deriveModuleNamespace($controller)
return $module;
}

/**
* @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
*
Expand Down
29 changes: 29 additions & 0 deletions tests/ZendTest/Mvc/View/InjectTemplateListenerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 86a4554

Please sign in to comment.