Installation
composer require coste/now-bundle
Add to config/app.php under providers:
Coste\NowBundle\CosteNowBundle::class,
First Use Case Inject the current date directly into a controller:
use Symfony\Component\HttpFoundation\Request;
use Coste\NowBundle\Annotation\Now;
class MyController extends AbstractController
{
/**
* @Now()
*/
public function index(DateTimeInterface $now)
{
return $this->json(['current_date' => $now->format('Y-m-d H:i:s')]);
}
}
Where to Look First
@Now annotation and its parameters (e.g., timezone, format).Coste\NowBundle\DependencyInjection\Compiler\NowPass.php for extension points.Timezone Handling Override the default timezone (UTC) via annotation:
/**
* @Now(timezone="Europe/Paris")
*/
public function index(DateTimeInterface $now) { ... }
Date Formatting Return formatted strings directly:
/**
* @Now(format="Y-m-d")
*/
public function getDate(string $formattedNow) { ... }
Integration with Symfony Components Combine with other bundles (e.g., SensioFrameworkExtraBundle) for mixed annotations:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Coste\NowBundle\Annotation\Now;
/**
* @Route("/date")
* @Now()
*/
public function index(DateTimeInterface $now) { ... }
Service Injection
Inject the NowService manually for reusable logic:
use Coste\NowBundle\Service\NowService;
public function __construct(private NowService $nowService) {}
public function index() {
$now = $this->nowService->getNow();
}
@Now for cases where the current time is truly dynamic (e.g., logs, time-sensitive operations). For static dates, use constants or config.NowService in tests:
$this->nowService->shouldReceive('getNow')->andReturn(new DateTime('2023-01-01'));
Annotation Parsing
config/packages/sensio_framework_extra.yaml:
framework:
validation: { enabled: true }
router:
default_uri_generator: router.default
templating:
engines: ['twig']
default_locale: en
annotations: true # <-- Critical for @Now to work
sensio_framework_extra is installed and configured.Circular Dependencies
@Now in services that are also injected into controllers may cause issues if not handled carefully.Timezone Ambiguity
timezone in annotations for clarity.Performance Overhead
@Now creates a new DateTime object per request. For high-traffic endpoints, cache the result if reused:
$now = $this->nowService->getNow(); // Cache this in a service if needed.
@Now is ignored, verify the compiler pass runs:
php bin/console debug:container --parameters | grep now
// In a custom compiler pass or event subscriber
dump($annotationReader->getMethodAnnotation(new \ReflectionMethod($controller, 'action'), Now::class));
config/packages/coste_now.yaml:
coste_now:
default_timezone: 'America/New_York'
default_format: 'c'
Custom Formats
Extend the NowService to support custom formatters:
// src/Service/CustomNowService.php
class CustomNowService extends NowService {
public function getCustomFormattedNow(): string {
return $this->getNow()->format('custom\format');
}
}
Bind it in services.yaml:
services:
App\Service\CustomNowService: '@Coste\NowBundle\Service\NowService'
Event Listeners Trigger actions on time-based events (e.g., daily reports):
use Symfony\Component\HttpKernel\Event\RequestEvent;
class DailyReportListener {
public function onKernelRequest(RequestEvent $event) {
if ($event->isMainRequest() && $this->isDailyReset()) {
// Trigger logic
}
}
private function isDailyReset(): bool {
return $this->nowService->getNow()->format('Y-m-d') === date('Y-m-d');
}
}
Database Integration
Use @Now to timestamp records:
/**
* @Now(format="Y-m-d H:i:s")
*/
public function createRecord(string $createdAt) {
$entity->setCreatedAt($createdAt);
$entityManager->persist($entity);
}
How can I help you explore Laravel packages today?