Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Kdyby/Console
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.7.0
Choose a base ref
...
head repository: Kdyby/Console
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 1,422 additions and 1,421 deletions.
  1. +26 −0 .github/workflows/php-package-ci.yml
  2. +1 −0 .gitignore
  3. +0 −54 .travis.yml
  4. +17 −0 Makefile
  5. +0 −13 README.md
  6. +34 −32 composer.json
  7. +72 −50 docs/en/index.md
  8. +12 −0 phpstan-baseline.neon
  9. +8 −0 phpstan.neon
  10. +23 −0 ruleset.xml
  11. +52 −56 src/Application.php
  12. +37 −26 src/CliPresenter.php
  13. +17 −29 src/CliResponse.php
  14. +28 −50 src/CliRouter.php
  15. +14 −47 src/ContainerHelper.php
  16. +13 −13 src/DI/BootstrapHelper.php
  17. +10 −0 src/DI/ClassAliasMap.php
  18. +108 −110 src/DI/ConsoleExtension.php
  19. +10 −0 src/Exception/Exception.php
  20. +10 −0 src/Exception/InvalidApplicationModeException.php
  21. +10 −0 src/Exception/InvalidArgumentException.php
  22. +10 −0 src/Exception/InvalidStateException.php
  23. +18 −0 src/Exception/UnknownCommandException.php
  24. +14 −10 src/FatalThrowableError.php
  25. +13 −26 src/Helpers/PresenterHelper.php
  26. +17 −25 src/HttpRequestFactory.php
  27. +8 −20 src/StringOutput.php
  28. +0 −61 src/exceptions.php
  29. +0 −140 tests/KdybyTests/Console/Application.phpt
  30. +90 −0 tests/KdybyTests/Console/ApplicationTest.phpt
  31. +0 −73 tests/KdybyTests/Console/CliRouter.phpt
  32. +83 −0 tests/KdybyTests/Console/CliRouterTest.phpt
  33. +10 −20 tests/KdybyTests/Console/{ContainerHelper.phpt → ContainerHelperTest.phpt}
  34. +0 −96 tests/KdybyTests/Console/Extension.phpt
  35. +73 −0 tests/KdybyTests/Console/ExtensionTest.phpt
  36. +20 −17 tests/KdybyTests/Console/{HttpRequestFactory.phpt → HttpRequestFactoryTest.phpt}
  37. +0 −404 tests/KdybyTests/Console/InputErrors.phpt
  38. +152 −0 tests/KdybyTests/Console/InputErrorsTest.phpt
  39. +32 −0 tests/KdybyTests/Console/data/AmbiguousCommand1.php
  40. +32 −0 tests/KdybyTests/Console/data/AmbiguousCommand2.php
  41. +38 −0 tests/KdybyTests/Console/data/ArgCommand.php
  42. +12 −39 tests/KdybyTests/Console/{ → data}/CliAppTester.php
  43. +17 −0 tests/KdybyTests/Console/data/CommandMock.php
  44. +42 −0 tests/KdybyTests/Console/data/ConsoleListener.php
  45. +32 −0 tests/KdybyTests/Console/data/NamespaceAmbiguousCommand1.php
  46. +32 −0 tests/KdybyTests/Console/data/NamespaceAmbiguousCommand2.php
  47. +61 −0 tests/KdybyTests/Console/data/SameArgsCommandOne.php
  48. +61 −0 tests/KdybyTests/Console/data/SameArgsCommandTwo.php
  49. +32 −0 tests/KdybyTests/Console/data/TypoCommand.php
  50. +18 −10 tests/KdybyTests/bootstrap.php
  51. +3 −0 tests/{ → KdybyTests}/nette-reset.neon
26 changes: 26 additions & 0 deletions .github/workflows/php-package-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Package CI

on:
pull_request:

jobs:
checks:
name: Checks
runs-on: ubuntu-latest
strategy:
matrix:
php: [ 7.4, 8.0, 8.1 ]
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: iconv
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- run: make composer

- run: make phpstan

- run: make tester
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
tests/php.ini-local
54 changes: 0 additions & 54 deletions .travis.yml

This file was deleted.

17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
composer:
composer validate
composer update --no-interaction --prefer-dist

cs:
vendor/bin/phpcs --standard=./ruleset.xml --cache=${HOME}/phpcs-cache/.phpcs-cache --encoding=utf-8 -sp src tests/KdybyTests
vendor/bin/parallel-lint -e php,phpt --exclude vendor .

phpstan:
vendor/bin/phpstan analyse -l 2 -c phpstan.neon src tests/KdybyTests

phpstan-generate-baseline:
git clean -xdf tests/
php -d memory_limit=-1 vendor/bin/phpstan.phar analyse -l 2 -c phpstan.neon src tests/KdybyTests --no-progress --generate-baseline

