Completed
Push — master ( d7966f...06a3b6 )
by Matthieu
06:50 queued 03:47
created

functions.php ➔ autowire()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Importance

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