zenify / doctrine-filters
Doctrine Filters integration into Nette
Requires
- php: ^7.0
- doctrine/orm: ~2.5
- nette/application: ^3.0
- nette/di: ^3.0
- symplify/symfony-event-dispatcher: ~1.2
Requires (Dev)
- nette/bootstrap: ^3.0
- phpunit/phpunit: ^5.6
- symfony/console: ~3.0
- tracy/tracy: ~2.4
- zenify/coding-standard: ^4.0
README
What are Doctrine Filters? Check these few slides or see Usage to get the knowledge.
They are present in Doctrine by default. This package only simplifies their use in modular application.
Install
$ composer require zenify/doctrine-filters
Register extension in config.neon
:
extensions: - Zenify\DoctrineFilters\DI\FiltersExtension - Symplify\SymfonyEventDispatcher\Adapter\Nette\DI\SymfonyEventDispatcherExtension # Kdyby\Doctrine or another Doctrine to Nette implementation
Usage
Let's create our first filter, which will hide all deleted items on front. So called "soft delete" - data remains in database, but are filtered out in frontend.
First, we create class implementing Zenify\DoctrineFilters\Contract\FilterInterface.
use Doctrine\ORM\Mapping\ClassMetadata; use Zenify\DoctrineFilters\Contract\FilterInterface; final class SoftdeletableFilter implements FilterInterface { public function addFilterConstraint(ClassMetadata $entity, string $alias) : string { if ($entity->getReflectionClass()->hasProperty('isDeleted')) { return "$alias.isDeleted = 0"); } return ''; } }
Then register as service:
# app/config/config.neon services: - SoftdeletableFilter
And that's it!
Limit Access by Role
The management wants to show deleted content only to logged user with admin role.
To setup condition, we just implement Zenify\DoctrineFilters\Contract\ConditionalFilterInterface.
use Nette\Security\User; use Zenify\DoctrineFilters\Contract\ConditionalFilterInterface; final class SoftdeletableFilter implements ConditionalFilterInterface { /** * @var User */ private $user; public function __construct(User $user) { $this->user = $user; } public function addFilterConstraint(ClassMetadata $entity, string $alias) : string { // same as above } public function isEnabled() : bool { if ($this->user->isLoggedIn() && $this->user->hasRole('admin')) { return FALSE; } return TRUE; } }
Voilá!
Testing
composer check-cs vendor/bin/phpunit
Contributing
Rules are simple:
- new feature needs tests
- all tests must pass
- 1 feature per PR
We would be happy to merge your feature then!