Replacing dot-console with dot-cli based on laminas-cli

Implementing dot-cli in Your Application

Dotkernel's dot-cli package comes as a replacement for dot-console, which was abandoned after Laminas abandoned their laminas-console package, that dot-console was based on.

Setup

Install Package

Run the following command in your application's root directory:

composer require dotkernel/dot-cli

Register ConfigProvider

Open your application's config/config.php file and add Dot\Cli\ConfigProvider::class, under the DK packages comment.

Create Bootstrap File

Locate the provided vendor/dotkernel/dot-cli/bin/cli.php and copy it into your application's bin directory. This is the file you will execute your commands through.

Create Config File

Locate the provided vendor/dotkernel/dot-cli/config/autoload/cli.global.php and copy it into your application's config/autoload directory. This file already contains a sample command (demo:command), that serves as an example on creating and registering new commands. For more information on configuring/chaining commands, see the laminas-cli documentation.

Usage

Run the following command in your application's root directory:

php ./bin/cli.php

The output should look similar to this, containing information on how to start using dot-cli:

Dotkernel CLI 1.0.0

Usage:
  command

Options:
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help          Display help for a command
  list          List commands
 demo
  demo:command  Demo command description.

Set Up Command as Cronjob

Open your crontab and add the following line:

* * * * {PATH_TO_PHP_EXECUTABLE} {PATH_TO_APPLICATION}/bin/cli.php demo:command

Where {PATH_TO_PHP_EXECUTABLE} needs to be replaced with the full path to the PHP executable and {PATH_TO_APPLICATION} with the full path to your application's root directory.

FileLocker

dot-cli has a built-in locking system, called FileLocker, enabled by default. This feature prevents multiple calls to the same command overlapping each other by making sure the latter calls won't run until the former one is finished. You can toggle this by modifying the previously created config/autoload/cli.global.php under FileLockerInterface::class -> enabled.

Frequently Asked Questions

Why was dot-console replaced by dot-cli? +

dot-console was abandoned after Laminas abandoned its laminas-console package, which dot-console was based on, so dot-cli was created as its replacement.

How do you install dot-cli in an application? +

Run `composer require dotkernel/dot-cli` in the application's root directory, then register `Dot\Cli\ConfigProvider::class,` in `config/config.php`, and copy the provided `bin/cli.php` bootstrap file and `config/autoload/cli.global.php` config file into the application.

What does the provided cli.global.php config file contain? +

It already contains a sample command (`demo:command`) that serves as an example on how to create and register new commands.

How do you run a dot-cli command as a cronjob? +

Add a line to your crontab such as `* * * * {PATH_TO_PHP_EXECUTABLE} {PATH_TO_APPLICATION}/bin/cli.php demo:command`, replacing the placeholders with the full path to the PHP executable and the full path to the application's root directory.

What is FileLocker? +

FileLocker is a built-in locking system in dot-cli, enabled by default, that prevents multiple calls to the same command from overlapping by making sure later calls won't run until the earlier one finishes. It can be toggled in `config/autoload/cli.global.php` under `FileLockerInterface::class -> enabled`.