2024-05-07 14:21:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace TTN\Tea\Command;
|
|
|
|
|
2024-07-30 12:32:54 +02:00
|
|
|
use Symfony\Component\Console\Application;
|
2024-05-07 14:21:17 +02:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2024-07-30 12:32:54 +02:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
use TYPO3\CMS\Backend\Command\ProgressListener\ReferenceIndexProgressListener;
|
|
|
|
use TYPO3\CMS\Backend\Command\ReferenceIndexUpdateCommand;
|
|
|
|
use TYPO3\CMS\Core\Core\Bootstrap;
|
2024-05-07 14:21:17 +02:00
|
|
|
use TYPO3\CMS\Core\Database\Connection;
|
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
2024-07-30 12:32:54 +02:00
|
|
|
use TYPO3\CMS\Core\Database\ReferenceIndex;
|
2024-05-07 14:21:17 +02:00
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
|
2024-07-29 19:44:06 +02:00
|
|
|
/*
|
|
|
|
* Command to create test data for the tea extension.
|
|
|
|
*/
|
2024-05-07 14:21:17 +02:00
|
|
|
final class CreateTestDataCommand extends Command
|
|
|
|
{
|
2024-07-29 19:38:54 +02:00
|
|
|
/**
|
|
|
|
* @var list<array{title: non-empty-string, description: non-empty-string, sys_language_uid: int<0, max>}>
|
|
|
|
*/
|
2024-05-07 14:21:17 +02:00
|
|
|
protected array $teaData = [
|
|
|
|
[
|
|
|
|
'title' => 'Darjeeling',
|
|
|
|
'description' => 'I love that tea!',
|
2024-07-29 17:21:27 +02:00
|
|
|
'sys_language_uid' => 0,
|
2024-05-07 14:21:17 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Earl Grey',
|
|
|
|
'description' => 'A nice tea!',
|
2024-07-29 17:21:27 +02:00
|
|
|
'sys_language_uid' => 0,
|
|
|
|
],
|
2024-05-07 14:21:17 +02:00
|
|
|
];
|
2024-07-29 19:39:11 +02:00
|
|
|
|
2024-05-07 14:21:17 +02:00
|
|
|
protected function configure(): void
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setHelp('Create test data for the tea extension in an already existing page (sysfolder).')
|
|
|
|
->addArgument(
|
2024-07-29 19:48:34 +02:00
|
|
|
'pageUid',
|
2024-05-07 14:21:17 +02:00
|
|
|
InputArgument::REQUIRED,
|
|
|
|
'Existing sysfolder page id.'
|
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'delete-data-before',
|
|
|
|
'd',
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Delete all tea data in the defined pid before creating new data.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
{
|
2024-07-30 11:24:13 +02:00
|
|
|
$pageUid = (int)$input->getArgument('pageUid') ?? 0;
|
|
|
|
\assert(\is_int($pageUid));
|
2024-05-07 14:21:17 +02:00
|
|
|
$deleteDataBefore = $input->getOption('delete-data-before') ?? false;
|
2024-07-30 11:24:13 +02:00
|
|
|
\assert(\is_bool($deleteDataBefore));
|
2024-05-07 14:21:17 +02:00
|
|
|
$table = 'tx_tea_domain_model_tea';
|
|
|
|
$connectionForTable = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
|
|
|
|
|
2024-07-29 17:21:27 +02:00
|
|
|
if ($deleteDataBefore) {
|
2024-05-07 14:21:17 +02:00
|
|
|
$query = $connectionForTable;
|
2024-07-29 19:48:34 +02:00
|
|
|
$query->delete($table, ['pid' => $pageUid], [Connection::PARAM_INT]);
|
|
|
|
$output->writeln(sprintf('Existing data in page %s deleted.', $pageUid));
|
2024-05-07 14:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$query = $connectionForTable;
|
|
|
|
foreach ($this->teaData as $item) {
|
2024-07-29 19:48:34 +02:00
|
|
|
$item = ['pid' => $pageUid, ...$item];
|
2024-07-29 17:21:27 +02:00
|
|
|
$query->insert(
|
|
|
|
$table,
|
2024-05-07 14:21:17 +02:00
|
|
|
$item
|
|
|
|
);
|
|
|
|
}
|
2024-07-29 19:48:34 +02:00
|
|
|
$output->writeln(sprintf('Test data in page %s created.', $pageUid));
|
2024-05-07 14:21:17 +02:00
|
|
|
|
2024-07-30 12:32:54 +02:00
|
|
|
$referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
|
|
|
|
$referenceIndex->updateIndex(0);
|
|
|
|
$output->writeln('Reference index updated.');
|
|
|
|
|
2024-05-07 14:21:17 +02:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|