pk/command-extra-bundle

Bundle that adds some flavor to Symfony2's commands

v1.0.3 2013-12-13 14:06 UTC

README

This bundle adds some sugar to the default ContainerAwareCommand. It provides methods to make your command single-processed, turn off logging, and more.

Installation

Installation for Symfony 2.1 (and up) via composer:

Add the following to your composer.json:

{
    "require": {
        "pk/command-extra-bundle": "1.*"
    }
}

Update dependency:

composer.phar update pk/command-extra-bundle

Register bundle:

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new PK\CommandExtraBundle\PKCommandExtraBundle(),
    );
    // ...
}

Usage

The bundle mainly provides a class that you can extend your commands with. It's located at PK\CommandExtraBundle\Command\Command and it extends the ContainerAwareCommand provided by Symfony's framework bundle.

By extending the class you can call three methods in the configure method:

  • isSingleProcessed(): This sets a flag indicating that this command can only run one at a time. Useful when you're starting the command with a cronjob, preventing multiple running instances. Requires the posix extension loaded.
  • disableLoggers(): By default, a lot of information is being logged. If you don't need this information, or want your command to consume less memory (this depends on your setup of course, YMMV), this turns off logging for Doctrine and the default logger service.
  • setSummarizeDefinition(): For now, only the time and memory consumed are logged at the beginning and end of the command. This method controls which of these are logged. Both are logged by default.

Example:

protected function configure()
{
    $this
        ->setDefinition(array())
        ->setName('foo:bar')
        ->isSingleProcessed()
        ->disableLoggers()
        ->setSummarizeDefinition(array('memory' => false))
    ;
}

For tips and/or feature requests, please don't hesitate to add an issue.