Skip to content

Commit c19debe

Browse files
authored
expose symfony services directly from the zf service locator (#5)
Before it was required to first get the symfony container to access the services. Now, you can directly extract it from the zend service locator which will proxy to the symfony container service
1 parent 2e85476 commit c19debe

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ return [
5252

5353
# Usage
5454

55-
To retrieve the symfony container a zend controller:
55+
Any existing service will directly be available through the Service Manager of Zend:
56+
57+
```php
58+
<?php
59+
60+
$service = $this->getServiceLocator()->get(\My\Public\Service::class);
61+
```
62+
63+
But you can also retrieve the symfony container:
64+
5665

5766
```php
5867
<?php

config/module.config.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12+
use Adlogix\ZfSymfonyContainer\Service\Factory\SymfonyContainerAbstractFactory;
1213
use Adlogix\ZfSymfonyContainer\Service\Factory\SymfonyContainerConfigFactory;
1314
use Adlogix\ZfSymfonyContainer\Service\Factory\SymfonyContainerFactory;
1415

@@ -43,8 +44,12 @@
4344

4445
'service_manager' => [
4546

47+
'abstract_factories' => [
48+
SymfonyContainerAbstractFactory::class
49+
],
50+
4651
'factories' => [
47-
'zf_symfony_container' => SymfonyContainerFactory::class,
52+
'zf_symfony_container' => SymfonyContainerFactory::class,
4853
'zf_symfony_container_config' => SymfonyContainerConfigFactory::class,
4954
]
5055

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/*
3+
* This file is part of the Adlogix package.
4+
*
5+
* (c) Allan Segebarth <[email protected]>
6+
* (c) Jean-Jacques Courtens <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Adlogix\ZfSymfonyContainer\Service\Factory;
13+
14+
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
use Zend\ServiceManager\AbstractFactoryInterface;
17+
use Zend\ServiceManager\ServiceLocatorInterface;
18+
19+
/**
20+
* @author Toni Van de Voorde <[email protected]>
21+
*/
22+
final class SymfonyContainerAbstractFactory implements AbstractFactoryInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
28+
{
29+
/** @var ContainerInterface $sfContainer */
30+
$sfContainer = $serviceLocator->get('zf_symfony_container');
31+
32+
return $sfContainer->has($requestedName);
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
39+
{
40+
/** @var ContainerInterface $sfContainer */
41+
$sfContainer = $serviceLocator->get('zf_symfony_container');
42+
43+
return $sfContainer->get($requestedName);
44+
}
45+
}

tests/Bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* file that was distributed with this source code.
1010
*/
1111

12+
chdir(dirname(__DIR__));
13+
1214
ini_set('error_reporting', E_ALL);
1315

1416
$files = [__DIR__ . '/../vendor/autoload.php'];
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* This file is part of the Adlogix package.
4+
*
5+
* (c) Allan Segebarth <[email protected]>
6+
* (c) Jean-Jacques Courtens <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Adlogix\ZfSymfonyContainer\Test\Service\Factory;
13+
14+
15+
use Adlogix\ZfSymfonyContainer\Test\Fixtures\DummyTwo;
16+
use Adlogix\ZfSymfonyContainer\Test\Util\ServiceManagerFactory;
17+
use PHPUnit\Framework\TestCase;
18+
19+
final class SymfonyContainerAbstractFactoryTest extends TestCase
20+
{
21+
/**
22+
* @test
23+
*/
24+
public function getService_directlyThroughServiceLocator_shouldReturnInstance()
25+
{
26+
$dummyTwo = ServiceManagerFactory::getServiceManager()
27+
->get(DummyTwo::class);
28+
29+
$this->assertInstanceOf(DummyTwo::class, $dummyTwo);
30+
}
31+
}

0 commit comments

Comments
 (0)