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

Commit ae54c5f

Browse files
committed
Merge branch 'hotfix/console-usage' into develop
Close #81
2 parents 45d259c + a8c19bb commit ae54c5f

20 files changed

+301
-11
lines changed

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,21 @@ All notable changes to this project will be documented in this file, in reverse
5353

5454
### Removed
5555

56-
- Nothing.
56+
- `Zend\Mvc\Controller\AbstractController` no longer directly implements
57+
`Zend\ServiceManager\ServiceLocatorAwareInterface`, but still implements the
58+
methods defined in that interface. This was done to provide
59+
forwards-compatibility, as zend-servicemanager v3 no longer defines the
60+
interface. All initializers that do `ServiceLocatorInterface` injection were
61+
updated to also inject when just the methods are present.
5762

5863
### Fixed
5964

6065
- [#31](https://github.com/zendframework/zend-mvc/pull/31) and
6166
[#76](https://github.com/zendframework/zend-mvc/pull/76) update the component
6267
to be forwards-compatible with zend-eventmanager v3.
63-
- [#36](https://github.com/zendframework/zend-mvc/pull/36) and
64-
[#76](https://github.com/zendframework/zend-mvc/pull/76) update the component
68+
- [#36](https://github.com/zendframework/zend-mvc/pull/36),
69+
[#76](https://github.com/zendframework/zend-mvc/pull/76), and
70+
[#81](https://github.com/zendframework/zend-mvc/pull/81) update the component
6571
to be forwards-compatible with zend-servicemanager v3. Several changes were
6672
introduced to support this effort:
6773
- Added a `RouteInvokableFactory`, which can act as either a

src/Controller/AbstractController.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Zend\Http\Request as HttpRequest;
1818
use Zend\Mvc\InjectApplicationEventInterface;
1919
use Zend\Mvc\MvcEvent;
20+
use Zend\ServiceManager\ServiceLocatorInterface;
2021
use Zend\ServiceManager\ServiceManager;
2122
use Zend\Stdlib\DispatchableInterface as Dispatchable;
2223
use Zend\Stdlib\RequestInterface as Request;
@@ -62,6 +63,11 @@ abstract class AbstractController implements
6263
*/
6364
protected $response;
6465

66+
/**
67+
* @var ServiceLocatorInterface
68+
*/
69+
protected $serviceLocator;
70+
6571
/**
6672
* @var Event
6773
*/
@@ -224,6 +230,27 @@ public function getEvent()
224230
return $this->event;
225231
}
226232

233+
/**
234+
* Set serviceManager instance
235+
*
236+
* @param ServiceLocatorInterface $serviceLocator
237+
* @return void
238+
*/
239+
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
240+
{
241+
$this->serviceLocator = $serviceLocator;
242+
}
243+
244+
/**
245+
* Retrieve serviceManager instance
246+
*
247+
* @return ServiceLocatorInterface
248+
*/
249+
public function getServiceLocator()
250+
{
251+
return $this->serviceLocator;
252+
}
253+
227254
/**
228255
* Get plugin manager
229256
*

src/Controller/ControllerManager.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Zend\ServiceManager\AbstractPluginManager;
1717
use Zend\ServiceManager\ConfigInterface;
1818
use Zend\ServiceManager\Exception\InvalidServiceException;
19+
use Zend\ServiceManager\ServiceLocatorAwareInterface;
1920
use Zend\Stdlib\DispatchableInterface;
2021

2122
/**
@@ -54,6 +55,10 @@ public function __construct($configOrContainerInstance, array $v3config = [])
5455
$this->addInitializer([$this, 'injectConsole']);
5556
$this->addInitializer([$this, 'injectPluginManager']);
5657
parent::__construct($configOrContainerInstance, $v3config);
58+
59+
// Added after parent construction, as v2 abstract plugin managers add
60+
// one during construction.
61+
$this->addInitializer([$this, 'injectServiceLocator']);
5762
}
5863

5964
/**
@@ -191,4 +196,51 @@ public function injectPluginManager($first, $second)
191196

192197
$controller->setPluginManager($container->get('ControllerPluginManager'));
193198
}
199+
200+
/**
201+
* Initializer: inject service locator
202+
*
203+
* @param ContainerInterface|DispatchableInterface $first Container when
204+
* using zend-servicemanager v3; controller under v2.
205+
* @param DispatchableInterface|ContainerInterface $second Controller when
206+
* using zend-servicemanager v3; container under v2.
207+
*/
208+
public function injectServiceLocator($first, $second)
209+
{
210+
printf("In %s\n", __METHOD__);
211+
if ($first instanceof ContainerInterface) {
212+
$container = $first;
213+
$controller = $second;
214+
} else {
215+
$container = $second;
216+
$controller = $first;
217+
}
218+
219+
// For v2, we need to pull the parent service locator
220+
if (! method_exists($container, 'configure')) {
221+
$container = $container->getServiceLocator() ?: $container;
222+
}
223+
224+
if (! $controller instanceof ServiceLocatorAwareInterface
225+
&& method_exists($controller, 'setServiceLocator')
226+
) {
227+
trigger_error(sprintf(
228+
'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
229+
. 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove '
230+
. 'the implementation, and start injecting your dependencies via factory instead.',
231+
get_class($controller)
232+
), E_USER_DEPRECATED);
233+
$controller->setServiceLocator($container);
234+
}
235+
236+
if ($controller instanceof ServiceLocatorAwareInterface) {
237+
trigger_error(sprintf(
238+
'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
239+
. 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove '
240+
. 'the implementation, and start injecting your dependencies via factory instead.',
241+
get_class($controller)
242+
), E_USER_DEPRECATED);
243+
$controller->setServiceLocator($container);
244+
}
245+
}
194246
}

src/Router/Console/SimpleRouteStack.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ protected function init()
4242
Simple::class => RouteInvokableFactory::class,
4343

4444
// v2 normalized names
45-
'zendmvcrouterconsoleCatchall' => RouteInvokableFactory::class,
46-
'zendmvcrouterconsoleSimple' => RouteInvokableFactory::class,
45+
'zendmvcrouterconsolecatchall' => RouteInvokableFactory::class,
46+
'zendmvcrouterconsolesimple' => RouteInvokableFactory::class,
4747
],
4848
]))->configureServiceManager($this->routePluginManager);
4949
}
@@ -79,19 +79,21 @@ protected function routeFromArray($specs)
7979
{
8080
if ($specs instanceof Traversable) {
8181
$specs = ArrayUtils::iteratorToArray($specs);
82-
} elseif (!is_array($specs)) {
82+
}
83+
84+
if (! is_array($specs)) {
8385
throw new Exception\InvalidArgumentException('Route definition must be an array or Traversable object');
8486
}
8587

8688
// default to 'simple' console route
87-
if (!isset($specs['type'])) {
88-
$specs['type'] = 'simple';
89+
if (! isset($specs['type'])) {
90+
$specs['type'] = Simple::class;
8991
}
9092

9193
// build route object
9294
$route = parent::routeFromArray($specs);
9395

94-
if (!$route instanceof RouteInterface) {
96+
if (! $route instanceof RouteInterface) {
9597
throw new Exception\RuntimeException('Given route does not implement Console route interface');
9698
}
9799

src/Service/RouterFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ public function __invoke(ContainerInterface $container, $name, array $options =
4646
* For use with zend-servicemanager v2; proxies to __invoke().
4747
*
4848
* @param ServiceLocatorInterface $container
49+
* @param null|string $normalizedName
50+
* @param null|string $requestedName
4951
* @return RouteStackInterface
5052
*/
51-
public function createService(ServiceLocatorInterface $container)
53+
public function createService(ServiceLocatorInterface $container, $normalizedName = null, $requestedName = null)
5254
{
53-
return $this($container, RouteStackInterface::class);
55+
if ($normalizedName === 'router' && Console::isConsole()) {
56+
$requestedName = 'ConsoleRouter';
57+
}
58+
59+
return $this($container, $requestedName);
5460
}
5561
}

src/Service/ServiceListenerFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ServiceListenerFactory implements FactoryInterface
4040
'aliases' => [
4141
'Configuration' => 'config',
4242
'configuration' => 'config',
43+
'console' => 'ConsoleAdapter',
4344
'Console' => 'ConsoleAdapter',
4445
'ConsoleDefaultRenderingStrategy' => View\Console\DefaultRenderingStrategy::class,
4546
'ControllerLoader' => 'ControllerManager',

src/Service/ServiceManagerConfig.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ public function __construct(array $config = [])
141141
), E_USER_DEPRECATED);
142142
$instance->setServiceLocator($container);
143143
}
144+
145+
if (! $instance instanceof ServiceLocatorAwareInterface
146+
&& method_exists($instance, 'setServiceLocator')
147+
) {
148+
trigger_error(sprintf(
149+
'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
150+
. 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove '
151+
. 'the implementation, and start injecting your dependencies via factory instead.',
152+
get_class($instance)
153+
), E_USER_DEPRECATED);
154+
$instance->setServiceLocator($container);
155+
}
144156
},
145157
]);
146158

