Skip to content

Commit

Permalink
rewrite docs
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
fprochazka committed May 23, 2017
1 parent c211c6a commit fd0816c
Showing 1 changed file with 63 additions and 50 deletions.
113 changes: 63 additions & 50 deletions docs/en/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Quickstart
==========
# Quickstart

This extension is here to provide integration of [Symfony Console](https://github.com/symfony/console) into Nette Framework.


Installation
-----------
## Installation

The best way to install Kdyby/Console is using [Composer](http://getcomposer.org/):

Expand All @@ -17,25 +15,38 @@ You can enable the extension using your neon config.

```yml
extensions:
console: Kdyby\Console\DI\ConsoleExtension
console: Kdyby\Console\DI\ConsoleExtension
```

Minimal configuration
---------------------
## Minimal configuration
This extension creates new configuration section `console`, the absolute minimal configuration might look like this

```yml
console:
url: http://www.kdyby.org
url: http://www.kdyby.org
```

The `url` key specifies reference url that allows you to generate urls using `LinkGenerator` in CLI (which is not possible otherwise).


## Running the console

It is suggested, that you create a `bin/console` file, with the following contents

```php
#!/usr/bin/env php
<?php
/** @var \Nette\DI\Container $container */
$container = require __DIR__ . '/../app/bootstrap.php';
$container->getByType(\Symfony\Component\Console\Application::class)->run();
```

The `url` key specifies reference url that allows you to generate urls using Nette `UI\Presenter` in CLI (which is not possible otherwise). Another useful key is `commands` where you can register new commands. Look at the [Extending](#extending) part.
Make sure the console script is executable by running `chmod +x bin/console`.

And test it by running `php bin/console` (but just `bin/console` should work too), it should list all available commands.

Writing commands
----------------
## Writing commands

Commands are like controllers, but for Symfony Console. Example command might look like this

Expand All @@ -48,26 +59,26 @@ use Symfony\Component\Console\Output\OutputInterface;
class SendNewslettersCommand extends Command
{
protected function configure()
{
$this->setName('app:newsletter')
->setDescription('Sends the newsletter');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$newsletterSender = $this->getHelper('container')->getByType('Models\NewsletterSender');

try {
$newsletterSender->sendNewsletters();
$output->writeLn('Newsletter sent');
return 0; // zero return code means everything is ok

} catch (\Nette\Mail\SmtpException $e) {
$output->writeLn('<error>' . $e->getMessage() . '</error>');
return 1; // non-zero return code means error
}
}
protected function configure(): void
{
$this->setName('app:newsletter')
->setDescription('Sends the newsletter');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$newsletterSender = $this->getHelper('container')->getByType('Models\NewsletterSender');
try {
$newsletterSender->sendNewsletters();
$output->writeLn('Newsletter sent');
return 0; // zero return code means everything is ok
} catch (\Nette\Mail\SmtpException $e) {
$output->writeLn('<error>' . $e->getMessage() . '</error>');
return 1; // non-zero return code means error
}
}
}
```

Expand All @@ -81,36 +92,38 @@ The second one is command output which should be used to provide feedback to the
Best practise is to return an exit code which specifies if the command ran successfully and can be read by other applications when executed.


Extending
---------
## Registering commands

To add a command, simply register it as a service with tag `kdyby.console.command`
To add a command, register it as a service with tag `kdyby.console.command`

```yml
services:
newsletterCommand:
class: App\Console\SendNewslettersCommand
tags: [kdyby.console.command]
newsletterCommand:
class: App\Console\SendNewslettersCommand
tags: [kdyby.console.command]
```

Alternatively you can use shorter syntax for registering command (without tag). It's useful when you have a lot of commands:
To add a helper, register it as a service with tag `kdyby.console.helper`


```yml
console:
commands:
- App\Console\SendNewslettersCommand
- App\Console\AnotherCommand
- App\Console\AnotherCommand2
services:
fooHelper:
class: App\Console\FooHelper
tags: [kdyby.console.helper]
```

This is called anonymous registration (look at hyphens). You can name your command (`newsletterCommand: App\Console\SendNewslettersCommand`) but mostly it's not necessary.

To add a helper, simply register it as a service with tag `kdyby.console.helper`
### Shorter configuration

If you want to register all your commands and don't want to write tags to all of them, you can use the Nette Decorator extension

```yml
decorator:
Symfony\Component\Console\Command\Command:
tags: [kdyby.console.command]
services:
fooHelper:
class: App\Console\FooHelper
tags: [kdyby.console.helper]
- App\Console\SendNewslettersCommand
```

Nette will add the tag to all the command services automatically and they will get picked by Kdyby/Console and registered as commands.

0 comments on commit fd0816c

Please sign in to comment.