From a75203b154dd50d49707fc8acf6f2c542efb91bb Mon Sep 17 00:00:00 2001 From: Richard van Laak Date: Mon, 17 Jan 2022 10:37:11 +0100 Subject: [PATCH] Initial commit --- .editorconfig | 10 ++++++ .gitignore | 1 + README.md | 32 +++++++++++++++++++ bin/console | 10 ++++++ composer.json | 27 ++++++++++++++++ config/bootstrap.php | 20 ++++++++++++ config/services.php | 23 +++++++++++++ phpunit.xml.dist | 32 +++++++++++++++++++ src/Application.php | 15 +++++++++ src/Application/.gitkeep | 0 src/Domain/.gitkeep | 0 src/Infrastructure/.gitkeep | 0 .../Cli/Command/ExampleCommand.php | 30 +++++++++++++++++ tests/Functional/Application/.gitkeep | 0 tests/Functional/Domain/.gitkeep | 0 tests/Functional/Infrastructure/.gitkeep | 0 tests/Functional/UserInterface/.gitkeep | 0 tests/Unit/Application/.gitkeep | 0 tests/Unit/Domain/.gitkeep | 0 tests/Unit/Infrastructure/.gitkeep | 0 tests/Unit/UserInterface/.gitkeep | 0 tests/bootstrap.php | 6 ++++ 22 files changed, 206 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 README.md create mode 100755 bin/console create mode 100644 composer.json create mode 100644 config/bootstrap.php create mode 100644 config/services.php create mode 100644 phpunit.xml.dist create mode 100644 src/Application.php create mode 100644 src/Application/.gitkeep create mode 100644 src/Domain/.gitkeep create mode 100644 src/Infrastructure/.gitkeep create mode 100644 src/UserInterface/Cli/Command/ExampleCommand.php create mode 100644 tests/Functional/Application/.gitkeep create mode 100644 tests/Functional/Domain/.gitkeep create mode 100644 tests/Functional/Infrastructure/.gitkeep create mode 100644 tests/Functional/UserInterface/.gitkeep create mode 100644 tests/Unit/Application/.gitkeep create mode 100644 tests/Unit/Domain/.gitkeep create mode 100644 tests/Unit/Infrastructure/.gitkeep create mode 100644 tests/Unit/UserInterface/.gitkeep create mode 100644 tests/bootstrap.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5c1d2f4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..f28919b --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +Symfony CLI Minimum Skeleton +============================ + +Thi CLI project skeleton tempts to require the least amount of code needed to write a command line interface project +based on Symfony Console. + +The folder structure follow a simple Domain Driven Design application structure by suggesting splitting Application, +Domain, UserInterface, and Infrastructure responsibilities. + +The minimum of features that is needed for developing CLI applications is included: + +* [Auto-wiring](https://symfony.com/doc/current/service_container/autowiring.html) on all classes in `/src` +* Autoconfigure `Command` implementations directly to the CLI console application +* [PHPUnit](https://phpunit.de/) + +## How to run tests? + +Running PHPUnit test suites is possible by running `composer run tests`. The `/tests` folder gets auto-registered by +composer, and follows the DDD application structure, but no actual tests are provided with the skeleton. + +## How to register a command to the CLI console? + +Registering a command to the CLI application is the only autoconfiguration available in this skeleton, as can be found +in `config/services.php`. + +* Create a class (preferably in `/src/UserInterface/Cli`) and let it extend `Symfony\Component\Console\Command\Command` +* Run `bin/console`, the command created became available + +## Environment requirements + +* PHP `>=8.0.2` with `ext-mbstring` extension enabled +* [Composer](https://getcomposer.org/) diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..2efc1d7 --- /dev/null +++ b/bin/console @@ -0,0 +1,10 @@ +#!/usr/bin/env php +get(Application::class); +exit($application->run()); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..22b6d46 --- /dev/null +++ b/composer.json @@ -0,0 +1,27 @@ +{ + "require": { + "php": ">=8.0.2", + "ext-mbstring": "*", + "symfony/dependency-injection": "^6.0", + "symfony/console": "^6.0", + "symfony/config": "^6.0" + }, + "require-dev": { + "roave/security-advisories": "dev-latest", + "phpunit/phpunit": "^9.5" + }, + "scripts": { + "app:example": "bin/console app:example 'John Doe'", + "test": "vendor/bin/phpunit" + }, + "autoload": { + "psr-4": { + "App\\": ["src"] + } + }, + "autoload-dev": { + "psr-4": { + "App\\": ["tests"] + } + } +} diff --git a/config/bootstrap.php b/config/bootstrap.php new file mode 100644 index 0000000..5e8d382 --- /dev/null +++ b/config/bootstrap.php @@ -0,0 +1,20 @@ +addCompilerPass(new AddConsoleCommandPass()); + +$loader = new PhpFileLoader($container, new FileLocator(__DIR__)); +$loader->load(__DIR__.'/services.php'); + +$container->compile(); + +return $container; diff --git a/config/services.php b/config/services.php new file mode 100644 index 0000000..1cfc5e5 --- /dev/null +++ b/config/services.php @@ -0,0 +1,23 @@ +services(); + $services + ->defaults() + ->autoconfigure() + ->autowire() + ; + + $services->instanceof(Command::class)->tag('console.command'); + + $services->load(__NAMESPACE__.'\\', '../src/*'); + $services->get(Application::class) + ->args([tagged_iterator('console.command')]) + ->public(); +}; diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..ae82d5c --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,32 @@ + + + + + src + + + + + + + + + + + + + + tests/Unit + + + tests/Functional + + + diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 0000000..3630cdf --- /dev/null +++ b/src/Application.php @@ -0,0 +1,15 @@ +addCommands(iterator_to_array($commands)); + + parent::__construct(__NAMESPACE__, '1.0.0'); + } +} diff --git a/src/Application/.gitkeep b/src/Application/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/Domain/.gitkeep b/src/Domain/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/Infrastructure/.gitkeep b/src/Infrastructure/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/UserInterface/Cli/Command/ExampleCommand.php b/src/UserInterface/Cli/Command/ExampleCommand.php new file mode 100644 index 0000000..62673b4 --- /dev/null +++ b/src/UserInterface/Cli/Command/ExampleCommand.php @@ -0,0 +1,30 @@ +addArgument('name', InputArgument::REQUIRED, 'Name of the one running the example command.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $name = $input->getArgument('name'); + + $output->writeln("Hello worl, my name is $name."); + + return self::SUCCESS; + } +} diff --git a/tests/Functional/Application/.gitkeep b/tests/Functional/Application/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Functional/Domain/.gitkeep b/tests/Functional/Domain/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Functional/Infrastructure/.gitkeep b/tests/Functional/Infrastructure/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Functional/UserInterface/.gitkeep b/tests/Functional/UserInterface/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Unit/Application/.gitkeep b/tests/Unit/Application/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Unit/Domain/.gitkeep b/tests/Unit/Domain/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Unit/Infrastructure/.gitkeep b/tests/Unit/Infrastructure/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/Unit/UserInterface/.gitkeep b/tests/Unit/UserInterface/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..f491681 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,6 @@ +