bro-world/core-bundle
bro-world/core-bundle is a Laravel core bundle providing shared application foundations: common helpers, base classes, and reusable components to standardize project structure and speed up development across Bro World services.
Installation:
composer require bro-world/core-bundle
Ensure your composer.json includes:
"require": {
"bro-world/core-bundle": "^0.2.5",
"lexik/jwt-auth-bundle": "^2.16", # Auto-added by the bundle
"automapper/automapper": "^2.0" # Auto-added by the bundle
}
Bundle Registration:
Add to config/bundles.php:
BroWorld\CoreBundle\BroWorldCoreBundle::class => ['all' => true],
First Use Case: Use the UTCDateTimeType for Doctrine entities:
use BroWorld\CoreBundle\Doctrine\Types\UTCDateTimeType;
#[ORM\Entity]
class MyEntity {
#[ORM\Column(type: UTCDateTimeType::NAME)]
private ?\DateTimeInterface $createdAt = null;
}
AutoMapper Integration:
Define mappings in config/packages/automapper.php:
automapper:
mappings:
App\Entity\User:
to: App\DTO\UserDTO
type: entity_to_dto
Use in services:
$mapper = $this->get(MapperInterface::class);
$dto = $mapper->map($entity, UserDTO::class);
JWT Authentication:
Configure Lexik JWT in config/packages/lexik_jwt_auth.yaml:
lexik_jwt_authentication:
secret_key: '%env(JWT_SECRET_KEY)%'
public_key: '%env(JWT_PUBLIC_KEY)%'
Secure routes with annotations:
#[Route('/api/protected', methods: ['GET'])]
#[IsGranted('ROLE_USER')]
public function protectedRoute(): JsonResponse { ... }
UTC DateTime Handling:
Use UTCDateTimeType for consistent timezone handling:
$entity->setCreatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
UTCDateTimeType for custom logic:
use BroWorld\CoreBundle\Doctrine\Types\UTCDateTimeType;
use Doctrine\DBAL\Types\Type;
Type::addType('custom_utc_datetime', CustomUTCDateTimeType::class);
# config/packages/bro_world_core.yaml
bro_world_core:
api_proxies:
stripe:
base_uri: 'https://api.stripe.com/v1'
auth: 'Bearer %env(STRIPE_SECRET_KEY)%'
AutoMapper Alias Fix:
Ensure automapper service alias is correctly registered in services.yaml:
services:
Automapper\Automapper:
alias: automapper
(Fixed in v0.2.4, but may still cause issues in older setups.)
UTCDateTimeType Compatibility:
\DateTimeInterface (fixed in v0.2.2).JWT Bundle Dependency:
JWT_SECRET_KEY and JWT_PUBLIC_KEY are set in .env.AutoMapper Errors:
Check if mappings are registered in automapper.php and services are autowired.
php bin/console debug:automapper
UTCDateTimeType Issues:
Validate database column types (e.g., DATETIME(6) for MySQL).
ALTER TABLE my_table MODIFY created_at DATETIME(6) NULL;
Custom Doctrine Types:
Extend UTCDateTimeType for project-specific logic:
class CustomUTCDateTimeType extends UTCDateTimeType {
public function convertToDatabaseValue($value, AbstractPlatform $platform) {
// Custom conversion logic
return parent::convertToDatabaseValue($value, $platform);
}
}
API Proxy Extensions:
Add custom proxy handlers in bro_world_core.yaml:
bro_world_core:
api_proxies:
custom_service:
handler: App\Service\CustomProxyHandler
Event Subscribers:
Subscribe to Doctrine lifecycle events for UTCDateTimeType:
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
class UTCDateTimeSubscriber implements EventSubscriber {
public function getSubscribedEvents() {
return [Events::prePersist, Events::preUpdate];
}
public function prePersist(LifecycleEventArgs $args) {
$entity = $args->getObject();
if (method_exists($entity, 'setCreatedAt')) {
$entity->setCreatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
}
}
}
How can I help you explore Laravel packages today?