|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Penny\Container; |
|
4
|
|
|
|
|
5
|
|
|
use DI; |
|
6
|
|
|
use Penny\Dispatcher; |
|
7
|
|
|
use Penny\Event\ZendHttpFlowEvent; |
|
8
|
|
|
use Penny\Event\ZendEvmProxy; |
|
9
|
|
|
use Zend\Diactoros\Response; |
|
10
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
11
|
|
|
|
|
12
|
|
|
class PHPDiFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Container compilation. |
|
16
|
|
|
* |
|
17
|
|
|
* @param mixed $config Configuration file/array. |
|
18
|
|
|
* @param bool $annotation annotation usage. |
|
19
|
|
|
* |
|
20
|
|
|
* @link http://php-di.org/doc/php-definitions.html |
|
21
|
|
|
* |
|
22
|
|
|
* @return DI\Container |
|
23
|
|
|
*/ |
|
24
|
24 |
|
public static function buildContainer($config = [], $annotation = true) |
|
25
|
|
|
{ |
|
26
|
24 |
|
$builder = static::initialSetupContainerBuilder($annotation); |
|
27
|
24 |
|
$builder->addDefinitions($config); |
|
28
|
|
|
|
|
29
|
24 |
|
$container = $builder->build(); |
|
30
|
24 |
|
$container->set('di', $container); |
|
31
|
|
|
|
|
32
|
24 |
|
return $container; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initial setup of DI\ContainerBuilder. |
|
37
|
|
|
* |
|
38
|
|
|
* @param bool $annotation annotation usage. |
|
39
|
|
|
* |
|
40
|
|
|
* @return DI\ContainerBuilder |
|
41
|
|
|
*/ |
|
42
|
24 |
|
protected static function initialSetupContainerBuilder($annotation) |
|
43
|
|
|
{ |
|
44
|
24 |
|
$builder = new DI\ContainerBuilder(); |
|
45
|
24 |
|
$builder->useAnnotations($annotation); |
|
46
|
24 |
|
$builder->addDefinitions([ |
|
47
|
24 |
|
'request' => ServerRequestFactory::fromGlobals(), |
|
48
|
24 |
|
'response' => DI\object(Response::class), |
|
49
|
24 |
|
'http_flow_event' => DI\object(ZendHttpFlowEvent::class) |
|
50
|
24 |
|
->constructor('bootstrap', DI\get('request'), DI\get('response')), |
|
51
|
24 |
|
'event_manager' => DI\object(ZendEvmProxy::class), |
|
52
|
24 |
|
'dispatcher' => DI\factory(function ($container) { |
|
53
|
13 |
|
return new Dispatcher($container->get('router'), $container); |
|
54
|
24 |
|
}), |
|
55
|
24 |
|
]); |
|
56
|
|
|
|
|
57
|
24 |
|
return $builder; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|