Skip to content

Commit

Permalink
Merge branch 'hotfix/deprecations'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 2, 2016
2 parents a58183e + b13d197 commit c580db2
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 126 deletions.
22 changes: 20 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## 2.7.1 - TBD
## 2.7.1 - 2016-03-02

### Added

Expand All @@ -18,7 +18,25 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#88](https://github.com/zendframework/zend-mvc/pull/88) addresses backwards
compatibility concerns raised by users due to the new deprecation notices
emitted by `ServiceLocatorAware` initializers; in particular, all
`AbstractController` implementations were raising a deprecation wen first
pulled from the `ControllerManager`.

At this time, notices are now only raised in the following conditions:

- When a non-controller, non-plugin manager, `ServiceLocatorAware` instance
is detected.
- When a plugin manager instance is detected that is `ServiceLocatorAware` and
does not have a composed service locator. In this situation, the deprecation
notice indicates that the factory for the plugin manager should be updated
to inject the service locator via the constructor.
- For controllers that do not extend `AbstractController` but do implement
`ServiceLocatorAware`.
- When calling `getServiceLocator()` from within an `AbstractController`
extension; this properly calls out the practice that should be avoided and
which requires updates to the controller.

## 2.7.0 - 2016-03-01

Expand Down
9 changes: 9 additions & 0 deletions src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
*/
public function getServiceLocator()
{
trigger_error(sprintf(
'You are retrieving the service locator from within the class %s. Please be aware that '
. 'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
. 'with the ServiceLocatorAwareInitializer. You will need to update your class to accept '
. 'all dependencies at creation, either via constructor arguments or setters, and use '
. 'a factory to perform the injections.',
get_class($this)
), E_USER_DEPRECATED);

return $this->serviceLocator;
}

Expand Down
14 changes: 8 additions & 6 deletions src/Controller/ControllerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,20 @@ public function injectServiceLocator($first, $second)
$container = $container->getServiceLocator() ?: $container;
}

// Inject AbstractController extensions that are not ServiceLocatorAware
// with the service manager, but do not emit a deprecation notice. We'll
// emit it from AbstractController::getServiceLocator() instead.
if (! $controller instanceof ServiceLocatorAwareInterface
&& $controller instanceof AbstractController
&& method_exists($controller, 'setServiceLocator')
) {
trigger_error(sprintf(
'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
. 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove '
. 'the implementation, and start injecting your dependencies via factory instead.',
get_class($controller)
), E_USER_DEPRECATED);
// Do not emit deprecation notice in this case
$controller->setServiceLocator($container);
}

