intellect-web-development / symfony-entry-contract-bundle
SymfonyEntryContract Symfony bundle for pretty contracts
Installs: 1 997
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/intellect-web-development/symfony-entry-contract-bundle
Requires
- php: >=8.2
- symfony/config: *
- symfony/dependency-injection: *
- symfony/http-kernel: *
- symfony/serializer: *
- symfony/validator: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.23
- overtrue/phplint: ^9.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.3
- rector/rector: ^0.19.0
- symfony/framework-bundle: 6.*
- symfony/phpunit-bridge: 6.*
- symfony/test-pack: ^1.0
- vimeo/psalm: ^5.15
- zircote/swagger-php: ^4.9
README
HTTP Entry:
InputContract:
<?php declare(strict_types=1); namespace App\Entry\Http\Root; use IWD\SymfonyEntryContract\Interfaces\InputContractInterface; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotNull; class InputContract implements InputContractInterface { #[NotNull] #[Length(min: 3, max: 255)] public string $name; }
Http Action/Controller:
<?php declare(strict_types=1); namespace App\Entry\Http\Root; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class Action { #[Route(path: '', methods: ['GET'])] public function action( InputContract $inputContract ): Response { return new Response($inputContract->name); } }
CLI Entry:
InputContract:
<?php declare(strict_types=1); namespace App\Entry\Console\Demo; use IWD\SymfonyEntryContract\Interfaces\InputContractInterface; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; class InputContract implements InputContractInterface { /** User email */ #[NotNull] #[NotBlank] public string $email = 'user@dev.com'; /** User name */ #[NotNull] #[NotBlank] #[Length(min: 1)] public string $name = 'Foo'; /** Root user password */ #[NotNull] #[NotBlank] #[Length(min: 4)] public string $password = 'bar'; }
CliCommand:
<?php declare(strict_types=1); namespace App\Entry\Console\Demo; use IWD\SymfonyEntryContract\Attribute\CliContract; use IWD\SymfonyEntryContract\Console\CliCommand; use IWD\SymfonyEntryContract\Interfaces\InputContractInterface; use IWD\SymfonyEntryContract\Service\CliContractResolver; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Style\SymfonyStyle; #[AsCommand( name: 'app:demo', description: 'Demo CLI', )] #[CliContract(class: InputContract::class)] class DemoCommand extends CliCommand { public function __construct( CliContractResolver $cliContractResolver, ) { parent::__construct($cliContractResolver); } /** * @param InputContract $inputContract */ protected function handle(SymfonyStyle $io, InputContractInterface $inputContract): int { $io->success( sprintf( '%, %, %', $inputContract->name, $inputContract->email, $inputContract->password ) ); return self::SUCCESS; } }
Command help:
php bin/console app:demo -h
Result:
Description:
  Demo CLI
Usage:
  app:demo [options]
Options:
      --email=EMAIL        User email [default: "user@dev.com"]
      --name=NAME          User name [default: "Foo"]
      --password=PASSWORD  Root user password [default: "bar"]
  -h, --help               Display help for the given command. When no command is given display help for the list command
  -q, --quiet              Do not output any message
  -V, --version            Display this application version
      --ansi|--no-ansi     Force (or disable --no-ansi) ANSI output
  -n, --no-interaction     Do not ask any interactive question
  -e, --env=ENV            The Environment name. [default: "dev"]
      --no-debug           Switch off debug mode.
  -v|vv|vvv, --verbose     Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug