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

Commit c214402

Browse files
committed
Merge branch 'hotfix/view-manager-delegator'
Added ViewManager delegator factory.
2 parents 0d727b2 + 503c15b commit c214402

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

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

5-
## 1.1.7 - TBD
5+
## 1.1.7 - 2016-05-24
66

77
### Added
88

9-
- Nothing.
9+
- Adds `Zend\Mvc\Console\Service\ViewManagerDelegatorFactory`, which listens for
10+
the `ViewManager` service and, if in a console environment, returns the
11+
`ConsoleViewManager` service instead.
1012

1113
### Deprecated
1214

src/ConfigProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function getDependencyConfig()
4747
'Router' => [ Router\ConsoleRouterDelegatorFactory::class ],
4848
SendResponseListener::class => [ Service\ConsoleResponseSenderDelegatorFactory::class ],
4949
'ViewHelperManager' => [ Service\ConsoleViewHelperManagerDelegatorFactory::class ],
50+
'ViewManager' => [ Service\ViewManagerDelegatorFactory::class ],
5051
],
5152
'factories' => [
5253
'ConsoleAdapter' => Service\ConsoleAdapterFactory::class,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-mvc-console for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace Zend\Mvc\Console\Service;
9+
10+
use Interop\Container\ContainerInterface;
11+
use Zend\Console\Console;
12+
use Zend\ServiceManager\DelegatorFactoryInterface;
13+
use Zend\ServiceManager\ServiceLocatorInterface;
14+
15+
class ViewManagerDelegatorFactory implements DelegatorFactoryInterface
16+
{
17+
/**
18+
* Return a ConsoleViewManager if in a Console environment.
19+
*
20+
* @param ContainerInterface $container
21+
* @param string $name
22+
* @param callable $callback
23+
* @param null|array $options
24+
* @return \Zend\Mvc\Console\View\ViewManager|Zend\Mvc\View\Http\ViewManager
25+
*/
26+
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
27+
{
28+
if (! Console::isConsole() || ! $container->has('ConsoleViewManager')) {
29+
return $callback();
30+
}
31+
32+
return $container->get('ConsoleViewManager');
33+
}
34+
35+
/**
36+
* Return a ConsoleViewManager if in a Console environment. (v2)
37+
*
38+
* @param ServiceLocatorInterface $container
39+
* @param string $name
40+
* @param string $requestedName
41+
* @param callable $callback
42+
* @return \Zend\Mvc\Console\View\ViewManager|Zend\Mvc\View\Http\ViewManager
43+
*/
44+
public function createDelegatorWithName(ServiceLocatorInterface $container, $name, $requestedName, $callback)
45+
{
46+
return $this($container, $requestedName, $callback);
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-mvc-console for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\Mvc\Console\Service;
9+
10+
use Interop\Container\ContainerInterface;
11+
use PHPUnit_Framework_TestCase as TestCase;
12+
use Zend\Mvc\Console\Service\ViewManagerDelegatorFactory;
13+
14+
class ViewManagerDelegatorFactoryTest extends TestCase
15+
{
16+
use FactoryEnvironmentTrait;
17+
18+
public function setUp()
19+
{
20+
$this->factory = new ViewManagerDelegatorFactory();
21+
}
22+
23+
public function testReturnsReturnValueOfCallbackWhenNotInConsoleEnvironment()
24+
{
25+
$this->setConsoleEnvironment(false);
26+
27+
$callback = function () {
28+
return 'FOO';
29+
};
30+
31+
$this->assertSame(
32+
$callback(),
33+
$this->factory->__invoke($this->createContainer(), 'ViewManager', $callback)
34+
);
35+
}
36+
37+
public function testReturnsConsoleViewManagerWhenInConsoleEnvironment()
38+
{
39+
$this->setConsoleEnvironment(true);
40+
41+
$viewManager = (object) ['view' => true];
42+
$container = $this->prophesize(ContainerInterface::class);
43+
$container->has('ConsoleViewManager')->willReturn(true);
44+
$container->get('ConsoleViewManager')->willReturn($viewManager);
45+
46+
$callback = function () {
47+
return 'FOO';
48+
};
49+
50+
$result = $this->factory->__invoke($container->reveal(), 'ViewManager', $callback);
51+
$this->assertSame($viewManager, $result);
52+
}
53+
}

0 commit comments

Comments
 (0)