From aa354bffb3e6149c934181c2812b28ac42a4b768 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Fri, 27 Sep 2024 12:04:01 +0200 Subject: [PATCH] feat: add own di helper aliases Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- src/Kernel.php | 2 +- src/di-functions.php | 150 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/di-functions.php diff --git a/src/Kernel.php b/src/Kernel.php index 3b3d071..8e1963f 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -103,7 +103,7 @@ final class Kernel { $builder = new ContainerBuilder( $original_builder ); - if ( ! function_exists('WPDesk\Init\DI\create') ) { + if ( ! function_exists( 'WPDesk\Init\DI\create' ) ) { require __DIR__ . '/di-functions.php'; } diff --git a/src/di-functions.php b/src/di-functions.php new file mode 100644 index 0000000..993a5b3 --- /dev/null +++ b/src/di-functions.php @@ -0,0 +1,150 @@ +<?php +/** + * This is a vendored set of php-di/php-di helper functions. We place it directly in our library to improve experience when using DI along with code scoping, which defy composer's file autoloading. + * + * @see https://github.com/PHP-DI/PHP-DI/blob/master/src/functions.php + */ + +declare(strict_types=1); + +namespace WPDesk\Init\DI; + +use DI\Definition\ArrayDefinitionExtension; +use DI\Definition\EnvironmentVariableDefinition; +use DI\Definition\Helper\AutowireDefinitionHelper; +use DI\Definition\Helper\CreateDefinitionHelper; +use DI\Definition\Helper\FactoryDefinitionHelper; +use DI\Definition\Reference; +use DI\Definition\StringDefinition; +use DI\Definition\ValueDefinition; + +if ( ! function_exists( 'WPDesk\Init\DI\value' ) ) { + /** + * Helper for defining a value. + */ + function value( mixed $value ): ValueDefinition { + return new ValueDefinition( $value ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\create' ) ) { + /** + * Helper for defining an object. + * + * @param string|null $className Class name of the object. + * If null, the name of the entry (in the container) will be used as class name. + */ + function create( ?string $className = null ): CreateDefinitionHelper { + return new CreateDefinitionHelper( $className ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\autowire' ) ) { + /** + * Helper for autowiring an object. + * + * @param string|null $className Class name of the object. + * If null, the name of the entry (in the container) will be used as class name. + */ + function autowire( ?string $className = null ): AutowireDefinitionHelper { + return new AutowireDefinitionHelper( $className ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\factory' ) ) { + /** + * Helper for defining a container entry using a factory function/callable. + * + * @param callable|array|string $factory The factory is a callable that takes the container as parameter + * and returns the value to register in the container. + */ + function factory( callable|array|string $factory ): FactoryDefinitionHelper { + return new FactoryDefinitionHelper( $factory ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\decorate' ) ) { + /** + * Decorate the previous definition using a callable. + * + * Example: + * + * 'foo' => decorate(function ($foo, $container) { + * return new CachedFoo($foo, $container->get('cache')); + * }) + * + * @param callable $callable The callable takes the decorated object as first parameter and + * the container as second. + */ + function decorate( callable|array|string $callable ): FactoryDefinitionHelper { + return new FactoryDefinitionHelper( $callable, true ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\get' ) ) { + /** + * Helper for referencing another container entry in an object definition. + */ + function get( string $entryName ): Reference { + return new Reference( $entryName ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\env' ) ) { + /** + * Helper for referencing environment variables. + * + * @param string $variableName The name of the environment variable. + * @param mixed $defaultValue The default value to be used if the environment variable is not defined. + */ + function env( string $variableName, mixed $defaultValue = null ): EnvironmentVariableDefinition { + // Only mark as optional if the default value was *explicitly* provided. + $isOptional = 2 === func_num_args(); + + return new EnvironmentVariableDefinition( $variableName, $isOptional, $defaultValue ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\add' ) ) { + /** + * Helper for extending another definition. + * + * Example: + * + * 'log.backends' => DI\add(DI\get('My\Custom\LogBackend')) + * + * or: + * + * 'log.backends' => DI\add([ + * DI\get('My\Custom\LogBackend') + * ]) + * + * @param mixed|array $values A value or an array of values to add to the array. + * + * @since 5.0 + */ + function add( $values ): ArrayDefinitionExtension { + if ( ! is_array( $values ) ) { + $values = [ $values ]; + } + + return new ArrayDefinitionExtension( $values ); + } +} + +if ( ! function_exists( 'WPDesk\Init\DI\string' ) ) { + /** + * Helper for concatenating strings. + * + * Example: + * + * 'log.filename' => DI\string('{app.path}/app.log') + * + * @param string $expression A string expression. Use the `{}` placeholders to reference other container entries. + * + * @since 5.0 + */ + function string( string $expression ): StringDefinition { + return new StringDefinition( $expression ); + } +} -- GitLab