This package is abandoned and no longer maintained. No replacement package was suggested.

This package allows you to run a class with a design pattern similar to a command system

v1.2.0 2020-07-01 08:46 UTC

This package is auto-updated.

Last update: 2023-03-29 00:28:15 UTC


README

68747470733a2f2f6931372e73657276696d672e636f6d2f752f6631372f31312f31332f36312f33322f636c696b6531302e706e67

Scrutinizer Code Quality Build Status Code Coverage PHP range Latest Version on Packagist 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64656772616369616d6174686965752f636c696b652e7376673f7374796c653d666c61742d737175617265

DeGraciaMathieu/Clike

The command design pattern is useful to create modular components for a command line shell application that can be expanded with more functionality implemented later by separate modules.

Installation

Run in console below command to download package to your project:

composer require degraciamathieu/clike

Usage

Create a command

A command is a class that must implement the interface DeGraciaMathieu\Clike\Contracts\Command::class.

The following example is a valid command.

use DeGraciaMathieu\Clike\Lines;
use DeGraciaMathieu\Clike\Contracts;

class Clear implements Contracts\Command {

    /**
     * Get the command description
     */
    public function description() :string
    {
        return 'Command description...';
    }

    /**
     * Check if the command is executable
     */
    public function authorized() :bool
    {
        return true;
    }

    /**
     * Bind of this command
     */
    public function binding() :string
    {
        return '/clear';
    }

    /**
     * Code executed by this command
     */
    public function process() :void
    {
        //
    }

    /**
     * Output of this command
     */
    public function output() :array
    {
        return [
            new Lines\Info('Output text...'),
        ];
    }
}

Execute a command

Now let's play with our Clear command.

use DeGraciaMathieu\Clike\Command;

$command = new Command();
$command->execute(new Clear());

After checking that we can use this command with the authorized method this code will execute the process method of our command.

To finally execute the output method displaying the following result.

// array:2 [
//   "timestamp" => 1531339693
//   "lines" => array:1 [
//     0 => array:2 [
//       "type" => "info"
//       "content" => "Output text..."
//     ]
//   ]
// ]

Execute a command with Terminal

use DeGraciaMathieu\Clike\Terminal;

$terminal = new Terminal([
    Clear::class,
]);
$terminal->execute('/clear');

Retrieve all available commands with Terminal

use DeGraciaMathieu\Clike\Terminal;

$terminal = new Terminal([
    Clear::class,
]);
$terminal->getAvailableCommands();

// [
//     [
//       "binding" => "/clear"
//       "description" => "Command description..."
//     ]
// ]