Skip to content

Commit b527077

Browse files
committed
Merge pull request #4 from PHP-DI/refactoring
Big simplification, less overhead
2 parents b9eccdb + 0007f6d commit b527077

File tree

5 files changed

+112
-107
lines changed

5 files changed

+112
-107
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"php": ">=5.4",
1818
"php-di/php-di": "~5.0",
1919
"silex/silex" : "~1.3",
20-
"pimple/pimple" : "~1.1",
21-
"mouf/pimple-interop": "~1.0"
20+
"pimple/pimple" : "~1.1"
2221
},
2322
"require-dev": {
2423
"phpunit/phpunit": "~4.5",
25-
"twig/twig": "~1.8"
24+
"twig/twig": "~1.8",
25+
"swiftmailer/swiftmailer": "^5.4"
2626
}
2727
}

src/Application.php

+13-50
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace DI\Bridge\Silex;
44

5-
use DI\Bridge\Silex\Container\CompositeContainer;
5+
use DI\Bridge\Silex\Container\ContainerInteropProxy;
66
use DI\Bridge\Silex\Controller\ControllerResolver;
77
use DI\Container;
88
use DI\ContainerBuilder;
99
use Interop\Container\ContainerInterface;
10-
use Interop\Container\Pimple\PimpleInterop;
11-
use Pimple;
1210

1311
/**
1412
* Replacement for the Silex Application class to use PHP-DI instead of Pimple.
@@ -18,90 +16,55 @@
1816
class Application extends \Silex\Application
1917
{
2018
/**
21-
* @var CompositeContainer
19+
* @var ContainerInteropProxy
2220
*/
23-
private $rootContainer;
21+
private $containerInteropProxy;
2422

2523
/**
2624
* @var Container
2725
*/
2826
private $phpdi;
2927

30-
/**
31-
* @var Pimple
32-
*/
33-
private $pimple;
34-
3528
/**
3629
* @param ContainerBuilder|null $containerBuilder You can optionally provide your preconfigured container builder.
3730
* @param array $values
3831
*/
3932
public function __construct(ContainerBuilder $containerBuilder = null, array $values = [])
4033
{
41-
// The composite container "merges" PHP-DI and Pimple into one container
42-
$this->rootContainer = new CompositeContainer();
43-
44-
$this->pimple = new PimpleInterop();
45-
$this->rootContainer->setPimple($this->pimple);
34+
$this->containerInteropProxy = new ContainerInteropProxy($this);
4635

4736
$containerBuilder = $containerBuilder ?: new ContainerBuilder();
4837
$containerBuilder->addDefinitions([
49-
'Interop\Container\ContainerInterface' => $this->rootContainer,
38+
'Interop\Container\ContainerInterface' => $this->containerInteropProxy,
5039
]);
51-
$containerBuilder->wrapContainer($this->rootContainer);
40+
$containerBuilder->wrapContainer($this->containerInteropProxy);
5241
$this->phpdi = $containerBuilder->build();
5342

54-
$this->rootContainer->setPhpdi($this->phpdi);
55-
5643
parent::__construct($values);
5744

5845
// Override the controller resolver with ours
59-
$this->pimple['resolver'] = function () {
60-
return new ControllerResolver($this->phpdi);
61-
};
46+
$this['resolver'] = new ControllerResolver($this->phpdi);
6247
}
6348

6449
public function offsetGet($id)
6550
{
66-
return $this->rootContainer->get($id);
51+
if (parent::offsetExists($id)) {
52+
return parent::offsetGet($id);
53+
}
54+
return $this->phpdi->get($id);
6755
}
6856

6957
public function offsetExists($id)
7058
{
71-
return $this->rootContainer->has($id);
72-
}
73-
74-
public function offsetSet($id, $value)
75-
{
76-
$this->pimple[$id] = $value;
77-
}
78-
79-
public function offsetUnset($id)
80-
{
81-
unset($this->pimple[$id]);
82-
}
83-
84-
public function raw($id)
85-
{
86-
return $this->pimple->raw($id);
87-
}
88-
89-
public function extend($id, $callable)
90-
{
91-
return $this->pimple->extend($id, $callable);
92-
}
93-
94-
public function keys()
95-
{
96-
throw new \LogicException('Unsupported operation');
59+
return parent::offsetExists($id) || $this->phpdi->has($id);
9760
}
9861

