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

Better ServiceLocatorAware deprecation #88

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.