delormejonathan/accessible-bundle
Installation:
composer require antares/accessible-bundle
Register the bundle in config/bundles.php:
return [
// ...
Antares\AccessibleBundle\AntaresAccessibleBundle::class => ['all' => true],
];
Enable Annotations:
Ensure Doctrine Annotations are enabled in config/packages/doctrine.yaml:
doctrine:
orm:
annotations:
filter: ~
First Use Case: Create a simple entity with annotations:
use Antares\Accessible\Annotation\Access;
use Antares\Accessible\Annotation\AutomatedBehaviorTrait;
use Symfony\Component\Validator\Constraints as Assert;
class User
{
use AutomatedBehaviorTrait;
/**
* @Access({Access::GET, Access::SET})
* @Assert\NotBlank
*/
private $name;
}
Test access:
$user = new User();
$user->setName('John'); // Works
$user->getName(); // Returns 'John'
$user->setName(''); // Throws exception (due to @Assert\NotBlank)
Dynamic Getters/Setters:
Use @Access to control property access. Combine with Symfony Validator constraints for runtime validation:
/**
* @Access({Access::GET, Access::SET})
* @Assert\Length(min=5, max=255)
*/
private $username;
Constructor Injection:
Define constructor parameters with @Access:
class Order
{
use AutomatedBehaviorTrait;
/**
* @Access({Access::CONSTRUCT})
* @Assert\Positive
*/
private $quantity;
}
Usage:
$order = new Order(5); // Valid
$order = new Order(-1); // Throws exception
Collections and Associations:
Use @Access with @Assert\Valid for nested objects/collections:
/**
* @Access({Access::GET, Access::SET})
* @Assert\Valid()
* @Assert\All({
* @Assert\Type(type="App\Entity\Product")
* })
*/
private $products;
Custom Logic:
Extend AutomatedBehaviorTrait or create custom traits for reusable logic:
trait Timestampable
{
use AutomatedBehaviorTrait;
/**
* @Access({Access::GET, Access::SET})
*/
private $createdAt;
public function __construct()
{
$this->createdAt = new \DateTime();
}
}
@Access to control serialization/deserialization. Combine with @Groups (from Symfony Serializer) for granular control.@ORM\Column annotations are present for database mapping.Annotation Processing Order:
@Assert) are loaded via Symfony’s annotation autoloader.config/packages/framework.yaml includes:
framework:
validation:
enabled: true
Circular Dependencies:
@Assert\Valid can cause infinite loops if not handled carefully.@MaxDepth or lazy-loading for complex hierarchies:
@Assert\Valid(maxDepth=1)
Private Properties:
__get/__set manually.@Access annotations instead of direct property access.Performance:
AnnotationReader supports caching).Symfony 5+ Deprecations:
Enable Annotation Debugging:
Add this to config/packages/dev/doctrine.yaml to log processed annotations:
doctrine:
orm:
annotations:
filter: ~
cache: false
Check logs for Accessible events.
Validate Annotations: Use Symfony’s validator to catch issues early:
php bin/console debug:validator App\Entity\User
Override Behavior:
Extend Antares\Accessible\Accessible to customize logic:
class CustomAccessible extends Accessible
{
protected function getDefaultAccess(): array
{
return [Access::GET, Access::SET, Access::CONSTRUCT];
}
}
Bind it via dependency injection.
Custom Access Levels:
Extend the Access enum or create a decorator for AutomatedBehaviorTrait:
trait CustomAccessTrait
{
public function __construct()
{
$this->addAccessRule('customRule', function () { /* ... */ });
}
}
Event Listeners:
Listen to accessible.post_process events to modify behavior dynamically:
// config/services.yaml
services:
App\EventListener\AccessibleListener:
tags:
- { name: kernel.event_listener, event: accessible.post_process, method: onPostProcess }
Integration with Laravel (via Symfony Bridge):
symfony/dependency-injection and symfony/http-kernel to integrate in Laravel.use Antares\AccessibleBundle\AntaresAccessibleBundle;
use Symfony\Component\HttpKernel\Kernel;
class AccessibleServiceProvider extends ServiceProvider
{
public function register()
{
$kernel = new Kernel('dev', true);
$kernel->boot();
$this->app->singleton('accessible', function () use ($kernel) {
return $kernel->getContainer()->get('accessible');
});
}
}
How can I help you explore Laravel packages today?