SyMfonAK - vyvíjame databázy pomocou frameworku Symfony

Ako volať príkaz z kontroléra

Z SyMfonAK
Prejsť na: navigácia, hľadanie

Symfony dokumentáciaZložka pre konzolu (The Console Component)Príkazový riadok (console) → Ako volať príkaz z kontroléra


originál dokumentácia symfony.com/doc

Ak treba volať príkazy z konroléra, je vhodné z nich spraviť servis a tak ich používať. Sú prípady, kedy máme príkazy využívajúce softvér tretích strán, škoda znovu písať kódy a vtedy je lepšie priamo volať príkazy.

Treba dodať, že volanie z konzoly je menej náročné na prostriedky ako volanie z kontroléra.


Farebný výstup

Farebný výstup vyžaduje najprv nainštalovať:

composer require sensiolabs/ansi-to-html


Použil som obalenie výstupu do pre /pre a to pomohlo vidieť výstup akoby na konzole:


namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
class ConsoleController extends AbstractController
{
  /**
   * @Route("routedebug")
   */
    public function routedebug($messages = 20, KernelInterface $kernel)
    {
        $application = new Application($kernel);
        $application->setAutoExit(false);
        $input = new ArrayInput([
           'command' => 'debug:route',
           // (optional) define the value of command arguments
           //'fooArgument' => 'barValue',
           // (optional) pass options to the command
           //'--message-limit' => $messages,
        ]);
        // You can use NullOutput() if you don't need the output
	$output = new BufferedOutput(
	    OutputInterface::VERBOSITY_NORMAL,
            true // true for decorated
	);
        $application->run($input, $output);
        // return the output, don't use if you used NullOutput()
	$converter = new AnsiToHtmlConverter();
        $content = $output->fetch();
        // return new Response(""), if you used NullOutput()
        return new Response('<pre>'.$converter->convert($content).'<//pre>'); //pridal som / , v kóde samozrejme to nie je
    }
  /**
   * @Route("cc")
   */
    public function clearcache($messages = 20, KernelInterface $kernel)
    {
        $application = new Application($kernel);
        $application->setAutoExit(false);
        $input = new ArrayInput([
           'command' => 'cache:clear',
           // (optional) define the value of command arguments
           //'fooArgument' => 'barValue',
           // (optional) pass options to the command
           //'--message-limit' => $messages,
        ]);
        // You can use NullOutput() if you don't need the output
	$output = new BufferedOutput(
	    OutputInterface::VERBOSITY_NORMAL,
            true // true for decorated
	);
        $application->run($input, $output);
        // return the output, don't use if you used NullOutput()
	$converter = new AnsiToHtmlConverter();
        $content = $output->fetch();
        // return new Response(""), if you used NullOutput()
        return new Response('<pre>'.$converter->convert($content).'<//pre>');  //pridal som / , v kóde samozrejme to nie je
    }
}
?>