It is possible to implement a new cron job by creating an apposite PHP file, containing a new class that inherits the methods defined by the E4J\VikRestaurants\Cron\CronJob abstraction.

The class can be named as you prefer, there are no restrictions applied.

Further details about the implementable methods are specified from the related articles under this handbook.

<?php
// No direct access
defined('_JEXEC') or die('No script kiddies please!');

use E4J\VikRestaurants\Cron\CronJob;
use E4J\VikRestaurants\Cron\Installable;
use E4J\VikRestaurants\Cron\Status;

/**
 * Test cron implementation.
 * 
 * NOTE: CronJob abstract class implements by default the following interfaces: Configurable, Runnable.
 */
class TestCron extends CronJob implements Installable
{
    /**
     * @inheritDoc
     * 
     * @see Configurable
     */
    public function getTitle()
    {
        return 'Test Cron';
    }

    /**
     * @inheritDoc
     * 
     * @see Configurable
     */
    public function getDescription()
    {
        return 'Testing the implementation of a custom cron job.';
    }

    /**
     * @inheritDoc
     * 
     * @see Configurable
     */
    public function getForm()
    {
        return [];
    }

    /**
     * inheritDoc
     * 
     * @see Runnable
     */
    public function isSupported()
    {
        return true;
    }

    /**
     * @inheritDoc
     * 
     * @see Runnable
     */
    protected function execute(\JRegistry $args, Status $status)
    {
        /**
         * @todo do stuff here
         */
    }

    /**
     * @inheritDoc
     * 
     * @see Installable
     */
    public function install(int $id)
    {
        /**
         * @todo perform installation routine here
         */
    }

    /**
     * @inheritDoc
     * 
     * @see Installable
     */
    public function uninstall(int $id)
    {
        /**
         * @todo perform uninstallation routine here
         */
    }
}

Where should I place that file?

You can place that file wherever you want and name it as you wish. Just remember to automatically load it during the registration of the cron job, as described by the onPrepareCronDispatcher hook.

Let's take the following plugin structure.

  • plg_vikrestaurants_myplugin/
    • myplugin.php
    • crons/
      • custom_cron_one.php
      • custom_cron_two.php

According to the structure of these files, both the cron jobs can be supported by using the following code, which should be placed within the main myplugin.php file.

// subscribe to the hook used to set up the cron dispatcher of VikRestaurants
public function onPrepareCronDispatcher($cronDispatcher)
{
    // register a cron provider to create the first job
    $cronDispatcher->registerCronProvider('custom_cron_one', function(int $id) {
        // autoload the file ./crons/custom_cron_one.php, which should declare a class named as:
        // CustomCronOne
        require_once dirname(__FILE__) . '/crons/custom_cron_one.php';
        // instantiate the class
        return new CustomCronOne($id);
    });

    // register a cron provider to create the second job
    $cronDispatcher->registerCronProvider('custom_cron_two', function(int $id) {
        // autoload the file ./crons/custom_cron_two.php, which should declare a class named as:
        // CustomCronTwo
        require_once dirname(__FILE__) . '/crons/custom_cron_two.php';
        // instantiate the class
        return new CustomCronTwo($id);
    });
}
Last Update: One day ago.
Helpful?