I'm currently building out an application that uses Slim v4
and PHP-DI
for auto-wiring dependencies. This is fantastic besides the fact that I need to build a CRON that utilizes some classes that are auto-wired. Here's my example:
class NotificationService
{
private $notification;
public function __construct( NotificationFactory $notificationFactory )
{
$this->notification = $notificationFactory;
}
}
In this case the Notification Service is auto-wired to use the Notification Factory, which is great. But I need to create a CRON that utilizes the NotificationService for sending notifications:
class TimedNotification
{
private $notification;
public function __construct( NotificationService $notificationService )
{
$this->notification = $notificationService;
}
public function sendNotification(): bool
{
$sent = $this->notification->sendMessage( 'This message is a test.' );
return $sent;
}
}
// Separate File
$timedNotification = new TimedNotification();
$timedNotification->sendMessage();
I'd like to be able to use a CRON to call the Timed notification file, or a separate file that has the TimedNotification instantiation like 0 23 * * * /my/dir/mycrons && php timednotifications.php
Is there a tried and true way of performing this, or would i have to build the entire app in a file to run the CRON ?
To autowire the dependencies, you have to use the DI container.
Example:
Output:
In a real world application a Symfony Console would be the "standard" way to go.
File:
bin\console.php