tester:
vendor/bin/tester -s -C ./tests/KdybyTests/
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -6,17 +6,6 @@ Kdyby/Console
[![Latest stable](https://img.shields.io/packagist/v/kdyby/console.svg)](https://packagist.org/packages/kdyby/console)
[![Coverage Status](https://coveralls.io/repos/github/Kdyby/Console/badge.svg?branch=master)](https://coveralls.io/github/Kdyby/Console?branch=master)



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

Kdyby/Console requires PHP 5.4 or higher.

- [Nette Framework](https://github.com/nette/nette)
- [Symfony Console](https://github.com/symfony/Console)


Installation
------------

@@ -26,13 +15,11 @@ The best way to install Kdyby/Console is using [Composer](http://getcomposer.or
$ composer require kdyby/console
```


Documentation
------------

Learn more in the [documentation](https://github.com/Kdyby/Console/blob/master/docs/en/index.md).


-----

Homepage [http://www.kdyby.org](http://www.kdyby.org) and repository [http://github.com/kdyby/Console](http://github.com/kdyby/Console).
66 changes: 34 additions & 32 deletions composer.json
Original file line number Diff line number Diff line change
@@ -17,54 +17,56 @@
"issues": "https://github.com/kdyby/console/issues"
},
"require": {
"php": ">=5.4",
"nette/di": "^2.4@dev",

"symfony/console": "~2.3|^3.0"
"php": "^7.4 | ^8.0",
"nette/di": "^3.0",
"nette/routing": "^3.0.0",
"tracy/tracy": "^2.8 || ^3.0",
"nette/utils": "^3.1",
"symfony/console": "~2.3 || ^3.0 || < 4.5"
},
"require-dev": {
"nette/application": "^2.4@dev",
"nette/bootstrap": "^2.4@dev",
"nette/caching": "^2.4@dev",
"nette/component-model": "^2.3@dev",
"nette/database": "^2.4@dev",
"nette/deprecated": "^2.4@dev",
"nette/di": "^2.4@dev",
"nette/finder": "^2.4@dev",
"nette/forms": "^2.4@dev",
"nette/http": "^2.4@dev",
"nette/mail": "^2.4@dev",
"nette/neon": "^2.4@dev",
"nette/php-generator": "^2.4@dev",
"nette/reflection": "^2.4@dev",
"nette/robot-loader": "^2.4@dev",
"nette/safe-stream": "^2.3@dev",
"nette/security": "^2.4@dev",
"nette/tokenizer": "^2.2@dev",
"nette/utils": "^2.4@dev",
"latte/latte": "^2.4@dev",
"tracy/tracy": "^2.4@dev",
"nette/application": "^3.0",
"nette/bootstrap": "^3.0",
"nette/caching": "^3.0",
"nette/http": "^3.0",
"kdyby/events": "^5.0",
"symfony/event-dispatcher": "~2.3 || ^3.0 || < 4.5",

"nette/tester": "~1.7",
"kdyby/events": "^3.1@dev",
"symfony/event-dispatcher": "~2.3|^3.0"
"nette/tester": "^2.2",
"phpstan/phpstan": "^0.12.88",
"doctrine/coding-standard": "8.2.*",
"php-coveralls/php-coveralls": "^2.1",
"php-parallel-lint/php-parallel-lint": "^v1.2.0",
"typo3/class-alias-loader": "^1.0"
},
"suggest": {
"nette/application": "For hooking the extension into the app and enabling running it through www/index.php",
"nette/http": "For faking the url of the request in cli"
},
"autoload": {
"psr-4": {
"Kdyby\\Console\\": "src/"
},
"classmap": [
"src/CliPresenter.php",
"src/exceptions.php"
"src/CliPresenter.php"
]
},
"autoload-dev": {
"psr-4": {
"KdybyTests\\": "tests/KdybyTests/"
},
"classmap": [
"tests/KdybyTests/"
]
},
"extra": {
"branch-alias": {
"dev-master": "2.7-dev"
"dev-master": "2.8-dev"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"typo3/class-alias-loader": true
}
}
}
122 changes: 72 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/):

@@ -17,25 +15,39 @@ 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';
$console = $container->getByType(\Symfony\Component\Console\Application::class);
exit($console->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

@@ -48,26 +60,34 @@ 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
}
}
/** @var \Models\NewsletterSender */
private $newsletterSender;
public function __construct(NewsletterSender $newsletterSender)
{
parent::__construct();
$this->newsletterSender = $newsletterSender;
}
protected function configure(): void
{
$this->setName('app:newsletter')
->setDescription('Sends the newsletter');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$this->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
}
}
}
```

@@ -81,36 +101,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.
Loading