|
| 1 | +# ConsoleServiceProvider |
| 2 | + |
| 3 | +Provides a `Symfony\Component\Console` based console for Silex. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +Add `knplabs/console-service-provider` to your `composer.json` and register the service: |
| 8 | + |
| 9 | +```php |
| 10 | +<?php |
| 11 | + |
| 12 | +use Knp\Provider\ConsoleServiceProvider; |
| 13 | + |
| 14 | +$app->register(new ConsoleServiceProvider(), array( |
| 15 | + 'console.name' => 'MyApplication', |
| 16 | + 'console.version' => '1.0.0', |
| 17 | + 'console.project_directory' => __DIR__.'/..' |
| 18 | +)); |
| 19 | + |
| 20 | +?> |
| 21 | +``` |
| 22 | + |
| 23 | +You can now copy the `console` executable in whatever place you see fit, and tweak it to your needs. You will need a way to fetch your silex application, the most common way is to return it from your bootstrap: |
| 24 | + |
| 25 | +```php |
| 26 | +<?php |
| 27 | + |
| 28 | +$app = new Silex\Application(); |
| 29 | + |
| 30 | +// your beautiful silex bootstrap |
| 31 | + |
| 32 | +return $app; |
| 33 | + |
| 34 | +?> |
| 35 | +``` |
| 36 | + |
| 37 | +For the rest of this document, we will assume you do have an `app` directory, so the `console` executable will be located at `app/console`. |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +Use the console just like any `Symfony\Component` based console: |
| 42 | + |
| 43 | +``` |
| 44 | +$ app/console my:command |
| 45 | +``` |
| 46 | + |
| 47 | +## Write commands |
| 48 | + |
| 49 | +Your commands should extend `Knp\Command\Command` to have access to the 2 useful following commands: |
| 50 | + |
| 51 | +* `getSilexApplication`, which returns the silex application |
| 52 | +* `getProjectDirectory`, which returns your project's root directory (as configured earlier) |
| 53 | + |
| 54 | +I know, it's a lot to learn, but it's worth the pain. |
| 55 | + |
| 56 | +## Register commands |
| 57 | + |
| 58 | +There are two ways of registering commands to the console application. |
| 59 | + |
| 60 | +### Directly access the console application from the `console` executable |
| 61 | + |
| 62 | +Open up `app/console`, and stuff your commands directly into the console application: |
| 63 | + |
| 64 | +```php |
| 65 | +#!/usr/bin/env php |
| 66 | +<?php |
| 67 | + |
| 68 | +set_time_limit(0); |
| 69 | + |
| 70 | +$app = require_once __DIR__.'/bootstrap.php'; |
| 71 | + |
| 72 | +use My\Command\MyCommand; |
| 73 | + |
| 74 | +$application = $app['console']; |
| 75 | +$application->add(new MyCommand()); |
| 76 | +$application->run(); |
| 77 | + |
| 78 | +?> |
| 79 | +``` |
| 80 | + |
| 81 | +### Use the Event Dispatcher |
| 82 | + |
| 83 | +This way is intended for use by provider developers and exposes an unobstrusive way to register commands in 3 simple steps: |
| 84 | + |
| 85 | +1. Register a listener to the `ConsoleEvents::INIT` event |
| 86 | +2. ??? |
| 87 | +3. PROFIT! |
| 88 | + |
| 89 | +Example: |
| 90 | + |
| 91 | +```php |
| 92 | +<?php |
| 93 | + |
| 94 | +use My\Command\MyCommand; |
| 95 | +use Knp\Console\Events as ConsoleEvents; |
| 96 | +use Knp\Console\Event as ConsoleEvent; |
| 97 | + |
| 98 | +$app['dispatcher']->addListener(ConsoleEvents::INIT, function(ConsoleEvent $event) { |
| 99 | + $app = $event->getApplication(); |
| 100 | + $app->add(new MyCommand()); |
| 101 | +}); |
| 102 | + |
| 103 | +?> |
| 104 | +``` |
0 commit comments