Download the PHP package maximebf/consolekit without Composer

On this page you can find all versions of the php package maximebf/consolekit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package consolekit

ConsoleKit

PHP 5.3+ library to create command line utilities.

Build Status

Example

In cli.php:

<?php

class HelloCommand extends ConsoleKit\Command
{
    public function execute(array $args, array $options = array())
    {
        $this->writeln('hello world!', ConsoleKit\Colors::GREEN);
    }
}

$console = new ConsoleKit\Console();
$console->addCommand('HelloCommand');
$console->run();

In the shell:

$ php cli.php hello
hello world!

More examples in example.php

Installation

The easiest way to install ConsoleKit is using Composer with the following requirement:

{
    "require": {
        "maximebf/consolekit": ">=1.0.0"
    }
}

Alternatively, you can download the archive and add the src/ folder to PHP's include path:

set_include_path('/path/to/src' . PATH_SEPARATOR . get_include_path());

ConsoleKit does not provide an autoloader but follows the PSR-0 convention.
You can use the following snippet to autoload ConsoleKit classes:

spl_autoload_register(function($className) {
    if (substr($className, 0, 10) === 'ConsoleKit') {
        $filename = str_replace('\\', DIRECTORY_SEPARATOR, trim($className, '\\')) . '.php';
        require_once $filename;
    }
});

Usage

Options parser

The default options parser parses an argv-like array. Items can be of the form:

When an option has no value, true will be used. If multiple key/value pairs with the same key are specified, the "key" value will be an array containing all the values.
If "--" is detected, all folowing values will be treated as a single argument

Example: the string "-a -bc --longopt --key=value arg1 arg2 -- --any text" will produce the following two arrays:

$args = array('arg1', 'arg2', '--any text');
$options = array('a' => true, 'b' => true, 'c' => true, 'longopt' => true, 'key' => 'value');

Creating commands

Any callbacks can be a command. It will receive three parameters: the arguments array, the options array and the console object.

function my_command($args, $opts, $console) {
    $console->writeln("hello world!");
}

Commands can also be defined as classes. In this case, they must inherit from ConsoleKit\Command and override the execute() method.

class MyCommand extends ConsoleKit\Command {
    public function execute(array $args, array $opts) {
        $this->writeln("hello world!");
    }
}

The ConsoleKit\Command class offers helper methods, check it out for more info.

Registering commands

Commands need to be registered in the console object using the addCommand() method (or addCommands()).

$console = new ConsoleKit\Console();
$console->addCommand('my_command'); // the my_command function
$console->addCommand('MyCommand'); // the MyCommand class
$console->addCommand(function() { echo 'hello!'; }, 'hello'); // using a closure
// or:
$console->addCommand('hello', function() { echo 'hello!'; }); // alternative when using a closure

Notice that in the last example we have provided a second argument which is an alias for a command. As closures have no name, one must be specified.

The command name for functions is the same as the function name with underscores replaced by dashes (ie. my_command becomes my-command).

The command name for command classes is the short class name without the Command suffix and "dashized" (ie. HelloWorldCommand becomes hello-world).

Running

Simply call the run() method of the console object

$console->run();
$console->run(array('custom arg1', 'custom arg2')); // overrides $_SERVER['argv']

Automatic help generation

The help command is automatically registered and provides help about available methods based on doc comments.
Check out example.php for example of available tags

$ php myscript.php help

Formating text

Colors

The ConsoleKit\Colors::colorize() method provides an easy way to colorize a text. Colors are defined as either a string or an integer (through constants of the Colors class).
Available colors: black, red, green, yellow, blue, magenta, cyan, white.

Foreground colors are also available in a "bold" variant. Suffix the color name with "+bold" or use the OR bit operator with constants.

echo Colors::colorize('my red text', Colors::RED);
echo Colors::colorize('my red text', 'red');

echo Colors::colorize('my red bold text', Colors::RED | Colors::BOLD);
echo Colors::colorize('my red bold text', 'red+bold');

echo Colors::colorize('my red text over yellow background', Colors::RED, Colors::YELLOW);

TextFormater

The ConsoleKit\TextFormater class allows you to format text using the following options:

Options can be defined using setOptions() or as the first parameter of the constructor.

$formater = new ConsoleKit\TextFormater(array('quote' => ' > '));
echo $formater->format("hello!");
// produces: " > hello"

Widgets

Dialog

Used to interact with the user

$dialog = new ConsoleKit\Widgets\Dialog($console);
$name = $dialog->ask('What is your name?');
if ($dialog->confirm('Are you sure?')) {
    $console->writeln("hello $name");
}

Box

Wraps text in a box

$box = new ConsoleKit\Widgets\Box($console, 'my text');
$box->write();

Produces:

********************************************
*                 my text                  *
********************************************

Progress bar

Displays a progress bar

$total = 100;
$progress = new ConsoleKit\Widgets\ProgressBar($console, $total);
for ($i = 0; $i < $total; $i++) {
    $progress->incr();
    usleep(10000);
}
$progress->stop();

All versions of consolekit with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package maximebf/consolekit contains the following files

Loading the files please wait ....