PHP code example of litvinab / cron-event

1. Go to this page and download the library: Download litvinab/cron-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

litvinab / cron-event example snippets


cron:
    resource: "@CronEventBundle/Resources/config/routing.yml"
    prefix:   /cron

// get cron manager
$cronManager = $this->get('cron_event.manager');

// set timer with: human name, name, period 
// name of the symfony event: `cron_event.` + name
$timer = $cronManager->setTimer('My timer', 'test_timer', 60);

services:
     app.subscriber.cron:
          class: AppBundle\EventSubscriber\CronSubscriber
          calls:
            - [setLogger, [@cron_event.logger]]
          tags:
            - { name: kernel.event_subscriber }


namespace AppBundle\EventSubscriber;

use Litvinab\Bundle\CronEventBundle\Events\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
 * Class CronSubscriber
 */
class CronSubscriber implements EventSubscriberInterface
{
    /**
     * @var Cron Logger
     */
    private $logger;

    /**
     * Set cron logger
     *
     * @param $logger
     */
    public function setLogger($logger)
    {
        $this->logger = $logger;
    }

    /**
     * Get subscribed events
     *
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return array(
            'cron_event.test_timer' => array('onCronTestEvent', 0)
        );
    }

    /**
     * Test event
     *
     * @param CronEvent $cronEvent
     */
    public function onCronTestEvent(CronEvent $cronEvent)
    {
        // confirm that event is executed
        $this->logger->addInfo('onCronTestEvent');

        // delete cron event from DB
        $cronEvent->delete();
    }
}