PHP Classes
elePHPant
Icontem

Clike: Execute actions using the command design pattern

Recommend this page to a friend!
  Info   View files Documentation   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-07-26 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 87 This week: 39All time: 9,096 This week: 27Up
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
  Performance   Level  
Name: DeGraciaMathieu <contact>
Classes: 4 packages by
Country: France France
Innovation award
Innovation award
Nominee: 2x

Details

<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  
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  /  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  /  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  /  src  /  Exceptions  
File Role Description
  Plain text file UnknowCommand.php Class Class source

  Files folder image Files  /  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  /  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  /  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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:87
This week:39
All time:9,096
This week:27Up