Installation
Add the bundle to your composer.json:
composer require damianociarla/dcs-user-core-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Damianociarla\DCSUserCoreBundle\DCSUserCoreBundle::class => ['all' => true],
];
First Use Case: User Creation Inject the factory service into a controller or service:
use Damianociarla\DCSUserCoreBundle\Factory\UserFactoryInterface;
class UserController extends Controller
{
public function __construct(private UserFactoryInterface $userFactory) {}
public function createUser()
{
$user = $this->userFactory->create(['email' => 'user@example.com']);
// Handle the user object (not yet persisted)
}
}
Event Listeners
Configure listeners for dcs_user.manager.save and dcs_user.manager.delete events in config/services.yaml:
services:
App\EventListener\UserSaveListener:
tags:
- { name: kernel.event_listener, event: dcs_user.save, method: onUserSave }
User Creation & Persistence
UserFactoryInterface to create a user object.dcs_user.save event to trigger persistence logic:
$this->eventDispatcher->dispatch(new UserSaveEvent($user));
User Deletion
dcs_user.delete event with the user ID:
$this->eventDispatcher->dispatch(new UserDeleteEvent($userId));
Repository Integration
Damianociarla\DCSUserCoreBundle\Repository\UserRepositoryInterface for custom queries:
class DoctrineUserRepository implements UserRepositoryInterface
{
public function findByEmail(string $email): ?User
{
return $this->entityManager->getRepository(User::class)
->findOneBy(['email' => $email]);
}
}
services:
Damianociarla\DCSUserCoreBundle\Repository\UserRepositoryInterface: '@App\Repository\DoctrineUserRepository'
dcs_user.* events.UserFactoryInterface or UserRepositoryInterface over instantiating directly.No Built-in Persistence
No listeners found for event "dcs_user.save".
Repository Interface Only
UserRepositoryInterface is abstract. Implementations must be provided manually (e.g., Doctrine, Eloquent).findAll() unless implemented.Event Naming Conflicts
UserSaveEvent) match the dispatched event names (dcs_user.save).Check Event Dispatching:
Use Symfony’s EventDispatcher debug tool or add a DEBUG listener to verify events:
public function onKernelEvent(GetResponseEvent $event)
{
if ($event->isMasterRequest()) {
$this->logger->debug('Dispatched events:', $event->getRequest()->attributes->get('_controller_events'));
}
}
Validate User Data Early:
Use Symfony’s ValidatorInterface before dispatching events to avoid partial state issues:
$errors = $validator->validate($user);
if (count($errors) > 0) {
throw new \RuntimeException('User validation failed');
}
Custom User Classes Extend the factory to support custom user entities:
class CustomUserFactory implements UserFactoryInterface
{
public function create(array $data): User
{
return new CustomUser($data['email'], $data['name']);
}
}
Pre/Post-Event Logic Add logic before/after events using event subscribers:
class UserPreSaveSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'dcs_user.save' => 'onPreSave',
];
}
public function onPreSave(UserSaveEvent $event)
{
$event->getUser()->setCreatedAt(new \DateTime());
}
}
Repository Decorators Decorate the repository to add cross-cutting concerns (e.g., logging):
class LoggingUserRepository implements UserRepositoryInterface
{
public function __construct(private UserRepositoryInterface $decorated) {}
public function findByEmail(string $email): ?User
{
$this->logger->info('Finding user by email', ['email' => $email]);
return $this->decorated->findByEmail($email);
}
}
config/packages/dcs_user_core.yaml:
dcs_user_core:
factory: App\Factory\CustomUserFactory
repository: App\Repository\CustomUserRepository
priority in event tags to control listener order:
tags:
- { name: kernel.event_listener, event: dcs_user.save, method: onSave, priority: 10 }
How can I help you explore Laravel packages today?