Available Types & Actions

Currently, there has 7 types, You can use registerAttribute() to control attribute working scope.

  • AttributeType::CLASSES: Same with Attribute::TARGET_CLASS
  • AttributeType::CLASS_CONSTANTS: Same with Attribute::TARGET_CLASS_CONSTANT
  • AttributeType::METHODS: Same with Attribute::TARGET_METHOD
  • AttributeType::FUNCTIONS: Same with Attribute::TARGET_FUNCTION
  • AttributeType::CALLABLE: Special type only provided by AttributeType.
  • AttributeType::PROPERTIES: Same with Attribute::TARGET_PROPERTY
  • AttributeType::PARAMETERS: Same with Attribute::TARGET_PARAMETER

Object & Classes

use Windwalker\Attributes\AttributesResolver;
use Windwalker\Attributes\AttributeType;

// Work on Class and Object
$attributes->registerAttribute(\Decorator::class, AttributeType::CLASSES);

// Decorate existing object
$object = $attributes->decorateObject($object);

// Create object from class and decorate it.
$object = $attributes->createObject(\Foo::class, ...$args);

Function & Method

use Windwalker\Attributes\AttributesResolver;
use Windwalker\Attributes\AttributeType;

// Work on method and function.
$attributes->registerAttribute(\AOP::class, AttributeType::METHODS | AttributeType::FUNCTIONS);
$object = $attributes->resolveMethods(new SomObject());

Callable

Callable type is a special type, allows AttributesResolver to call any callable and wrap the calling process. You can replace parameters or change the return value.

This type works on methods, functions, closures and any callable.

use Windwalker\Attributes\AttributesResolver;
use Windwalker\Attributes\AttributeType;

// Work on method, function, Closure or callable.
$attributes->registerAttribute(\Autowire::class, AttributeType::CALLABLE);

$result = $attributes->call($callable, ...$args);

Properties

use Windwalker\Attributes\AttributesResolver;
use Windwalker\Attributes\AttributeType;

// Work on object properties
$attributes->registerAttribute(\Inject::class, AttributeType::PROPERTIES);

$object = new class {
    #[\Inject]
    protected ?\Foo $foo = null;
};

$object = $attributes->resolveProperties($object);

Parameters

use Windwalker\Attributes\AttributesResolver;
use Windwalker\Attributes\AttributeType;

// Work on callable parameters.
$attributes->registerAttribute(\StrUpper::class, AttributeType::PROPERTIES);

$func = function (
    #[\StrUpper]
    $foo    
) {
    return $foo;
};

$result = $attributes->call($func, ['flower'], /* $context to bind this */); // "FLOWER"
Found a typo? Something is wrong in this documentation? Make an edit
Table of Contents