Skip to content

Commit f5ccffd

Browse files
author
Rudy Gnodde
committed
[TASK] Rewrite Extbase Command Controller to Symfony Command Controller
1 parent 4686698 commit f5ccffd

File tree

6 files changed

+57
-32
lines changed

6 files changed

+57
-32
lines changed

Classes/Controller/RandomdataCommandController.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,49 @@
1515
*/
1616

1717
use Faker\Factory;
18-
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
use TYPO3\CMS\Core\Core\Bootstrap;
23+
use TYPO3\CMS\Core\Utility\GeneralUtility;
24+
use TYPO3\CMS\Extbase\Object\ObjectManager;
1925
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException;
2026
use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException;
21-
use \WIND\Randomdata\Exception\ConfigurationFileNotFoundException;
22-
use \WIND\Randomdata\Exception\FieldsNotFoundForItemException;
23-
use \WIND\Randomdata\Exception\PidNotFoundForItemException;
24-
use \WIND\Randomdata\Exception\TableNotFoundInTcaException;
25-
use \WIND\Randomdata\Exception\UnknownActionException;
26-
use \WIND\Randomdata\Exception\CountNotFoundForItemException;
27-
use \WIND\Randomdata\Exception\DataHandlerException;
28-
use \WIND\Randomdata\Exception\ProviderException;
27+
use WIND\Randomdata\Exception\ConfigurationFileNotFoundException;
28+
use WIND\Randomdata\Exception\FieldsNotFoundForItemException;
29+
use WIND\Randomdata\Exception\PidNotFoundForItemException;
30+
use WIND\Randomdata\Exception\TableNotFoundInTcaException;
31+
use WIND\Randomdata\Exception\UnknownActionException;
32+
use WIND\Randomdata\Exception\CountNotFoundForItemException;
33+
use WIND\Randomdata\Exception\DataHandlerException;
34+
use WIND\Randomdata\Exception\ProviderException;
2935
use WIND\Randomdata\Service\RandomdataService;
3036

3137
/**
3238
* Randomdata Command Controller
3339
*/
34-
class RandomdataCommandController extends CommandController
40+
class RandomdataCommandController extends Command
3541
{
3642
/**
37-
* Generate random data
43+
* Configure
3844
*
39-
* @param string $file YAML configuration file
40-
* @param string $locale Locale used to generate data
41-
* @param bool $quiet Only output errors if true
4245
* @return void
46+
*/
47+
public function configure()
48+
{
49+
$this->setDescription('Generate random data');
50+
$this->setHelp('This command allows your to create random data or replace existing data with random data');
51+
$this->addArgument('file', InputArgument::REQUIRED, 'YAML configuration file');
52+
$this->addArgument('locale', InputArgument::OPTIONAL, 'Locale used to generate data', Factory::DEFAULT_LOCALE);
53+
}
54+
55+
/**
56+
* Execute
57+
*
58+
* @param InputInterface $input
59+
* @param OutputInterface $output
60+
* @return int|void|null
4361
* @throws \RuntimeException
4462
* @throws ConfigurationFileNotFoundException
4563
* @throws FieldsNotFoundForItemException
@@ -52,10 +70,13 @@ class RandomdataCommandController extends CommandController
5270
* @throws InvalidSlotException
5371
* @throws InvalidSlotReturnException
5472
*/
55-
public function generateCommand($file, $locale = Factory::DEFAULT_LOCALE, $quiet = false)
73+
public function execute(InputInterface $input, OutputInterface $output)
5674
{
75+
Bootstrap::initializeBackendAuthentication();
76+
/** @var ObjectManager $objectManager */
77+
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
5778
/** @var RandomdataService $randomdataService */
58-
$randomdataService = $this->objectManager->get(RandomdataService::class);
59-
$randomdataService->generate($file, $locale, $quiet);
79+
$randomdataService = $objectManager->get(RandomdataService::class);
80+
$randomdataService->generate($input->getArgument('file'), $input->getArgument('locale'), $output);
6081
}
6182
}

