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