Completed
Push — develop ( 5a5c97...8c9c6b )
by jake
03:46
created

Config::modify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
crap 12
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
use Aura\View;
27
28
/**
29
 * Config
30
 *
31
 * @category Config
32
 * @package  Fusible\ViewProvider
33
 * @author   Jake Johns <[email protected]>
34
 * @license  http://jnj.mit-license.org/2016 MIT License
35
 * @link     https://github.com/fusible/fusible.view-provider
36
 *
37
 * @see ContainerConfig
38
 */
39
class Config extends ContainerConfig
40
{
41
    // View
42
    const VIEW         = View\View::class;
43
    const VIEW_FACTORY = View\ViewFactory::class;
44
45
    // View params
46
    const VIEW_MAP     = self::class . '::VIEW_MAP';
47
    const VIEW_PATHS   = self::class . '::VIEW_PATHS';
48
    const LAYOUT_MAP   = self::class . '::LAYOUT_MAP';
49
    const LAYOUT_PATHS = self::class . '::LAYOUT_PATHS';
50
51
    // Helper
52
    const HELPER_LOCATOR = Html\HelperLocator::class;
53
    const HELPER_FACTORY = Html\HelperLocatorFactory::class;
54
55
    // Helpers
56
    const HELPER_SPECS   = self::class . '::HELPER_SPECS';
57
58
    // Escaper
59
    const ESCAPER_FACTORY = Html\EscaperFactory::class;
60
    const ESCAPER         = Html\Escaper::class;
61 9
62
63
    protected $params = [
64 9
        self::VIEW_MAP      => [],
65 9
        self::VIEW_PATHS    => [],
66 9
        self::LAYOUT_MAP    => [],
67 9
        self::LAYOUT_PATHS  => [],
68
        self::HELPER_SPECS  => []
69 9
    ];
70 9
71 9
    /**
72 9
     * Define Aura\View and Aura\Html factories and services
73
     *
74 9
     * @param Container $di DI Container
75 9
     *
76
     * @return void
77 9
     *
78 9
     * @access public
79 9
     *
80 9
     * @SuppressWarnings(PHPMD.ShortVariable)
81
     */
82 9
    public function define(Container $di)
83 9
    {
84 9
        foreach ($this->params as $key => $value) {
85 9
            if (! isset($di->values[$key])) {
86
                $di->values[$key] = $value;
87 9
            }
88 9
        }
89 9
90
        $this->defineView($di);
91
        $this->defineHelpers($di);
92
    }
93 9
94 9
    /**
95 9
     * Define Aura\Html factories and services
96 9
     *
97
     * @param Container $di DI Container
98 9
     *
99 9
     * @return void
100 9
     *
101 9
     * @access public
102 9
     *
103 9
     * @SuppressWarnings(PHPMD.ShortVariable)
104 9
     */
105 9
    protected function defineHelpers(Container $di)
106
    {
107 9
        $di->set(
108
            self::HELPER_FACTORY,
109
            $di->lazyNew(Html\HelperLocatorFactory::class)
110
        );
111
112
        $di->set(
113
            self::HELPER_LOCATOR,
114
            $di->lazyGetCall(self::HELPER_FACTORY, 'newInstance')
115
        );
116
117
        $di->set(
118
            self::ESCAPER_FACTORY,
119
            $di->lazyNew(Html\EscaperFactory::class)
120
        );
121
122
        $di->set(
123
            self::ESCAPER,
124
            $di->lazyGetCall(self::ESCAPER_FACTORY, 'newInstance')
125
        );
126
127
        $di->params[Html\Helper\AbstractHelper::class] = [
128
            'escaper' => $di->lazyGet(self::ESCAPER)
129
        ];
130
    }
131
132
    /**
133
     * Define Aura\View factories and services
134
     *
135
     * @param Container $di DI Container
136
     *
137
     * @return void
138
     *
139
     * @access public
140
     *
141
     * @SuppressWarnings(PHPMD.ShortVariable)
142
     */
143
    protected function defineView(Container $di)
144
    {
145
        $di->set(
146
            self::VIEW_FACTORY,
147
            $di->lazyNew(View\ViewFactory::class)
148
        );
149
150
        $di->set(
151
            self::VIEW,
152
            $di->lazyGetCall(
153
                View\ViewFactory::class,
154
                'newInstance',
155
                $di->lazyGet(self::HELPER_LOCATOR),
156
                $di->lazyValue(self::VIEW_MAP),
157
                $di->lazyValue(self::VIEW_PATHS),
158
                $di->lazyValue(self::LAYOUT_MAP),
159
                $di->lazyValue(self::LAYOUT_PATHS)
160
            )
161
        );
162
    }
163
164
    /**
165
     * Define Add helpers
166
     *
167
     * @param Container $di DI Container
168
     *
169
     * @return void
170
     *
171
     * @access public
172
     *
173
     * @SuppressWarnings(PHPMD.ShortVariable)
174
     */
175
    public function modify(Container $di)
176
    {
177
        $specs = $di->lazyValue(self::HELPER_SPECS);
178
        $specs = $specs();
179
        if (! $specs) {
180
            return;
181
        }
182
183
        $helpers = $di->get(self::HELPER_LOCATOR);
184
        $resolve = $di->newResolutionHelper();
185
186
        foreach ($specs as $key => $spec) {
187
188
            $factory = function () use ($resolve, $spec) {
189
                return $resolve($spec);
190
            };
191
192
            $helpers->set($key, $factory);
193
        }
194
    }
195
196
}
197