9962
/**
10063
* @return ContainerInterface
10164
*/
10265
public function getContainer()
10366
{
104-
return $this->rootContainer;
67+
return $this->containerInteropProxy;
10568
}
10669

10770
/**

src/Container/CompositeContainer.php

-54
This file was deleted.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace DI\Bridge\Silex\Container;
4+
5+
use DI\Bridge\Silex\Application;
6+
use Interop\Container\ContainerInterface;
7+
8+
/**
9+
* Proxies container-interop methods to the application.
10+
*
11+
* ContainerInterface cannot be implemented directly by Application because it defines
12+
* a `get()` method already (to add a controller for a GET HTTP method). So we use this
13+
* proxy to have a ContainerInterop container but still use the application.
14+
*
15+
* @author Matthieu Napoli <[email protected]>
16+
*/
17+
class ContainerInteropProxy implements ContainerInterface
18+
{
19+
/**
20+
* @var Application
21+
*/
22+
private $application;
23+
24+
public function __construct(Application $application)
25+
{
26+
$this->application = $application;
27+
}
28+
29+
public function get($id)
30+
{
31+
return $this->application->offsetGet($id);
32+
}
33+
34+
public function has($id)
35+
{
36+
return $this->application->offsetExists($id);
37+
}
38+
}

tests/ProvidersTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
namespace DI\Bridge\Silex\Test;
44

55
use DI\ContainerBuilder;
6+
use Silex\Provider\SwiftmailerServiceProvider;
67
use Silex\Provider\TwigServiceProvider;
8+
use Silex\Provider\UrlGeneratorServiceProvider;
9+
use Swift_Events_SimpleEventDispatcher;
10+
use Swift_Mailer;
11+
use Swift_Transport_NullTransport;
712
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\Routing\Generator\UrlGenerator;
814

915
class ProvidersTest extends BaseTestCase
1016
{
@@ -31,4 +37,56 @@ public function test_twig()
3137
$response = $app->handle(Request::create('/'));
3238
$this->assertEquals('Hello', $response->getContent());
3339
}
40+
41+
/**
42+
* @see https://github.com/PHP-DI/Silex-Bridge/issues/3
43+
* @test
44+
*/
45+
public function test_url_generator()
46+
{
47+
$builder = new ContainerBuilder;
48+
$builder->addDefinitions([
49+
// Create an alias so that we can inject with the type-hint
50+
'Symfony\Component\Routing\Generator\UrlGenerator' => \DI\get('url_generator'),
51+
]);
52+
$app = $this->createApplication($builder);
53+
54+
$app->register(new UrlGeneratorServiceProvider());
55+
56+
$app->get('/', function (UrlGenerator $urlGenerator) {
57+
return $urlGenerator->generate('home');
58+
})->bind('home');
59+
60+
$response = $app->handle(Request::create('/'));
61+
$this->assertEquals('/', $response->getContent());
62+
}
63+
64+
/**
65+
* @see https://github.com/PHP-DI/Silex-Bridge/issues/3
66+
* @test
67+
*/
68+
public function test_mailer()
69+
{
70+
$builder = new ContainerBuilder;
71+
$builder->addDefinitions([
72+
// Create an alias so that we can inject with the type-hint
73+
'Swift_Mailer' => \DI\get('mailer'),
74+
]);
75+
$app = $this->createApplication($builder);
76+
77+
$app->register(new SwiftmailerServiceProvider, [
78+
'swiftmailer.transport' => new Swift_Transport_NullTransport(
79+
new Swift_Events_SimpleEventDispatcher
80+
),
81+
]);
82+
83+
$app->get('/', function (Swift_Mailer $mailer) {
84+
$message = \Swift_Message::newInstance();
85+
$mailer->send($message);
86+
return 'OK';
87+
});
88+
89+
$response = $app->handle(Request::create('/'));
90+
$this->assertEquals('OK', $response->getContent());
91+
}
3492
}

0 commit comments

Comments
 (0)