Completed
Push290fce...9f07ef
passed — Build
created

functions.php ➔ factory()   A

↳ Parent: Project

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace DI;
4
5
use DI\Definition\EntryReference;
6
use DI\Definition\Helper\ArrayDefinitionExtensionHelper;
7
use DI\Definition\Helper\EnvironmentVariableDefinitionHelper;
8
use DI\Definition\Helper\FactoryDefinitionHelper;
9
use DI\Definition\Helper\ObjectDefinitionHelper;
10
use DI\Definition\Helper\StringDefinitionHelper;
11
use DI\Definition\Helper\ValueDefinitionHelper;
12
13
if (! function_exists('DI\value')) {
14
    /**
15
     * Helper for defining a value.
16
     *
17
     * @param mixed $value
18
     *
19
     * @return ValueDefinitionHelper
20
     */
21
    function value($value)
22
    {
23
        return new ValueDefinitionHelper($value);
24
    }
25
}
26
27
if (! function_exists('DI\object')) {
28
    /**
29
     * Helper for defining an object.
30
     *
31
     * @param string|null $className Class name of the object.
32
     *                               If null, the name of the entry (in the container) will be used as class name.
33
     *
34
     * @return ObjectDefinitionHelper
35
     */
36
    function object($className = null)
37
    {
38
        return new ObjectDefinitionHelper($className);
39
    }
40
}
41
42
if (! function_exists('DI\factory')) {
43
    /**
44
     * Helper for defining a container entry using a factory function/callable.
45
     *
46
     * @param callable $factory The factory is a callable that takes the container as parameter
47
     *                          and returns the value to register in the container.
48
     *
49
     * @return FactoryDefinitionHelper
50
     */
51
    function factory($factory)
52
    {
53
        return new FactoryDefinitionHelper($factory);
54
    }
55
}
56
57
if (! function_exists('DI\decorate')) {
58
    /**
59
     * Decorate the previous definition using a callable.
60
     *
61
     * Example:
62
     *
63
     *     'foo' => decorate(function ($foo, $container) {
64
     *         return new CachedFoo($foo, $container->get('cache'));
65
     *     })
66
     *
67
     * @param callable $callable The callable takes the decorated object as first parameter and
68
     *                           the container as second.
69
     *
70
     * @return FactoryDefinitionHelper
71
     */
72
    function decorate($callable)
73
    {
74
        return new FactoryDefinitionHelper($callable, true);
75
    }
76
}
77
78
if (! function_exists('DI\get')) {
79
    /**
80
     * Helper for referencing another container entry in an object definition.
81
     *
82
     * @param string $entryName
83
     *
84
     * @return EntryReference
85
     */
86
    function get($entryName)
87
    {
88
        return new EntryReference($entryName);
89
    }
90
}
91
92
if (! function_exists('DI\link')) {
93
    /**
94
     * Helper for referencing another container entry in an object definition.
95
     *
96
     * @deprecated \DI\link() has been replaced by \DI\get()
97
     *
98
     * @param string $entryName
99
     *
100
     * @return EntryReference
101
     */
102
    function link($entryName)
103
    {
104
        return new EntryReference($entryName);
105
    }
106
}
107
108
if (! function_exists('DI\env')) {
109
    /**
110
     * Helper for referencing environment variables.
111
     *
112
     * @param string $variableName The name of the environment variable.
113
     * @param mixed $defaultValue The default value to be used if the environment variable is not defined.
114
     *
115
     * @return EnvironmentVariableDefinitionHelper
116
     */
117
    function env($variableName, $defaultValue = null)
118
    {
119
        // Only mark as optional if the default value was *explicitly* provided.
120
        $isOptional = 2 === func_num_args();
121
122
        return new EnvironmentVariableDefinitionHelper($variableName, $isOptional, $defaultValue);
123
    }
124
}
125
126
if (! function_exists('DI\add')) {
127
    /**
128
     * Helper for extending another definition.
129
     *
130
     * Example:
131
     *
132
     *     'log.backends' => DI\add(DI\get('My\Custom\LogBackend'))
133
     *
134
     * or:
135
     *
136
     *     'log.backends' => DI\add([
137
     *         DI\get('My\Custom\LogBackend')
138
     *     ])
139
     *
140
     * @param mixed|array $values A value or an array of values to add to the array.
141
     *
142
     * @return ArrayDefinitionExtensionHelper
143
     *
144
     * @since 5.0
145
     */
146
    function add($values)
147
    {
148
        if (! is_array($values)) {
149
            $values = [$values];
150
        }
151
152
        return new ArrayDefinitionExtensionHelper($values);
153
    }
154
}
155
156
if (! function_exists('DI\string')) {
157
    /**
158
     * Helper for concatenating strings.
159
     *
160
     * Example:
161
     *
162
     *     'log.filename' => DI\string('{app.path}/app.log')
163
     *
164
     * @param string $expression A string expression. Use the `{}` placeholders to reference other container entries.
165
     *
166
     * @return StringDefinitionHelper
167
     *
168
     * @since 5.0
169
     */
170
    function string($expression)
171
    {
172
        return new StringDefinitionHelper((string) $expression);
173
    }
174
}
175