This package provides an easy way to use and apply PHP 8 Attributes and allows quick real-world implementations meta-data or annotation related features like Routes, Events, DB Relations.
- Apply PHP 8 attributes to a class and class components
- Automatically apply attributes to autoloaded classes
- Restrict attributes to filter condition of class name, namespace or class reflection
See Upcoming features of next release.
php-attributes uses a decorated composer autoloader and notifies a handler with loaded class name. The attributes for class components get resolved using reflections.
This package supports attributes for
- classes
- class constants
- class methods
- class method parameters
- class properties.
Via Composer
$ composer require mbunge/php-attributes
Instantiate the attribute handler Mbunge\PhpAttributes\AttributeResolver
via factory
PhpAttributes\PhpAttributesFactory::createResolver()
or direct.
Attributes of traget class components get resolved by passed class name as string
to Mbunge\PhpAttributes\Resolver\AttributeResolver::resolve(string $class)
.
All resolved attributes returned as an array of
data-transfer object (dto) Mbunge\PhpAttributes\Resolver\ResolvedAttributesDto
.
<?php
use Mbunge\PhpAttributes\PhpAttributesFactory;
// instantiate via factory
$handler = (new PhpAttributesFactory())->createResolver();
// instantiate direct
$handler = new Mbunge\PhpAttributes\AttributeResolver();
/** @var \Mbunge\PhpAttributes\Resolver\ResolvedAttributeDto[] $result */
// via string
$result = $handler->resolve('\MyProject\MyClassWithAttributes');
// or via class name
$result = $handler->resolve(\MyProject\MyClassWithAttributes::class);
\Mbunge\PhpAttributes\Resolver\ChainedAttributeResolver
receives a list of resolvers, execute each resolver and merge reolved results.
This is usefull when resolvers with different contexts need to execute at once.
<?php
use Mbunge\PhpAttributes\Resolver\ChainedAttributeResolver;
$resolvers = [
new CustomAttributeResolver(),
new AnotherAttributeResolver(),
// ...
];
$resolver = new ChainedAttributeResolver($resolvers);
// receive results from CustomerAttributeResolver and AnotherAttributeResolver
$results = $resolver->resolve('MyProject\AnyClass');
Mbunge\PhpAttributes\Resolver\FilterClassAttributeResolverDecorator
decorates an instance of attribute handler with
any callable filter.
<?php
use Mbunge\PhpAttributes\Resolver\FilterClassAttributeResolverDecorator;
// instantiate base handler
$handler = new Mbunge\PhpAttributes\AttributeResolver();
// instantiate filter decorator with filter given as callable
$decoratedResolver = new FilterClassAttributeResolverDecorator(
$handler,
fn(string $className) => str_starts_with($className, 'MyProject')
);
// Attribute resolves only if filter condition matches
$result = $decoratedResolver
->resolve(\MyProject\MyClassWithAttributes::class);
See FilterClassAttributeResolverDecoratorTest for filter examples
Resolved attributes provide declared meta-data.
Pass resolved attributes to presenter and perform context specifc actions.
All examples use a small Application implementation.
- auto-subscribe event listeners with event dispatcher aware presenter.
\Mbunge\PhpAttributes\Presenter\ChainedAttributePresenter
receives a list of presenters, execute each presenter and merge results.
This is usefull when attributes needs to present to different contexts, like application routeing, event dispatcher, etc.
See ChainedAttributePresenterTest for detailed implementation.
Avoid blueprint code and use handler to resolve attributes and present them afterwards.
<?php
use Mbunge\PhpAttributes\AttributeHandler;
use Mbunge\PhpAttributes\PhpAttributesFactory;
/** @var ClassLoader $loader */
$loader = require __DIR__ . '/vendor/autoload.php';
// optionally pass a handler to handler
// You may add custom handler behaviour or a custom handler at this point
$handler = new AttributeHandler(
(new PhpAttributesFactory())->createResolver(),
new NullAttributePresenter()
);
$handler->handle('MyProject\MyClass');
The libray provides a composer classloader decorator which extends composer autoloader with the ability to execute attribute handler when class got autoloaded.
See also packaged autoload.
<?php
use Composer\Autoload\ClassLoader;
use Mbunge\PhpAttributes\AttributeHandler;
use Mbunge\PhpAttributes\LoaderHandler;
use Mbunge\PhpAttributes\PhpAttributesFactory;
/** @var ClassLoader $loader */
$loader = require __DIR__ . '/vendor/autoload.php';
// optionally pass a handler to handler
// You may add custom handler behaviour or a custom handler at this point
$attributeHandler = new AttributeHandler(
(new PhpAttributesFactory())->createResolver(),
new NullAttributePresenter()
);
$handler = new LoaderHandler($attributeHandler);
return $handler->handle($loader);
// optionally avoid unregister of previous autoloader
// it is recommanded to keep only one autoloader, since previous autoloader will be unreachable
return $handler->handle($loader, false);
Replace composer default autoload /vendor/autoload
with packaged autoload /vendor/mbunge/php-attributes/autoload.php
The packed autoload applies all attributes to autoloaded classes.
<?php
require_once __DIR__ . '/vendor/mbunge/php-attributes/autoload.php';
PHP 8 Attributes will not solve all problems.
Automatically apply logic through meta-data declarations sounds cool, but requires a lot of convention and my lead into complexity and may be hard to debug!
SOLID may get violated as well, since attributes are some sort of dependecy. Use attributes only for helpful tasks and without violating clean code priciples!
Furthermore, it is helpful to provide cli tools for attribute analytics and debugging.
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Only maintainers are allowed to deploy new versions!
- Run
composer run release
which will run tests and on success update changelog, package version and creates a release tag - switch to master branch and merge develop branch
- Run
composer run deploy
which will run tests and on success push tags, master branch and develop branch
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
- In-depth PHP Attributes by stitcher.io: https://www.stitcher.io/blog/attributes-in-php-8