// If a controller implements ServiceLocatorAwareInterface explicitly, we
// inject, but emit a deprecation notice. Since AbstractController no longer
// explicitly does this, this will only affect userland controllers.
if ($controller instanceof ServiceLocatorAwareInterface) {
trigger_error(sprintf(
'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
Expand Down
20 changes: 15 additions & 5 deletions src/Service/ServiceManagerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Zend\EventManager\SharedEventManagerInterface;
use Zend\ModuleManager\Listener\ServiceListener;
use Zend\ModuleManager\ModuleManager;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceManager;
Expand Down Expand Up @@ -132,7 +133,12 @@ public function __construct(array $config = [])
$instance = $first;
}

if ($instance instanceof ServiceLocatorAwareInterface) {
// For service locator aware classes, inject the service
// locator, but emit a deprecation notice. Skip plugin manager
// implementations; they're dealt with later.
if ($instance instanceof ServiceLocatorAwareInterface
&& ! $instance instanceof AbstractPluginManager
) {
trigger_error(sprintf(
'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
. 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove '
Expand All @@ -142,13 +148,17 @@ public function __construct(array $config = [])
$instance->setServiceLocator($container);
}

if (! $instance instanceof ServiceLocatorAwareInterface
&& method_exists($instance, 'setServiceLocator')
// For service locator aware plugin managers that do not have
// the service locator already injected, inject it, but emit a
// deprecation notice.
if ($instance instanceof ServiceLocatorAwareInterface
&& $instance instanceof AbstractPluginManager
&& ! $instance->getServiceLocator()
) {
trigger_error(sprintf(
'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
. 'with the ServiceLocatorAwareInitializer. Please update your class %s to remove '
. 'the implementation, and start injecting your dependencies via factory instead.',
. 'with the ServiceLocatorAwareInitializer. Please update your %s plugin manager factory '
. 'to inject the parent service locator via the constructor.',
get_class($instance)
), E_USER_DEPRECATED);
$instance->setServiceLocator($container);
Expand Down
7 changes: 0 additions & 7 deletions test/Application/AllowsReturningEarlyFromRoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Mvc\Application;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\PhpEnvironment\Response;
use Zend\Mvc\MvcEvent;
Expand All @@ -18,12 +17,6 @@ class AllowsReturningEarlyFromRoutingTest extends TestCase
{
use PathControllerTrait;

public function setUp()
{
// Ignore deprecation errors
PHPUnit_Framework_Error_Deprecated::$enabled = false;
}

public function testAllowsReturningEarlyFromRouting()
{
$application = $this->prepareApplication();
Expand Down
7 changes: 0 additions & 7 deletions test/Application/ControllerIsDispatchedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@

namespace ZendTest\Mvc\Application;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Mvc\MvcEvent;

class ControllerIsDispatchedTest extends TestCase
{
use PathControllerTrait;

public function setUp()
{
// Ignore deprecation errors
PHPUnit_Framework_Error_Deprecated::$enabled = false;
}

public function testControllerIsDispatchedDuringRun()
{
$application = $this->prepareApplication();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@

namespace ZendTest\Mvc\Application;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Mvc\MvcEvent;

class ExceptionsRaisedInDispatchableShouldRaiseDispatchErrorEventTest extends TestCase
{
use BadControllerTrait;

public function setUp()
{
// Ignore deprecation errors
PHPUnit_Framework_Error_Deprecated::$enabled = false;
}

/**
* @group error-handling
*/
Expand Down
12 changes: 12 additions & 0 deletions test/Controller/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPUnit_Framework_TestCase as TestCase;
use ReflectionProperty;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* @covers \Zend\Mvc\Controller\AbstractController
Expand Down Expand Up @@ -104,4 +105,15 @@ public function testSetEventManagerWithDefaultIdentifiersIncludesImplementedInte

$this->controller->setEventManager($eventManager);
}

public function testRetrievingServiceLocatorRaisesDeprecationNotice()
{
$services = $this->prophesize(ServiceLocatorInterface::class)->reveal();

$controller = new TestAsset\SampleController();
$controller->setServiceLocator($services);

$this->setExpectedException('PHPUnit_Framework_Error_Deprecated', 'retrieving the service locator');
$controller->getServiceLocator();
}
}
15 changes: 0 additions & 15 deletions test/Controller/ControllerManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@

namespace ZendTest\Mvc\Controller;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use ReflectionClass;
use Zend\EventManager\EventManager;
use Zend\EventManager\SharedEventManager;
use Zend\Mvc\Controller\ControllerManager;
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\ServiceManager;
use Zend\Console\Adapter\Virtual as ConsoleAdapter;
use ZendTest\Mvc\Service\TestAsset\DuckTypedServiceLocatorAwareController;

class ControllerManagerTest extends TestCase
{
public function setUp()
{
// Disable deprecation notices
PHPUnit_Framework_Error_Deprecated::$enabled = false;

$this->sharedEvents = new SharedEventManager;
$this->events = $this->createEventManager($this->sharedEvents);
$this->consoleAdapter = new ConsoleAdapter();
Expand Down Expand Up @@ -151,13 +145,4 @@ public function testDoNotUsePeeringServiceManagers()
$this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
$this->controllers->get('EventManager');
}

public function testServiceLocatorAwareInitializerInjectsDuckTypedImplementations()
{
$this->controllers->setFactory(DuckTypedServiceLocatorAwareController::class, InvokableFactory::class);

$controller = $this->controllers->get(DuckTypedServiceLocatorAwareController::class);
$this->assertInstanceOf(DuckTypedServiceLocatorAwareController::class, $controller);
$this->assertSame($this->services, $controller->getServiceLocator());
}
}
4 changes: 0 additions & 4 deletions test/Controller/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Mvc\Controller;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\EventManager;
use Zend\EventManager\SharedEventManager;
Expand All @@ -22,9 +21,6 @@ class IntegrationTest extends TestCase
{
public function setUp()
{
// Ignore deprecation errors
PHPUnit_Framework_Error_Deprecated::$enabled = false;

$this->sharedEvents = new SharedEventManager();

$this->services = new ServiceManager();
Expand Down
4 changes: 0 additions & 4 deletions test/Controller/Plugin/ForwardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Mvc\Controller\Plugin;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use ReflectionClass;
use stdClass;
Expand Down Expand Up @@ -52,9 +51,6 @@ class ForwardTest extends TestCase

public function setUp()
{
// Ignore deprecation errors
PHPUnit_Framework_Error_Deprecated::$enabled = false;

$eventManager = $this->createEventManager(new SharedEventManager());
$mockApplication = $this->getMock('Zend\Mvc\ApplicationInterface');
$mockApplication->expects($this->any())->method('getEventManager')->will($this->returnValue($eventManager));
Expand Down
4 changes: 0 additions & 4 deletions test/Service/ControllerManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Mvc\Service;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\SharedEventManager;
use Zend\Mvc\Service\ControllerManagerFactory;
Expand All @@ -35,9 +34,6 @@ class ControllerManagerFactoryTest extends TestCase

public function setUp()
{
// Ignore deprecation errors
PHPUnit_Framework_Error_Deprecated::$enabled = false;

$loaderFactory = new ControllerManagerFactory();
$this->defaultServiceConfig = [
'aliases' => [
Expand Down
16 changes: 0 additions & 16 deletions test/Service/ServiceManagerConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Mvc\Service;

use PHPUnit_Framework_Error_Deprecated;
use PHPUnit_Framework_TestCase as TestCase;
use ReflectionClass;
use stdClass;
Expand Down Expand Up @@ -38,9 +37,6 @@ class ServiceManagerConfigTest extends TestCase
*/
protected function setUp()
{
// Disable deprecation notices
PHPUnit_Framework_Error_Deprecated::$enabled = false;

$this->config = new ServiceManagerConfig();
$this->services = new ServiceManager();
$this->config->configureServiceManager($this->services);
Expand Down Expand Up @@ -216,16 +212,4 @@ public function testEventManagerInitializerCanBeReplaced()

$serviceManager->get('EventManagerAware');
}

public function testServiceLocatorAwareInitializerInjectsDuckTypedImplementations()
{
$serviceManager = new ServiceManager();
(new ServiceManagerConfig(['factories' => [
TestAsset\DuckTypedServiceLocatorAware::class => InvokableFactory::class,
]]))->configureServiceManager($serviceManager);

$instance = $serviceManager->get(TestAsset\DuckTypedServiceLocatorAware::class);
$this->assertInstanceOf(TestAsset\DuckTypedServiceLocatorAware::class, $instance);
$this->assertSame($serviceManager, $instance->getServiceLocator());
}
}
27 changes: 0 additions & 27 deletions test/Service/TestAsset/DuckTypedServiceLocatorAware.php

This file was deleted.

22 changes: 0 additions & 22 deletions test/Service/TestAsset/DuckTypedServiceLocatorAwareController.php

This file was deleted.

0 comments on commit c580db2

Please sign in to comment.