Skip to content

Commit

Permalink
[WIP] Check for TYPO3 context in command controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
tinzog committed Nov 6, 2023
1 parent 109bdf9 commit e70ee1b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Classes/Command/Foreign/Status/AllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
* This copyright notice MUST APPEAR in all copies of the script!
*/

use In2code\In2publishCore\Testing\Service\Typo3ContextServiceInjection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\CMS\Core\Console\CommandRegistry;

class AllCommand extends Command
{
use Typo3ContextServiceInjection;

public const IDENTIFIER = 'in2publish_core:status:all';
protected CommandRegistry $cmdRegistry;

Expand All @@ -50,6 +53,10 @@ public function injectCommandRegistry(CommandRegistry $commandRegistry): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this->typo3ContextService->isCliOrBackendEnvironment()) {
$output->writeln('This command is only available in backend or CLI context');
return Command::FAILURE;
}
$this->cmdRegistry->getCommandByIdentifier(VersionCommand::IDENTIFIER)->execute($input, $output);
$this->cmdRegistry->getCommandByIdentifier(CreateMasksCommand::IDENTIFIER)->execute($input, $output);
$this->cmdRegistry->getCommandByIdentifier(GlobalConfigurationCommand::IDENTIFIER)->execute($input, $output);
Expand Down
21 changes: 21 additions & 0 deletions Classes/Service/Typo3Context/Typo3ContextService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace In2code\In2publishCore\Service\Typo3Context;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\ApplicationType;

class Typo3ContextService
{
public function isCliOrBackendEnvironment(): bool
{
if (Environment::isCli()) {
return true;
}

$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
return $request instanceof ServerRequestInterface && ApplicationType::fromRequest($request)->isBackend();
}
}

21 changes: 21 additions & 0 deletions Classes/Service/Typo3Context/Typo3ContextServiceInjection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace In2code\In2publishCore\Service\Typo3Context;

/**
* @codeCoverageIgnore
*/
trait Typo3ContextServiceInjection
{
protected Typo3ContextService $typo3ContextService;

/**
* @noinspection PhpUnused
*/
public function injectTypo3ContextService(Typo3ContextService $typo3ContextService): void
{
$this->typo3ContextService = $typo3ContextService;
}
}

0 comments on commit e70ee1b

Please sign in to comment.