PHP Classes

Clike: Execute actions using the command design pattern

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 120 All time: 9,502 This week: 560Up
Version License PHP version Categories
clike 1.0.0The PHP License7.1Console, Design Patterns, PHP 7
Description 

Author

This package can execute actions using the command design pattern.

It provides a base class that can be extended to implement actions to be executed depending on conditions that the class can control.

Applications can extend the base class to implement functions that determine if the necessary conditions are satisfied before executing the desired action, execute the command actions, or return an error message when the conditions are not satisfied, or return the list of avaliable actions and their names.

Innovation Award
PHP Programming Innovation award nominee
July 2018
Number 2
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.

This package provides an implementation of the command design pattern to allow easy implementation of command line shell applications.

Manuel Lemos
Picture of DeGraciaMathieu
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 21 packages by
Country: France France
Age: ???
All time rank: 289780 in France France
Week rank: 4 Up1 in France France Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

Documentation

<p align="center"> <img src="https://i17.servimg.com/u/f17/11/13/61/32/clike10.png" width="270"> </p> <p align="center"> <a href="https://scrutinizer-ci.com/g/degraciamathieu/clike/?branch=master"><img src="https://scrutinizer-ci.com/g/DeGraciaMathieu/Clike/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a> <a href="https://travis-ci.org/DeGraciaMathieu/Clike"><img src="https://travis-ci.org/DeGraciaMathieu/Clike.svg?branch=master" alt="Build Status"></a> <a href="https://scrutinizer-ci.com/g/DeGraciaMathieu/Clike/?branch=master"><img src="https://scrutinizer-ci.com/g/DeGraciaMathieu/Clike/badges/coverage.png?b=master" alt="Code Coverage"></a> <img src="https://img.shields.io/travis/php-v/DeGraciaMathieu/Clike.svg" alt="PHP range"> <a href="https://packagist.org/packages/degraciamathieu/clike"><img src="https://img.shields.io/packagist/v/degraciamathieu/clike.svg?style=flat-square" alt="Latest Version on Packagist"></a> <a href='https://packagist.org/packages/degraciamathieu/clike'><img src='https://img.shields.io/packagist/dt/degraciamathieu/clike.svg?style=flat-square' /></a> </p>

DeGraciaMathieu/Clike

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..."
//     ]
// ]

  Files folder image Files (24)  
File Role Description
Files folder imagesrc (3 files, 4 directories)
Files folder imagetests (4 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (24)  /  src  
File Role Description
Files folder imageContracts (3 files)
Files folder imageExceptions (1 file)
Files folder imageLines (6 files)
Files folder imageOutputs (2 files)
  Plain text file Command.php Class Class source
  Plain text file Interpreter.php Class Class source
  Plain text file Terminal.php Class Class source

  Files folder image Files (24)  /  src  /  Contracts  
File Role Description
  Plain text file Command.php Class Class source
  Plain text file Line.php Class Class source
  Plain text file Output.php Class Class source

  Files folder image Files (24)  /  src  /  Exceptions  
File Role Description
  Plain text file UnknowCommand.php Class Class source

  Files folder image Files (24)  /  src  /  Lines  
File Role Description
  Plain text file Error.php Class Class source
  Plain text file Info.php Class Class source
  Plain text file Line.php Class Class source
  Plain text file LineConstants.php Class Class source
  Plain text file Success.php Class Class source
  Plain text file Warning.php Class Class source

  Files folder image Files (24)  /  src  /  Outputs  
File Role Description
  Plain text file Command.php Class Class source
  Plain text file Unauthorize.php Class Class source

  Files folder image Files (24)  /  tests  
File Role Description
  Plain text file Clear.php Class Class source
  Plain text file CommandIntegration.php Class Class source
  Plain text file LinesTest.php Class Class source
  Plain text file TerminalIntegration.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:120
This week:0
All time:9,502
This week:560Up