Documentation

AttributeProcessor
in package
implements MiddlewareInterface

Processes PHP 8 attributes on registered classes

Interfaces, Classes, Traits and Enums

MiddlewareInterface

Table of Contents

$config  : ConfigInterface|null
$handlerClasses  : array<string|int, mixed>
$handlers  : array<string|int, mixed>
$namespaces  : array<string|int, mixed>
__construct()  : mixed
add()  : void
Adds an attribute handler for processing
addNamespace()  : void
Adds a namespace
run()  : void
Runs the middleware
createHandlersPerTarget()  : void
Creates the handler instances and puts them into the right `$handlers` array
discoverClassesFromNamespaces()  : void
Discovers classes from Composer PSR-4 autoload map for configured namespaces
isProcessAllowed()  : bool
If no namespace added returns true, otherwise checks the namespace and returns true if the interface is in it.
loadNamespacesFromConfig()  : void
Reads `app.scan_namespaces` from config and merges with programmatically added namespaces
process()  : void
Processes one class with the given name
processAll()  : void
Processes all interfaces in the App or those that are in the given namespaces
processClass()  : void
Processes all class-level attributes for the class
processMethods()  : void
Processes all method-level attributes for all the methods of a class
processProperties()  : void
Processes all property-level attributes for all the properties of a class
processSubject()  : void
Processes attributes on a class, property or method
scanDirectory()  : array<string|int, string>
Recursively scans a directory for PHP files and derives FQCNs

Properties

$handlers

protected array<string|int, mixed> $handlers = [\Dynart\Micro\AttributeHandlerInterface::TARGET_CLASS => [], \Dynart\Micro\AttributeHandlerInterface::TARGET_PROPERTY => [], \Dynart\Micro\AttributeHandlerInterface::TARGET_METHOD => []]

Methods

add()

Adds an attribute handler for processing

public add(string $className) : void

The given class name should implement the AttributeHandler interface, otherwise it will throw a MicroException.

Parameters
$className : string

The class name

Tags
throws
MicroException

if the given class does not implement AttributeHandler

Return values
void —

addNamespace()

Adds a namespace

public addNamespace(string $namespace) : void

If one or more namespace added only those will be processed. The namespace should NOT start with a backslash!

Parameters
$namespace : string
Return values
void —

createHandlersPerTarget()

Creates the handler instances and puts them into the right `$handlers` array

protected createHandlersPerTarget() : void
Return values
void —

discoverClassesFromNamespaces()

Discovers classes from Composer PSR-4 autoload map for configured namespaces

protected discoverClassesFromNamespaces() : void

Scans directories matching the configured namespaces, derives FQCNs from file paths, and registers them with Micro::add(). Skips interfaces, abstract classes, and traits.

Return values
void —

isProcessAllowed()

If no namespace added returns true, otherwise checks the namespace and returns true if the interface is in it.

protected isProcessAllowed(string $className) : bool
Parameters
$className : string

The name of the class

Return values
bool —

Should we process this class?

loadNamespacesFromConfig()

Reads `app.scan_namespaces` from config and merges with programmatically added namespaces

protected loadNamespacesFromConfig() : void
Return values
void —

process()

Processes one class with the given name

protected process(string $className) : void
Parameters
$className : string

The name of the class

Return values
void —

processAll()

Processes all interfaces in the App or those that are in the given namespaces

protected processAll() : void
Return values
void —

processClass()

Processes all class-level attributes for the class

protected processClass(ReflectionClass $refClass) : void
Parameters
$refClass : ReflectionClass
Return values
void —

processMethods()

Processes all method-level attributes for all the methods of a class

protected processMethods(ReflectionClass $refClass) : void
Parameters
$refClass : ReflectionClass
Return values
void —

processProperties()

Processes all property-level attributes for all the properties of a class

protected processProperties(ReflectionClass $refClass) : void
Parameters
$refClass : ReflectionClass
Return values
void —

processSubject()

Processes attributes on a class, property or method

protected processSubject(AttributeHandler $handler, string $className, ReflectionClass|ReflectionProperty|ReflectionMethod $subject) : void

Gets the PHP 8 attributes from the subject that match the handler's attribute class, instantiates each and calls the handler's handle() method.

Parameters
$handler : AttributeHandler

The attribute handler

$className : string

The class name

$subject : ReflectionClass|ReflectionProperty|ReflectionMethod

The reflection class, property or method

Return values
void —

scanDirectory()

Recursively scans a directory for PHP files and derives FQCNs

protected scanDirectory(string $dir, string $namespacePrefix[, string $subPath = '' ]) : array<string|int, string>
Parameters
$dir : string

The directory to scan

$namespacePrefix : string

The PSR-4 namespace prefix (e.g. "App\")

$subPath : string = ''

Additional sub-path within the namespace (e.g. "Controllers")

Return values
array<string|int, string> —

Array of fully-qualified class names

Search results