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