Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Created December 16, 2020 20:09
Show Gist options
  • Save BlackScorp/8d8b4b667b7626245ca17e7067034a6a to your computer and use it in GitHub Desktop.
Save BlackScorp/8d8b4b667b7626245ca17e7067034a6a to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/RandomCommand.php';
use Symfony\Component\Console\Application;
$console = new Application();
$console->add(new RandomCommand());
$console->run();
<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RandomCommand extends Command{
protected static $defaultName = 'test:show-random';
public function configure(){
$this->setDescription('Ein Programm welche Zufallszahlen generiert');
$this->addArgument('maxNumber',InputArgument::OPTIONAL,'Maximale nummer die generiert wird',42);
}
public function execute(InputInterface $input,OutputInterface $output){
$output->writeln("Programm start");
$max = $input->getArgument('maxNumber');
$randomNumber = random_int(0,$max);
$verboseOutput = $output->section();
$normalOutput = $output->section();
$progressBar = new ProgressBar($normalOutput,10);
for($i = 0;$i<10;$i++){
sleep(1);
$verboseOutput->overwrite("ich zähle ".$i,OutputInterface::VERBOSITY_VERBOSE);
$progressBar->advance();
}
$progressBar->finish();
$output->write("\nIch habe lange nachgedacht und die Antwort lautet: ".$randomNumber);
return Command::SUCCESS;
}
}
@woemar
Copy link

woemar commented Feb 23, 2021

To initialize usage of symfony console:
composer require symfony/console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment