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

Commit 3cda628

Browse files
bakura10weierophinney
authored andcommitted
Typehint against ContainerInterface and allow merge config
1 parent b0325dc commit 3cda628

14 files changed

+126
-63
lines changed

src/AbstractPluginManager.php

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

1010
namespace Zend\ServiceManager;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\Exception\InvalidServiceException;
1314

1415
/**
@@ -21,13 +22,13 @@ abstract class AbstractPluginManager extends ServiceManager implements PluginMan
2122
*
2223
* @var null|string
2324
*/
24-
In $instanceOf = null;
25+
protected $instanceOf = null;
2526

2627
/**
27-
* @param ServiceLocatorInterface $parentLocator
28-
* @param array $config
28+
* @param ContainerInterface $parentLocator
29+
* @param array $config
2930
*/
30-
public function __construct(ServiceLocatorInterface $parentLocator, array $config)
31+
public function __construct(ContainerInterface $parentLocator, array $config)
3132
{
3233
parent::__construct($config);
3334
$this->creationContext = $parentLocator;

src/Factory/DelegatorFactoryInterface.php

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

1010
namespace Zend\ServiceManager\Factory;
1111

12-
use Zend\ServiceManager\ServiceLocatorInterface;
12+
use Interop\Container\ContainerInterface;
1313

1414
/**
1515
* Delegator factory interface
@@ -19,11 +19,11 @@ interface DelegatorFactoryInterface
1919
/**
2020
* A factory that creates delegates of a given service
2121
*
22-
* @param ServiceLocatorInterface $serviceLocator
23-
* @param string $name
24-
* @param callable $callback
25-
* @param array $options
22+
* @param ContainerInterface $container
23+
* @param string $name
24+
* @param callable $callback
25+
* @param array $options
2626
* @return object
2727
*/
28-
public function __invoke(ServiceLocatorInterface $serviceLocator, $name, callable $callback, array $options = []);
28+
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = []);
2929
}

src/Factory/FactoryInterface.php

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

1010
namespace Zend\ServiceManager\Factory;
1111

12-
use Zend\ServiceManager\ServiceLocatorInterface;
12+
use Interop\Container\ContainerInterface;
1313

1414
/**
1515
* Interface for a factory
@@ -22,10 +22,10 @@ interface FactoryInterface
2222
/**
2323
* Create an object
2424
*
25-
* @param ServiceLocatorInterface $serviceLocator
26-
* @param string $requestedName
27-
* @param array $options
25+
* @param ContainerInterface $container
26+
* @param string $requestedName
27+
* @param array $options
2828
* @return object
2929
*/
30-
public function __invoke(ServiceLocatorInterface $serviceLocator, $requestedName, array $options = []);
30+
public function __invoke(ContainerInterface $container, $requestedName, array $options = []);
3131
}

src/Factory/InvokableFactory.php

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

1010
namespace Zend\ServiceManager\Factory;
1111

12-
use Zend\ServiceManager\ServiceLocatorInterface;
12+
use Interop\Container\ContainerInterface;
1313

1414
/**
1515
* Factory to create newable classes
@@ -19,7 +19,7 @@ final class InvokableFactory implements FactoryInterface
1919
/**
2020
* {@inheritDoc}
2121
*/
22-
public function __invoke(ServiceLocatorInterface $serviceLocator, $requestedName, array $options = [])
22+
public function __invoke(ContainerInterface $container, $requestedName, array $options = [])
2323
{
2424
return new $requestedName($options);
2525
}

src/Factory/LazyServiceFactoryFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
namespace Zend\ServiceManager\Factory;
1111

12+
use Interop\Container\ContainerInterface;
1213
use ProxyManager\Configuration as ProxyConfiguration;
1314
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
1415
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
1516
use Zend\ServiceManager\Exception;
1617
use Zend\ServiceManager\Proxy\LazyServiceFactory;
17-
use Zend\ServiceManager\ServiceLocatorInterface;
1818

1919
/**
2020
* Factory to create a lazy factory
@@ -24,9 +24,9 @@ class LazyServiceFactoryFactory implements FactoryInterface
2424
/**
2525
* {@inheritDoc}
2626
*/
27-
public function __invoke(ServiceLocatorInterface $serviceLocator, $requestedName, array $options = [])
27+
public function __invoke(ContainerInterface $container, $requestedName, array $options = [])
2828
{
29-
$config = $serviceLocator->get('Config');
29+
$config = $container->get('config');
3030

3131
if (!isset($config['lazy_services'])) {
3232
throw new Exception\InvalidArgumentException('Missing "lazy_services" config key');

src/Initializer/InitializerInterface.php

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

1010
namespace Zend\ServiceManager\Initializer;
1111

12-
use Zend\ServiceManager\ServiceLocatorInterface;
12+
use Interop\Container\ContainerInterface;
1313

1414
/**
1515
* Interface for an initializer
@@ -22,9 +22,9 @@ interface InitializerInterface
2222
/**
2323
* Initialize the given instance
2424
*
25-
* @param ServiceLocatorInterface $serviceLocator
26-
* @param object $instance
25+
* @param ContainerInterface $container
26+
* @param object $instance
2727
* @return void
2828
*/
29-
public function __invoke(ServiceLocatorInterface $serviceLocator, $instance);
29+
public function __invoke(ContainerInterface $container, $instance);
3030
}

src/ServiceLocatorInterface.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,22 @@
99

1010
namespace Zend\ServiceManager;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\Exception;
1314

1415
/**
1516
* Interface for service locator
1617
*/
17-
interface ServiceLocatorInterface
18+
interface ServiceLocatorInterface extends ContainerInterface
1819
{
1920
/**
20-
* Retrieve a service by its name, with optional options
21+
* Build a service by its name, using optional options (such services are NEVER cached)
2122
*
2223
* @param string $name
2324
* @param array $options
2425
* @return object
2526
* @throws Exception\ServiceNotFoundException If no factory/abstract factory could be found to create the instance
2627
* @throws Exception\ServiceNotCreatedException If factory/delegator fails to create the instance
2728
*/
28-
public function get($name, array $options = []);
29-
30-
/**
31-
* Check if the service locator has a registered service for the given name
32-
*
33-
* @param string $name
34-
* @return bool
35-
*/
36-
public function has($name);
29+
public function build($name, array $options = []);
3730
}

src/ServiceManager.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* Service Manager
2323
*/
24-
class ServiceManager implements ServiceLocatorInterface, ContainerInterface
24+
class ServiceManager implements ServiceLocatorInterface
2525
{
2626
/**
2727
* A list of factories (either as string name or callable)
@@ -83,7 +83,7 @@ class ServiceManager implements ServiceLocatorInterface, ContainerInterface
8383
protected $shared = [];
8484

8585
/**
86-
* @var ServiceLocatorInterface
86+
* @var ContainerInterface
8787
*/
8888
protected $creationContext;
8989

@@ -96,6 +96,24 @@ public function __construct(array $config)
9696
$this->configure($config);
9797
}
9898

99+
/**
100+
* Create a new service locator with config's merged
101+
*
102+
* Note that the original service locator is left untouched (as with PSR-7 interfaces)
103+
*
104+
* @param array $config
105+
* @return ContainerInterface
106+
*/
107+
public function withConfig(array $config)
108+
{
109+
$container = clone $this;
110+
$container->creationContext = $container;
111+
112+
$container->configure($config);
113+
114+
return $container;
115+
}
116+
99117
/**
100118
* {@inheritDoc}
101119
*
@@ -139,6 +157,11 @@ public function get($name, array $options = [])
139157
return $object;
140158
}
141159

160+
public function build($name, array $options = [])
161+
{
162+
163+
}
164+
142165
/**
143166
* {@inheritDoc}
144167
*/

test/AbstractPluginManagerTest.php

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

1010
namespace ZendTest\ServiceManager;
1111

12+
use Interop\Container\ContainerInterface;
1213
use stdClass;
1314
use Zend\ServiceManager\Exception\InvalidServiceException;
1415
use Zend\ServiceManager\Factory\FactoryInterface;
1516
use Zend\ServiceManager\Factory\InvokableFactory;
16-
use Zend\ServiceManager\ServiceLocatorInterface;
1717
use ZendTest\ServiceManager\TestAsset\InvokableObject;
1818
use ZendTest\ServiceManager\TestAsset\SimplePluginManager;
1919

@@ -29,12 +29,12 @@ public function testInjectCreationContextInFactories()
2929
]
3030
];
3131

32-
$serviceLocator = $this->getMock(ServiceLocatorInterface::class);
33-
$pluginManager = new SimplePluginManager($serviceLocator, $config);
32+
$container = $this->getMock(ContainerInterface::class);
33+
$pluginManager = new SimplePluginManager($container, $config);
3434

3535
$invokableFactory->expects($this->once())
3636
->method('__invoke')
37-
->with($serviceLocator, InvokableObject::class)
37+
->with($container, InvokableObject::class)
3838
->will($this->returnValue(new InvokableObject()));
3939

4040
$object = $pluginManager->get(InvokableObject::class);
@@ -51,8 +51,8 @@ public function testValidateInstance()
5151
]
5252
];
5353

54-
$serviceLocator = $this->getMock(ServiceLocatorInterface::class);
55-
$pluginManager = new SimplePluginManager($serviceLocator, $config);
54+
$container = $this->getMock(ContainerInterface::class);
55+
$pluginManager = new SimplePluginManager($container, $config);
5656

5757
// Assert no exception is triggered because the plugin manager validate ObjectWithOptions
5858
$pluginManager->get(InvokableObject::class);

test/Factory/InvokableFactoryTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99

1010
namespace ZendTest\ServiceManager\Factory;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\Factory\InvokableFactory;
13-
use Zend\ServiceManager\ServiceLocatorInterface;
1414
use ZendTest\ServiceManager\TestAsset\InvokableObject;
1515

1616
class InvokableFactoryTest extends \PHPUnit_Framework_TestCase
1717
{
1818
public function testCanCreateObject()
1919
{
20-
$serviceLocator = $this->getMock(ServiceLocatorInterface::class);
21-
$factory = new InvokableFactory();
20+
$container = $this->getMock(ContainerInterface::class);
21+
$factory = new InvokableFactory();
2222

23-
$object = $factory($serviceLocator, InvokableObject::class, ['foo' => 'bar']);
23+
$object = $factory($container, InvokableObject::class, ['foo' => 'bar']);
2424

2525
$this->assertInstanceOf(InvokableObject::class, $object);
2626
$this->assertEquals(['foo' => 'bar'], $object->options);

0 commit comments

Comments
 (0)