Classes/Service/RandomdataService.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Faker\Factory;
1818
use Faker\Generator;
19+
use Symfony\Component\Console\Output\OutputInterface;
1920
use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
2021
use TYPO3\CMS\Core\Database\ConnectionPool;
2122
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
@@ -46,9 +47,9 @@ class RandomdataService
4647
protected $configuration;
4748

4849
/**
49-
* @var bool
50+
* @var OutputInterface
5051
*/
51-
protected $quiet = false;
52+
protected $output;
5253

5354
/**
5455
* @var DataHandler
@@ -78,7 +79,7 @@ public function injectObjectManager(ObjectManager $objectManager)
7879
*
7980
* @param string $configurationFile
8081
* @param string $locale
81-
* @param bool $quiet
82+
* @param OutputInterface $output
8283
* @return void
8384
* @throws \RuntimeException
8485
* @throws ConfigurationFileNotFoundException
@@ -92,11 +93,10 @@ public function injectObjectManager(ObjectManager $objectManager)
9293
* @throws InvalidSlotException
9394
* @throws InvalidSlotReturnException
9495
*/
95-
public function generate($configurationFile, $locale, $quiet = false)
96+
public function generate($configurationFile, $locale, $output = null)
9697
{
97-
$this->quiet = $quiet;
98+
$this->output = $output;
9899

99-
$GLOBALS['BE_USER']->user['admin'] = true;
100100
$this->dataHandler = GeneralUtility::makeInstance(DataHandler::class);
101101

102102
$this->faker = Factory::create($locale);
@@ -307,7 +307,6 @@ protected function generateAndInsertRecords($configurationKey, $table, $pid, arr
307307
}
308308

309309
$this->dataHandler->start($dataMap, []);
310-
$this->dataHandler->admin = true;
311310
$this->dataHandler->process_datamap();
312311

313312
if (!empty($this->dataHandler->errorLog)) {
@@ -345,7 +344,6 @@ protected function generateAndReplaceRecords($configurationKey, $table, $pid, ar
345344
}
346345

347346
$this->dataHandler->start($dataMap, []);
348-
$this->dataHandler->admin = true;
349347
$this->dataHandler->process_datamap();
350348

351349
if (!empty($this->dataHandler->errorLog)) {
@@ -416,8 +414,8 @@ protected function dispatchSignalSlot($name, array $arguments)
416414
*/
417415
protected function output($message)
418416
{
419-
if (!$this->quiet) {
420-
echo($message . "\n");
417+
if (!empty($this->output)) {
418+
$this->output->writeln($message);
421419
}
422420
}
423421
}

Configuration/Commands.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
return [
3+
'randomdata:generate' => [
4+
'class' => \WIND\Randomdata\Controller\RandomdataCommandController::class,
5+
],
6+
];

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ After installing randomdata in TYPO3 you can run it using the following command:
1515
vendor/bin/typo3 randomdata:generate configuration.yaml
1616
```
1717

18-
The command has 2 optional parameters:
18+
For more information about the command line options, use the following command:
1919

20-
- **--locale** The locale used to generate the data (default: en_US)
21-
- **--quiet** If set, the command will not display any messages other than errors
20+
```
21+
vendor/bin/typo3 help randomdata:generate
22+
```
2223

2324
The location of the configuration yaml file needs to be inside the site root.
2425

ext_emconf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'state' => 'stable',
1010
'uploadfolder' => false,
1111
'clearCacheOnLoad' => false,
12-
'version' => '1.0.0',
12+
'version' => '1.0.1',
1313
'constraints' => [
1414
'depends' => [
1515
'typo3' => '8.7.13-9.5.99',

ext_localconf.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
defined('TYPO3_MODE') or die();
33

44
if (TYPO3_MODE === 'BE') {
5-
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers']['Randomdata-Generate'] = \WIND\Randomdata\Controller\RandomdataCommandController::class;
65
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['randomdata']['allowedActions'] = ['insert', 'replace'];
76
}

0 commit comments

Comments
 (0)