0 follower

Final Class Yiisoft\Di\Helpers\DefinitionParser

InheritanceYiisoft\Di\Helpers\DefinitionParser

Public Methods

Hide inherited methods

Method Description Defined By
parse() Yiisoft\Di\Helpers\DefinitionParser

Constants

Hide inherited constants

Constant Value Description Defined By
DEFINITION_META 'definition' Yiisoft\Di\Helpers\DefinitionParser
IS_PREPARED_ARRAY_DEFINITION_DATA 'isPreparedArrayDefinitionData' Yiisoft\Di\Helpers\DefinitionParser

Method Details

Hide inherited methods

parse() public static method

public static parse( mixed $definition ): array
$definition mixed

Definition to parse.

return array

Definition parsed into an array of a special structure.

                public static function parse(mixed $definition): array
{
    if (!is_array($definition)) {
        return [$definition, []];
    }
    // Dedicated definition
    if (isset($definition[self::DEFINITION_META])) {
        $newDefinition = $definition[self::DEFINITION_META];
        unset($definition[self::DEFINITION_META]);
        return [$newDefinition, $definition];
    }
    // Callable definition
    if (is_callable($definition, true)) {
        return [$definition, []];
    }
    // Array definition
    $meta = [];
    $class = null;
    $constructorArguments = [];
    $methodsAndProperties = [];
    foreach ($definition as $key => $value) {
        if (is_string($key)) {
            // Class
            if ($key === ArrayDefinition::CLASS_NAME) {
                $class = $value;
                continue;
            }
            // Constructor arguments
            if ($key === ArrayDefinition::CONSTRUCTOR) {
                $constructorArguments = $value;
                continue;
            }
            // Methods and properties
            if (count($methodArray = explode('()', $key, 2)) === 2) {
                $methodsAndProperties[$key] = [ArrayDefinition::TYPE_METHOD, $methodArray[0], $value];
                continue;
            }
            if (count($propertyArray = explode('$', $key, 2)) === 2) {
                $methodsAndProperties[$key] = [ArrayDefinition::TYPE_PROPERTY, $propertyArray[1], $value];
                continue;
            }
        }
        $meta[$key] = $value;
    }
    return [
        [
            'class' => $class,
            '__construct()' => $constructorArguments,
            'methodsAndProperties' => $methodsAndProperties,
            self::IS_PREPARED_ARRAY_DEFINITION_DATA => true,
        ],
        $meta,
    ];
}