test/Application/AllowsReturningEarlyFromRoutingTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace ZendTest\Mvc\Application;
1111

12+
use PHPUnit_Framework_Error_Deprecated;
1213
use PHPUnit_Framework_TestCase as TestCase;
1314
use Zend\Http\PhpEnvironment\Response;
1415
use Zend\Mvc\MvcEvent;
@@ -17,6 +18,12 @@ class AllowsReturningEarlyFromRoutingTest extends TestCase
1718
{
1819
use PathControllerTrait;
1920

21+
public function setUp()
22+
{
23+
// Ignore deprecation errors
24+
PHPUnit_Framework_Error_Deprecated::$enabled = false;
25+
}
26+
2027
public function testAllowsReturningEarlyFromRouting()
2128
{
2229
$application = $this->prepareApplication();

test/Application/ControllerIsDispatchedTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99

1010
namespace ZendTest\Mvc\Application;
1111

12+
use PHPUnit_Framework_Error_Deprecated;
1213
use PHPUnit_Framework_TestCase as TestCase;
1314
use Zend\Mvc\MvcEvent;
1415

1516
class ControllerIsDispatchedTest extends TestCase
1617
{
1718
use PathControllerTrait;
1819

20+
public function setUp()
21+
{
22+
// Ignore deprecation errors
23+
PHPUnit_Framework_Error_Deprecated::$enabled = false;
24+
}
25+
1926
public function testControllerIsDispatchedDuringRun()
2027
{
2128
$application = $this->prepareApplication();

test/Application/ExceptionsRaisedInDispatchableShouldRaiseDispatchErrorEventTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99

1010
namespace ZendTest\Mvc\Application;
1111

12+
use PHPUnit_Framework_Error_Deprecated;
1213
use PHPUnit_Framework_TestCase as TestCase;
1314
use Zend\Mvc\MvcEvent;
1415

1516
class ExceptionsRaisedInDispatchableShouldRaiseDispatchErrorEventTest extends TestCase
1617
{
1718
use BadControllerTrait;
1819

20+
public function setUp()
21+
{
22+
// Ignore deprecation errors
23+
PHPUnit_Framework_Error_Deprecated::$enabled = false;
24+
}
25+
1926
/**
2027
* @group error-handling
2128
*/

0 commit comments

Comments
 (0)