www.lowcodeapp.de - Beschleunigung der digitalen Transformation mit Open Source Low-Code Development.

TYPO3 8.7 Commands

Logo von TYPO3

Mit TYPO3 8 wurde eine neue Schnittstelle integriert, um Kommandos für die CLI bereitzustellen. Diese basiert auf den bekannten und weit verbreiteten Symfony Console Commands. Der alte Weg über den CommandLineController und Aufruf von typo3/cli_dispatch.phpsh ist nun deprecated und wird in TYPO3 9 entfernt.

Werfen wir nun einen Blick auf die neue Schnittstelle.

Command Anlegen
Eine Command Klasse muss angelegt werden. Sie sollte unter Classes/Command liegen.

Classes/Command/SimpleTextOutputCommand.php

<?php
namespace Vendor\Example\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SimpleTextOutputCommand extends Command
{
/**
     * Configure the command by defining the name
     */
    protected function configure()
    {
        $this
        ->setDescription('Simple Text Output');
    }

**
     * Executes the command
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
// Show Name
$io = new SymfonyStyle($input, $output);
        $io->title($this->getDescription());

    }

}

Anforderungen:

  1. Klasse muss Symfony\Component\Console\Command\Command erweitern
  2. Klasse muss eine configure und eine execute Methode haben

Nach der Erstellung der Command Klasse muss diese in der Datei Configuration/Commands.php (Konvention von TYPO3) registriert werden.

<?php
return [
    'Example:simpleOutput' => [
            'class' => \Vendor\Example\Command\SimpleTextOutputCommand::class
    ]


Command Aufrufen
Nachdem ein Command registriert wurde und verfügbar ist, kann es über vendor/bin/typo3 Example:simpleOutput aufgerufen werden.

Die Ausgabe sollte so aussehen:

Simple Text Output
==================


Command Argumente
Manche commands benötigen Argumente. Diese werdem im configure Block definiert:

<?php
namespace Vendor\Example\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SimpleTextOutputCommand extends Command
{
/**
     * Configure the command by defining the name
     */
    protected function configure()
    {
        $this
        ->setDescription('Simple Text Output')
        ->addArgument(
            'text',
            InputArgument::OPTIONAL,
            'If set, then this will be printed Out.'
        );
    }

**
     * Executes the command
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
// Show Name
$io = new SymfonyStyle($input, $output);
        $io->title($this->getDescription());

// print out Argument
$io->text($input->getArgument('text'));
    }
}


Die Ausgabe sollte folgendermaßen aussehen, wenn vendor/bin/typo3 Example:simpleOutput 123 aufgerufen wird:

Simple Text Output
==================
123


Die Argument Information wird angezeigt, wenn vendor/bin/typo3 help Example:simpleOutput aufgerufen wird:

Usage:
  Example:simpleOutput [<text>]

Arguments:
  text                  If set, then this will be printed Out.
ZURÜCK

Kontaktieren Sie uns