2017 © Pedro Peláez
 

library clike

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

image

degraciamathieu/clike

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

  • Wednesday, July 25, 2018
  • by DeGraciaMathieu
  • Repository
  • 1 Watchers
  • 2 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

, (*1)

Scrutinizer Code Quality Build Status Code Coverage PHP range Latest Version on Packagist , (*2)

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., (*3)

Installation

Run in console below command to download package to your project:, (*4)

composer require degraciamathieu/clike

Usage

Create a command

A command is a class that must implement the interface DeGraciaMathieu\Clike\Contracts\Command::class., (*5)

The following example is a valid command., (*6)

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., (*7)

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., (*8)

To finally execute the output method displaying the following result., (*9)

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

The Versions

25/07 2018

dev-master

9999999-dev https://github.com/degraciamathieu/clike

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

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

by De Gracia Mathieu

laravel cli design pattern command system

25/07 2018

v1.0.0

1.0.0.0 https://github.com/degraciamathieu/clike

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

  Sources   Download

MIT

The Requires

  • php >=7.1.0

 

The Development Requires

by De Gracia Mathieu

laravel cli design pattern command system