Completed
Pull Request — master (#138)
by Abdul Malik
02:24
created

PHPDiFactory::buildContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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