locomotivemtl/charcoal-contrib-command

Charcoal contrib to handle dynamic cron scripts as commands.

0.1.5.1 2021-07-05 15:59 UTC

This package is auto-updated.

Last update: 2024-12-06 01:31:55 UTC


README

License Latest Stable Version Code Quality Coverage Status Build Status

A charcoal contrib used to provide dynamic cron scripts, easily schedulable.

Table of Contents

Installation

The preferred (and only supported) method is with Composer:

$ composer require locomotivemtl/charcoal-contrib-command

Including the modules

Just add the following module in your json configuration file:

"charcoal/command/command": {},

Setting a cron

Script command/process-queue is added with the command's module.

* * * * * /path/to/bin/php /path/to/vendor/bin/charcoal command/process-queue > /dev/null 2>&1

Dependencies

Required

PSR

--TBD--

Service Provider

The following services are provided with the use of charcoal-contrib-command

Services

  • command/queue-stack: instance of Command\Service\CommandQueuer

Configuration

Every bit of configuration for the command contrib should be under the namespace command. Configurations allows the possibility to define options for the logger, such as the handlers, processors or formatters. All default configurations are visible in the Charcoal\Command\Logger\Config\CommandLoggerConfig class.

Defaults

    "command": {
        "logger": {
            "level": "debug",
            "active": true,
            "handlers": {
                "charcoal/command/logger/handler/charcoal": {
                    "model": "charcoal/command/log",
                    "formatter": {
                        "charcoal/command/logger/formatter/command": {}
                    }
                }
            }
        }
    }

The handlers should be resolvable by the logger handler generic factory. In the given example, we see Charcoal\Command\Logger\Handler\CharcoalHandler class. The options defined with the handler will be passed in the handler's constructor method.

The formatters should be resolvable by the logger formatter generic factory. In the given example, we see Charcoal\Command\Logger\Formatter\Command. The options defined with the formatter will be passed in the formatter's constructor method.

The class should be resolvable by the model factory. In the given example, we see Charcoal\Command\Log.

Usage

Creating a command class

A command class should extend the AbstractCommand class. You can use the setDependencies public method to access any dependencies you might need. The public method execute is called when everything is ready.

Interface

  • public AbstractCommand::__invoke ( array $arguments ): Sets arguments, execute command and log results
  • public AbstractCommand::setDependencies ( Container $container ): Allows to set any required dependencies
  • protected AbstractCommand::log (): Called upon execution. Logs according to the given options.
  • protected AbstractCommand::setSuccess ( boolean $success ): Defines if the scripts was executed successfully or not.
  • abstract protected AbstractCommand::execute (): The actual command goes in there.

Example

namespace Charcoal\Cache\Command;

[...]

/**
 * Cache clearer command
 */
class ClearCommand extends AbstractCommand
{
    use CachePoolAwareTrait;

    /**
     * @param array $arguments
     */
    public function setDependencies(Container $container)
    {
        parent::setDependencies($container);
        $this->setCachePool($container['cache']);
    }

    /**
     * @return mixed|void
     */
    public function execute()
    {
        $success = $this->cachePool()->clear();
        $this->setSuccess($success);
    }
}

Back-end form

Use the CommandQueue model to queue a new command.

Available Service

Method

public QueueStack::enqueue ( array $data ) : QueueStack

Parameters

Array data

  • command: Required. Resolvable Command class string.
  • processingDate: Optional. Date when the command should be run. Accept any valid DateTime format or a DateTime object. Defaults to now
  • arguments: Options. Array of arguments to be passed to the command constructor method.
Example
    $stack = $container['command/queue-stack'];
    $stack->enqueue([
        'command' => 'charcoal/cache/command/clear',
        'processingDate' => 'NOW +1 day'
    ]);

The default processing date is "now".

Development

To install the development environment:

$ composer install

To run the scripts (phplint, phpcs, and phpunit):

$ composer test

API Documentation

Development Dependencies

  • [php-coveralls/php-coveralls][phpcov]
  • [phpunit/phpunit][phpunit]
  • [squizlabs/php_codesniffer][phpcs]

Coding Style

The charcoal-contrib-command module follows the Charcoal coding-style:

Coding style validation / enforcement can be performed with composer phpcs. An auto-fixer is also available with composer phpcbf.

Credits

License

Charcoal is licensed under the MIT license. See LICENSE for details.