Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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