GithubHelp home page GithubHelp logo

degraciamathieu / clike Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 2.0 48 KB

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

PHP 100.00%
laravel php command-system design-pattern cli

clike's Introduction

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

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

clike's People

Contributors

degraciamathieu avatar peter279k avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

peter279k

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.