Completed
Push — develop ( 8060c5...f5ccfa )
by jake
02:47
created

ViewHelperConfig::define()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
/**
3
 * Aura\View Provider for Aura\Di
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Config
13
 * @package   Fusible\ViewProvider
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      https://github.com/fusible/fusible.view-provider
18
 */
19
20
namespace Fusible\ViewProvider;
21
22
use Aura\Di\Container;
23
use Aura\Di\ContainerConfig;
24
25
use Aura\Html;
26
27
/**
28
 * View Helper Config
29
 *
30
 * @category Config
31
 * @package  Fusible\ViewProvider
32
 * @author   Jake Johns <[email protected]>
33
 * @license  http://jnj.mit-license.org/2016 MIT License
34
 * @link     https://github.com/fusible/fusible.view-provider
35
 *
36
 * @see ContainerConfig
37
 */
38
class ViewHelperConfig extends ContainerConfig
39
{
40
41
    protected $specs = [];
42
43
    /**
44
     * __construct
45
     *
46
     * @param array $specs helper specs
47
     *
48
     * @return mixed
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
49
     *
50
     * @access public
51
     */
52
    public function __construct(array $specs = [])
53
    {
54
        $this->specs = $specs;
55
    }
56
57
    /**
58
     * AddHelpers
59
     *
60
     * @param array $specs DESCRIPTION
61
     *
62
     * @return mixed
63
     * @throws exceptionclass [description]
64
     *
65
     * @access public
66
     */
67
    public function addHelpers(array $specs)
68
    {
69
        $this->specs = array_merge($this->specs, $specs);
70
    }
71
72
    /**
73
     * Define Aura\View and Aura\Html factories and services
74
     *
75
     * @param Container $di DI Container
76
     *
77
     * @return void
78
     *
79
     * @access public
80
     *
81
     * @SuppressWarnings(PHPMD.ShortVariable)
82
     */
83
    public function define(Container $di)
84
    {
85
        $di->set(
86
            Html\HelperLocatorFactory::class,
87
            $di->lazyNew(Html\HelperLocatorFactory::class)
88
        );
89
90
        $di->set(
91
            Html\HelperLocator::class,
92
            $di->lazyGetCall(
93
                Html\HelperLocatorFactory::class,
94
                'newInstance'
95
            )
96
        );
97
98
        $di->set(
99
            Html\EscaperFactory::class,
100
            $di->lazyNew(Html\EscaperFactory::class)
101
        );
102
103
        $di->set(
104
            Html\Escaper::class,
105
            $di->lazyGetCall(Html\EscaperFactory::class, 'newInstance')
106
        );
107
108
        $di->params[Html\Helper\AbstractHelper::class] = [
109
            'escaper' => $di->lazyGet(Html\Escaper::class)
110
        ];
111
    }
112
113
    /**
114
     * Modify
115
     *
116
     * @param Container $di DESCRIPTION
117
     *
118
     * @return null
119
     *
120
     * @access public
121
     *
122
     * @SuppressWarnings(PHPMD.ShortVariable)
123
     */
124
    public function modify(Container $di)
125
    {
126
        $helpers  = $di->get(HelperLocator::class);
127
        $resolver = $di->newResolutionHelper();
128
        foreach ($this->getFactories($resolver) as $name => $factory) {
129
            $helpers->set($name, $factory);
130
        }
131
    }
132
133
    /**
134
     * GetFactories
135
     *
136
     * @param callable $resolve DESCRIPTION
137
     *
138
     * @return mixed
139
     *
140
     * @access protected
141
     *
142
     * @SuppressWarnings(PHPMD.ShortVariable)
143
     */
144
    protected function getFactories(callable $resolve)
145
    {
146
        $factories = [];
147
        foreach ($this->specs as $name => $class) {
148
            $factories[$name] = function () use ($resolve, $class) {
149
                return $resolve($class);
150
            };
151
        }
152
        return $factories;
153
    }
154
155
}
156