Completed
Push — master ( 309e79...fc03a4 )
by Gianluca
02:01
created

PHPDiFactory::initialSetupContainerBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.4286
cc 1
eloc 12
nc 1
nop 1
crap 1
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 22
    public static function buildContainer($config = [], $annotation = true)
25
    {
26 22
        $builder = static::initialSetupContainerBuilder($annotation);
27 22
        $builder->addDefinitions($config);
28
29 22
        $container = $builder->build();
30 22
        $container->set('di', $container);
31
32 22
        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 22
    protected static function initialSetupContainerBuilder($annotation)
43
    {
44 22
        $builder = new DI\ContainerBuilder();
45 22
        $builder->useAnnotations($annotation);
46 22
        $builder->addDefinitions([
47 22
            'request' => ServerRequestFactory::fromGlobals(),
48 22
            'response' => DI\object(Response::class),
49 22
            'http_flow_event' => DI\object(ZendHttpFlowEvent::class)
50 22
                ->constructor('bootstrap', DI\get('request'), DI\get('response')),
51 22
            'event_manager' => DI\object(ZendEvmProxy::class),
52 22
            'dispatcher' => DI\factory(function ($container) {
53 12
                return new Dispatcher($container->get('router'), $container);
54 22
            }),
55
        ]);
56
57 22
        return $builder;
58
    }
59
}
60