PHPackages                             box-project/console - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. box-project/console

AbandonedArchivedLibrary[CLI &amp; Console](/categories/cli)

box-project/console
===================

A console application assembled through dependency injection.

0.2.6(10y ago)91.1k3[1 issues](https://github.com/box-project/console/issues)[1 PRs](https://github.com/box-project/console/pulls)MITPHPPHP &gt;=5.4

Since May 18Pushed 10y ago1 watchersCompare

[ Source](https://github.com/box-project/console)[ Packagist](https://packagist.org/packages/box-project/console)[ Docs](http://box-project.org)[ RSS](/packages/box-project-console/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (15)Versions (11)Used By (0)

[![Build Status](https://camo.githubusercontent.com/61aeefb04f151d810ac3f1a7d2b0f2aed767296cc573d3949c3d4431aa7978b1/68747470733a2f2f7472617669732d63692e6f72672f626f782d70726f6a6563742f636f6e736f6c652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/box-project/console)[![Latest Stable Version](https://camo.githubusercontent.com/f90f681df2aff61a2520be64abd3bbead7d2bef3d4b7a3af5601a168a959d703/68747470733a2f2f706f7365722e707567782e6f72672f626f782d70726f6a6563742f636f6e736f6c652f762f737461626c652e706e67)](https://packagist.org/packages/box-project/console)[![Latest Unstable Version](https://camo.githubusercontent.com/127bee37be41f9ecdaa41b5678a814627a96e1b9a709a3474cfcfdfbcc89a95d/68747470733a2f2f706f7365722e707567782e6f72672f626f782d70726f6a6563742f636f6e736f6c652f762f756e737461626c652e706e67)](https://packagist.org/packages/box-project/console)[![Total Downloads](https://camo.githubusercontent.com/392564712ec13957926452fe0dd8c2f67c7fc4355d6eee54d96b2ae8f15943d2/68747470733a2f2f706f7365722e707567782e6f72672f626f782d70726f6a6563742f636f6e736f6c652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/box-project/console)

Console
=======

[](#console)

```
composer require box-project/console

```

Console simplifies the process of building a command line application using the [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) design pattern. Input and output management is already handled for you. You simply need to create your commands and register each as a service.

```
use Box\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
$application = new Application($container);

$container->compile()
$application->run();
```

Requirements
------------

[](#requirements)

- `kherge/file` ~1.3
- `herrera-io/object-storage` ~1.0
- `symfony/config` ~2.5
- `symfony/console` ~2.5
- `symfony/dependency-injection` ~2.5
- `symfony/yaml` ~2.5

### Suggested

[](#suggested)

- `symfony/event-dispatcher` ~2.5
- `symfony/expression-language` ~2.5
- `symfony/framework-bundle` ~2.5

Getting Started
---------------

[](#getting-started)

You need to be familiar with some of the third-party libraries that are used by Console in order to be able to make sense of anything. These libraries come from [Symfony](https://symfony.com/), an open source web application framework. For your convenience, the documentation for the most relevant libraries are linked below.

- [Console](http://symfony.com/doc/current/components/console/index.html) - Manages all aspects of the console (input and output). When you author your commands you will be targeting this library.
- [DependencyInjection](http://symfony.com/doc/current/components/dependency_injection/index.html) - Responsible for wiring all of the dependencies together. Also makes it possible to alter the defaults provided by the library to better suit your needs.
- [EventDispatcher](http://symfony.com/doc/current/components/event_dispatcher/index.html) - A simple implementation of the [mediator](http://en.wikipedia.org/wiki/Mediator_pattern) pattern. Enables events in the **Console** library and makes it possible to add a plugin system to your console application.

### Creating an Application

[](#creating-an-application)

```
use Box\Component\Console\Application;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
```

Before a new application can be created, a dependency injection container will be needed. While any instance of `ContainerInterface` may be used, we will be using an instance of `ContainerBuilder` (more detail on why later).

```
$container = new ContainerBuilder();
```

With the container, a new `Application` instance can now be created.

```
$app = new Application($container);
```

When an instance of `ContainerBuilder` is provided to `Application`, it will automatically set parameters and register services needed to run the console application. `Application` will not set parameters or register services that already exist. It is important to note that only instances of `ContainerBuilder`will cause `Application` to set the default parameters and register the default services.

### Running an Application

[](#running-an-application)

Before we can begin process of running the console, the container must first be compiled. [Compiling](http://symfony.com/doc/current/components/dependency_injection/compilation.html) the container allows for some last minute processes to occur.

```
$container->compile();
```

With the compiled container, the application is ready to run.

```
$app->run();
```

When the code in the examples above are run from a script in the command line, the following output will be shown. It may be important to note that the output may vary slightly depending on the age of the documentation and what libraries were installed in addition to **Console**.

```
Console Tool

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message
  --quiet          -q Do not output any message
  --verbose        -v|vv|vvv Increase the verbosity of messages: [...snip...]
  --version        -V Display this application version
  --ansi              Force ANSI output
  --no-ansi           Disable ANSI output
  --no-interaction -n Do not ask any interactive question

Available commands:
  help               Displays help for a command
  list               Lists commands
config
  config:current     Displays the current configuration
  config:list        Lists the registered extensions
  config:reference   Displays a configuration reference
container
  container:debug    Displays current services for an application
debug
  debug:container    Displays current services for an application

```

Using the Container
-------------------

[](#using-the-container)

`Application` is designed around the use of the container. All functionality that is provided by **Console** can be found as a parameter or service within the container. As a result, all changes to the console (adding commands, adding helpers, changing defaults, etc) must also occur through the container.

### Loading Resources

[](#loading-resources)

In order to make changes to the container, the loaders provided by the **DependencyInjection** library must be used. More information about how to use the [DI loaders](http://symfony.com/doc/current/components/dependency_injection/introduction.html#setting-up-the-container-with-configuration-files) can be found on Symfony's website. While you may use any compatible loader, **Console** will only officially support XML and YAML for file-based loading. PHP is also supported, but not in conjunction with the bundled commands or loaders.

#### Loading `.dist` Files

[](#loading-dist-files)

In addition to the standard loaders, **Console** provides its own loader for special cases. Many applications make use of files that end with `.dist`. This file extension is used to indicate that the file is part of the distribution. A user may then make a copy of the file, drop the `.dist` extension, and use their version of the file with their software.

The following example will support the loading of XML and YAML files, with or without the `.dist` file extension.

```
use Box\Component\Console\Loader\Resource;
use Box\Component\Console\Loader\ResourceCollection;
use Box\Component\Console\Loader\ResourceCollectionLoader;
use Box\Component\Console\Loader\ResourceSupport;
use Symfony\Component\Config\Exception\FileLoaderLoadException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

// load files from the current directory
$locator = new FileLocator('.');

// create a loader for xml and yaml files
$loader = new ResourceCollectionLoader(
    new LoaderResolver(
        array(
            new XmlFileLoader($container, $locator),
            new YamlFileLoader($container, $locator)
        )
    )
);

// load the first available file from a collection of possible resources
$loader->load(
    new ResourceCollection(
        array(
            new Resource('example.xml'),
            new ResourceSupport('example.xml.dist', 'example.xml'),
            new Resource('example.yml'),
            new ResourceSupport('example.yml.dist', 'example.yml')
        )
    )
);
```

As the name implies, the `ResourceCollection` class manages a collection of `Resource` instances. Because of how the standard file loaders behave when determining support for files, `ResourceSupport` is used to map an unsupported file extension (e.g. `.yml.dist`) a supported one (e.g. `.yml`). The loader, `ResourceCollectionLoader`, will then iterate through the collection attempting load each resource until one is successfully loaded. If the first resource in the collection fails to load due to it not existing, the next will be attempted. This iteration will continue until the list is exhausted, or an error is found while processing an available resource.

In the example above, an exception is thrown if none of the resources in the collection exist. To optionally load a resource, without having an exception thrown, the `loadOptional()` method should be used.

```
$loader->loadOptional(
    new ResourceCollection(
        array(
            new Resource('example.xml'),
            new ResourceSupport('example.xml.dist', 'example.xml'),
            new Resource('example.yml'),
            new ResourceSupport('example.yml.dist', 'example.yml')
        )
    )
);
```

### Registering Commands

[](#registering-commands)

To register a command, you must tag its service with "box.console.command".

#### In PHP

[](#in-php)

```
$definition = new Definition('My\Command');
$definition->addTag('box.console.command');

$container->setDefinition('my_command', $definition);
```

#### In XML

[](#in-xml)

```

```

#### In YAML

[](#in-yaml)

```
services:

    my_command:
        class: My\Command
        tags:
            - { name: box.console.command }
```

### Registering Helpers

[](#registering-helpers)

To register a command, you must tag its service with "box.console.helper".

#### In PHP

[](#in-php-1)

```
$definition = new Definition('My\Helper');
$definition->addTag('box.console.helper');

$container->setDefinition('my_helper', $definition);
```

#### In XML

[](#in-xml-1)

```

```

#### In YAML

[](#in-yaml-1)

```
services:

    my_helper:
        class: My\Helper
        tags:
            - { name: box.console.helper }
```

### Registering Event Listeners and Subscribers

[](#registering-event-listeners-and-subscribers)

The **EventDispatcher** library supports the registration of listeners, or a collection of listeners through what is known as a "subscriber". A listener is a single callable, while a subscriber is a class that returns a list of which methods must be called when specific events a dispatched.

#### Listeners

[](#listeners)

##### In PHP

[](#in-php-2)

```
$definition = new Definition('My\Listener');
$definition->addTag(
    'box.console.event.listener',
    array(
        'event' => 'the.event',
        'method' => 'onEvent'
    )
);

$container->setDefinition('my_listener', $definition);
```

##### In XML

[](#in-xml-2)

```

```

##### In YAML

[](#in-yaml-2)

```
services:

    my_listener:
        class: My\Listener
        tags:
            - name: box.console.event.listener
              event: the.event
              method: onEvent
```

#### Subscribers

[](#subscribers)

##### In PHP

[](#in-php-3)

```
$definition = new Definition('My\Subscriber');
$definition->addTag('box.console.event.subscriber');

$container->setDefinition('my_subscriber', $definition);
```

##### In XML

[](#in-xml-3)

```

```

##### In YAML

[](#in-yaml-3)

```
services:

    my_subscriber:
        class: My\Subscriber
        tags:
            - { name: box.console.event.subscriber }
```

### Registering Extensions

[](#registering-extensions)

Extensions provide another way of setting parameters and setting service definitions for the container. However, in order make those extensions available to the `container` helper so that the bundled commands still work, registration must be done through the `registerExtension()` method.

```
$app->registerExtension(new MyExtension());
```

This will create a tagged service definition in the container builder, in addition to register the extension with the container. This will allow the helper to re-register the same extension when a new container builder is created for the bundled commands.

The Defaults
------------

[](#the-defaults)

As mentioned early in **Getting Started**, when a `ContainerBuilder` instance is passed to `Application`, a set of default parameters and services are set within the container. The following is a list of those parameters and services.

### Parameters

[](#parameters)

   Name (Default Value) Description      `box.console.auto_exit`
 (`true`)  If `true`, `exit()` is called once a command finishes.    `box.console.class`
 (`Symfony\Component\Console\Application`)  The class for the console application.    `box.console.command.*.class`
 (Instances of `Symfony\Component\Console\Command\Command`)  The class for each default command.    `box.console.event_dispatcher.class`
 (`Symfony\Component\EventDispatcher\ContainerAwareDispatcher`)  The class for the event dispatcher.    `box.console.helper.*.class`
 (Instances of `Symfony\Component\Console\Helper\Helper`)  The class for each default helper.    `box.console.helper.container.class`
 (`Box\Component\Console\Helper\ContainerHelper`)  The class a helper that provides access to the container.    `box.console.helper_set.class`
 (`Symfony\Component\Console\Helper\HelperSet`)  The class for the helper set.    `box.console.input.class`
 (`Symfony\Component\Console\Input\ArgvInput`)  The class for the default input manager.    `box.console.name`
 (`UNKNOWN`)  The name of the console application.    `box.console.output.class`
 (`Symfony\Component\Console\Output\ConsoleOutput`)  The class for the default output manager.    `box.console.version`
 (`UNKNOWN`)  The version of the console application.  ### Services

[](#services)

   Identifier (Class) Description      `box.console`
 (`%box.console.class%`)  The console application which contains all commands.    `box.console.helper.container`
 (`%box.console.helper.container.class%`)  A helper that provides access to the container.    `box.console.command.*`
 (`%box.console.command.*.class%`)  A command.    `box.console.helper.*`
 (`%box.console.helper.*.class%`)  A helper.    `box.console.event_dispatcher`
 (`%box.console.event_dispatcher.class%`)  The event dispatcher.    `box.console.helper_set`
 (`%box.console.helper_set.class%`)  The helper set which contains all helpers.    `box.console.input`
 (`%box.console.input.class%`)  The input manager.    `box.console.output`
 (`%box.console.output.class%`)  The output manager.  ### Commands

[](#commands)

CommandDescription`config:current`Displays the current configuration for an extension registered with the container.`config:list`Displays the list of extensions registered with the container.`config:reference`Displays the reference configuration for an extension registered with the container.`debug:container`Displays the parameters and services in the container. This command is only available if `symfony/framework-bundle` is installed.### Helpers

[](#helpers)

HelperDescription`container`Provides access to the container.Performance
-----------

[](#performance)

The processing of building a container can potentially be time consuming and costly in terms of performance. **Console** provides a way to cache the results of the container building process so that subsequent uses of the application can be faster.

```
use Box\Component\Console\ApplicationCache;
use Symfony\Component\DependencyInjection\ContainerBuilder;

ApplicationCache::bootstrap(
    '/path/to/cache/example.php',
    function (ContainerBuilder $container, ApplicationCache $app) {
        // first-run container building
        // register extensions using $app
    },
    'MyCachedContainer', // name of cached container class
    true                 // toggle debugging
);
```

The `ApplicationCache::bootstrap()` method manages the process of creating, loading, and saving the container. When the application is first run using this method, the following files are created. It is important to note that the name of the generated files will vary depending on what you provided as the first argument to `bootstrap()`.

FileDescription`example.php`The cached container.`example.php.meta`The cache metadata, used to determine if the cache needs to be refreshed.`example.xml`The container configuration used for debugging.By default, the name of the cached container class is `ConsoleContainer` and resides in the root namespace. Also by default, "debugging" is enabled. The debugging option will cause the cache to be refreshed if a resource is updated. By disabling debugging, the cache files must be manually deleted before any of the changes to the resources take effect.

License
-------

[](#license)

This software is released under the MIT license.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

10

Last Release

4008d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9122157?v=4)[Kevin Herrera](/maintainers/kherge)[@kherge](https://github.com/kherge)

---

Top Contributors

[![kherge](https://avatars.githubusercontent.com/u/9122157?v=4)](https://github.com/kherge "kherge (86 commits)")

---

Tags

consoledependencyinjection

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/box-project-console/health.svg)

```
[![Health](https://phpackages.com/badges/box-project-console/health.svg)](https://phpackages.com/packages/box-project-console)
```

###  Alternatives

[crunzphp/crunz

Schedule your tasks right from the code.

2292.0M6](/packages/crunzphp-crunz)[drupal/console-core

Drupal Console Core

13514.7M12](/packages/drupal-console-core)[acquia/orca

A tool for testing a company's software packages together in the context of a realistic, functioning, best practices Drupal build

32902.4k](/packages/acquia-orca)[phpcr/phpcr-shell

Shell for PHPCR

721.3M8](/packages/phpcr-phpcr-shell)[padam87/cron-bundle

A cron job manager for symfony console.

1153.2k](/packages/padam87-cron-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
