Skip to content

Commit d9adffa

Browse files
committed
PHP-DI 4.0 compatibility
1 parent fc6b354 commit d9adffa

File tree

8 files changed

+44
-55
lines changed

8 files changed

+44
-55
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ To use PHP-DI in your ZF1 application, you need to change the Dispatcher used by
2929
* Initialize the dependency injection container
3030
*/
3131
protected function _initDependencyInjection()
32-
{
33-
$container = new \DI\Container();
32+
{
33+
$builder = new ContainerBuilder();
34+
// Configure your container here
35+
$container = $builder->build();
3436

3537
$dispatcher = new \DI\ZendFramework1\Dispatcher();
3638
$dispatcher->setContainer($container);
3739

3840
$frontController = Zend_Controller_Front::getInstance();
3941
$frontController->setDispatcher($dispatcher);
40-
}
42+
}
4143
```
4244

4345
That's it!
@@ -49,8 +51,6 @@ Now you can inject dependencies in your controllers!
4951
For example, here is the GuestbookController of the quickstart:
5052

5153
```php
52-
use DI\Annotation\Inject;
53-
5454
class GuestbookController extends Zend_Controller_Action
5555
{
5656
/**
@@ -97,6 +97,7 @@ how the ZF quickstart is ;)
9797

9898
## Change log
9999

100-
* 1.1.0 Require PHP-DI >= 3.3
100+
* 2.0.0 Requires PHP-DI >= 4.0.0
101+
* 1.1.0 Requires PHP-DI >= 3.3
101102
* 1.0.1 Bugfix
102103
* 1.0.0 First release

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"name": "mnapoli/php-di-zf1",
33
"type": "library",
44
"description": "Integrates PHP-DI to Zend Framework 1",
5-
"homepage": "http://mnapoli.github.io/PHP-DI-ZF1/",
5+
"homepage": "https://github.com/mnapoli/PHP-DI-ZF1",
66
"license": "MIT",
77
"autoload": {
88
"psr-0": {
9-
"DI\\ZendFramework1": "src/"
9+
"DI\\Bridge\\ZendFramework1\\": "src/"
1010
}
1111
},
12-
"require": {
13-
"mnapoli/php-di": "~3.3",
14-
"zendframework/zendframework1": "1.12.*"
15-
}
12+
"require": {
13+
"mnapoli/php-di": "~4.0",
14+
"zendframework/zendframework1": "1.12.*"
15+
}
1616
}

quickstart/application/Bootstrap.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use DI\ZendFramework1\Dispatcher;
3+
use DI\Bridge\ZendFramework1\Dispatcher;
44

55
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
66
{
@@ -15,14 +15,13 @@ protected function _initDoctype()
1515
* Initialize the dependency injection container
1616
*/
1717
protected function _initDependencyInjection()
18-
{
19-
$container = new \DI\Container();
18+
{
19+
$container = \DI\ContainerBuilder::buildDevContainer();
2020

2121
$dispatcher = new Dispatcher();
2222
$dispatcher->setContainer($container);
2323

2424
$frontController = Zend_Controller_Front::getInstance();
2525
$frontController->setDispatcher($dispatcher);
26-
}
26+
}
2727
}
28-

quickstart/application/controllers/GuestbookController.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<?php
22

3-
use DI\Annotation\Inject;
4-
53
class GuestbookController extends Zend_Controller_Action
64
{
7-
85
/**
96
* This dependency will be injected by PHP-DI
107
* @Inject
118
* @var Application_Service_GuestbookService
129
*/
1310
private $guestbookService;
1411

15-
1612
public function indexAction()
1713
{
1814
$this->view->entries = $this->guestbookService->getAllEntries();
@@ -30,5 +26,4 @@ public function signAction()
3026

3127
$this->view->form = $form;
3228
}
33-
3429
}
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
<?php
22

3-
use DI\Annotation\Inject;
4-
53
class Application_Service_GuestbookService
64
{
5+
/**
6+
* @Inject
7+
* @var Application_Model_GuestbookMapper
8+
*/
9+
private $guestbookMapper;
710

8-
/**
9-
* @Inject
10-
* @var Application_Model_GuestbookMapper
11-
*/
12-
private $guestbookMapper;
13-
14-
public function getAllEntries()
15-
{
16-
return $this->guestbookMapper->fetchAll();
17-
}
18-
19-
public function addEntry($fields)
20-
{
21-
$comment = new Application_Model_Guestbook($fields);
22-
$this->guestbookMapper->save($comment);
23-
}
11+
public function getAllEntries()
12+
{
13+
return $this->guestbookMapper->fetchAll();
14+
}
2415

16+
public function addEntry($fields)
17+
{
18+
$comment = new Application_Model_Guestbook($fields);
19+
$this->guestbookMapper->save($comment);
20+
}
2521
}

src/DI/ZendFramework1/Dispatcher.php renamed to src/DI/Bridge/ZendFramework1/Dispatcher.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
/**
33
* PHP-DI
44
*
5-
* @link http://mnapoli.github.io/PHP-DI/
5+
* @link http://php-di.org/
66
* @copyright Matthieu Napoli (http://mnapoli.fr/)
77
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
88
*/
99

10-
namespace DI\ZendFramework1;
10+
namespace DI\Bridge\ZendFramework1;
1111

12-
use DI\Container;
12+
use DI\ContainerInterface;
1313
use Exception;
1414
use Zend_Controller_Action;
1515
use Zend_Controller_Action_Interface;
@@ -27,9 +27,8 @@
2727
*/
2828
class Dispatcher extends Zend_Controller_Dispatcher_Standard
2929
{
30-
3130
/**
32-
* @var Container
31+
* @var ContainerInterface
3332
*/
3433
private $container;
3534

@@ -135,19 +134,18 @@ public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Control
135134
}
136135

137136
/**
138-
* @return Container
137+
* @return ContainerInterface
139138
*/
140139
public function getContainer()
141140
{
142141
return $this->container;
143142
}
144143

145144
/**
146-
* @param Container $container
145+
* @param ContainerInterface $container
147146
*/
148-
public function setContainer(Container $container)
147+
public function setContainer(ContainerInterface $container)
149148
{
150149
$this->container = $container;
151150
}
152-
153151
}

tests/UnitTests/DI/ZendFramework1/DispatcherTest.php

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

1010
namespace UnitTests\DI;
1111

12-
use DI\Container;
13-
use DI\ZendFramework1\Dispatcher;
12+
use DI\ContainerBuilder;
13+
use DI\Bridge\ZendFramework1\Dispatcher;
1414

1515
/**
1616
* Test class for Dispatcher
@@ -34,10 +34,10 @@ public function testDispatch()
3434

3535
$valueDependency = new \stdClass();
3636

37-
$container = new Container();
37+
$container = ContainerBuilder::buildDevContainer();
3838
$container->set("SomeName", $valueDependency);
3939

40-
$dispatcher->setContainer( $container );
40+
$dispatcher->setContainer($container);
4141

4242

4343
$request = new \Zend_Controller_Request_Simple();
@@ -50,4 +50,4 @@ public function testDispatch()
5050

5151
$this->assertEquals("a value", $valueDependency->somedata);
5252
}
53-
}
53+
}

tests/UnitTests/DI/ZendFramework1/Fixtures/MockController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ public function methodAction()
1414
{
1515
$this->namedDependency->somedata = "a value";
1616
}
17-
}
17+
}

0 commit comments

Comments
 (0)