diff --git a/Classes/Controller/RandomdataCommandController.php b/Classes/Controller/RandomdataCommandController.php index da9a5b5..8d619e8 100755 --- a/Classes/Controller/RandomdataCommandController.php +++ b/Classes/Controller/RandomdataCommandController.php @@ -15,25 +15,25 @@ */ use Faker\Factory; +use TYPO3\CMS\Extbase\Exception; +use TYPO3\CMS\Core\Core\Bootstrap; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; use Symfony\Component\Console\Command\Command; +use WIND\Randomdata\Service\RandomdataService; +use WIND\Randomdata\Exception\ProviderException; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use WIND\Randomdata\Exception\DataHandlerException; use Symfony\Component\Console\Output\OutputInterface; -use TYPO3\CMS\Core\Core\Bootstrap; -use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Extbase\Exception; -use TYPO3\CMS\Extbase\Object\ObjectManager; -use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException; -use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException; -use WIND\Randomdata\Exception\ConfigurationFileNotFoundException; -use WIND\Randomdata\Exception\FieldsNotFoundForItemException; +use WIND\Randomdata\Exception\UnknownActionException; use WIND\Randomdata\Exception\PidNotFoundForItemException; use WIND\Randomdata\Exception\TableNotFoundInTcaException; -use WIND\Randomdata\Exception\UnknownActionException; use WIND\Randomdata\Exception\CountNotFoundForItemException; -use WIND\Randomdata\Exception\DataHandlerException; -use WIND\Randomdata\Exception\ProviderException; -use WIND\Randomdata\Service\RandomdataService; +use WIND\Randomdata\Exception\FieldsNotFoundForItemException; +use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException; +use WIND\Randomdata\Exception\ConfigurationFileNotFoundException; +use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException; /** * Randomdata Command Controller @@ -72,13 +72,12 @@ public function configure() * @throws InvalidSlotReturnException * @throws Exception */ - public function execute(InputInterface $input, OutputInterface $output) + public function execute(InputInterface $input, OutputInterface $output): int { Bootstrap::initializeBackendAuthentication(); - /** @var ObjectManager $objectManager */ - $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + /** @var RandomdataService $randomdataService */ - $randomdataService = $objectManager->get(RandomdataService::class); + $randomdataService = GeneralUtility::makeInstance(RandomdataService::class); $randomdataService->generate($input->getArgument('file'), $input->getArgument('locale'), $output); return 0; diff --git a/Classes/Event/RandomdataEvent.php b/Classes/Event/RandomdataEvent.php new file mode 100644 index 0000000..629fc81 --- /dev/null +++ b/Classes/Event/RandomdataEvent.php @@ -0,0 +1,37 @@ +arguments = $arguments; + $this->name = $name; + } + + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + public function getArguments() + { + return $this->arguments; + } +} \ No newline at end of file diff --git a/Classes/Service/RandomdataService.php b/Classes/Service/RandomdataService.php index 74cd9e6..d99f126 100755 --- a/Classes/Service/RandomdataService.php +++ b/Classes/Service/RandomdataService.php @@ -16,25 +16,26 @@ use Faker\Factory; use Faker\Generator; -use Symfony\Component\Console\Output\OutputInterface; -use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader; -use TYPO3\CMS\Core\Database\ConnectionPool; -use TYPO3\CMS\Core\Database\Query\QueryBuilder; -use TYPO3\CMS\Core\DataHandling\DataHandler; use TYPO3\CMS\Core\Utility\GeneralUtility; +use WIND\Randomdata\Event\RandomdataEvent; +use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Extbase\Object\ObjectManager; -use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; -use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException; -use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException; -use WIND\Randomdata\Exception\ConfigurationFileNotFoundException; -use WIND\Randomdata\Exception\CountNotFoundForItemException; +use TYPO3\CMS\Core\DataHandling\DataHandler; +use TYPO3\CMS\Core\Database\Query\QueryBuilder; +use WIND\Randomdata\Provider\ProviderInterface; +use WIND\Randomdata\Exception\ProviderException; +use TYPO3\CMS\Core\EventDispatcher\EventDispatcher; use WIND\Randomdata\Exception\DataHandlerException; -use WIND\Randomdata\Exception\FieldsNotFoundForItemException; +use Symfony\Component\Console\Output\OutputInterface; +use WIND\Randomdata\Exception\UnknownActionException; +use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader; use WIND\Randomdata\Exception\PidNotFoundForItemException; -use WIND\Randomdata\Exception\ProviderException; use WIND\Randomdata\Exception\TableNotFoundInTcaException; -use WIND\Randomdata\Exception\UnknownActionException; -use WIND\Randomdata\Provider\ProviderInterface; +use WIND\Randomdata\Exception\CountNotFoundForItemException; +use WIND\Randomdata\Exception\FieldsNotFoundForItemException; +use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException; +use WIND\Randomdata\Exception\ConfigurationFileNotFoundException; +use TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException; /** * Randomdata Service @@ -61,10 +62,6 @@ class RandomdataService */ protected $faker; - /** - * @var ObjectManager - */ - protected $objectManager; /** * @var array @@ -81,13 +78,7 @@ class RandomdataService */ protected $newUid = 0; - /** - * @param ObjectManager $objectManager - */ - public function injectObjectManager(ObjectManager $objectManager) - { - $this->objectManager = $objectManager; - } + /** * Generate random data @@ -105,8 +96,6 @@ public function injectObjectManager(ObjectManager $objectManager) * @throws CountNotFoundForItemException * @throws DataHandlerException * @throws ProviderException - * @throws InvalidSlotException - * @throws InvalidSlotReturnException */ public function generate($configurationFile, $locale, $output = null) { @@ -179,7 +168,7 @@ protected function loadConfigurationFile($configurationFile) } /** @var YamlFileLoader $yamlLoader */ - $yamlLoader = $this->objectManager->get(YamlFileLoader::class); + $yamlLoader = GeneralUtility::makeInstance(YamlFileLoader::class); $this->configuration = $yamlLoader->load($configurationFile); } @@ -215,7 +204,7 @@ protected function generateItem($configurationKey, array $itemConfiguration) $this->generateAndReplaceRecords($configurationKey, $table, $pid, $fields, $itemConfiguration); break; default: - $this->dispatchSignalSlot('generateItemCustomAction', [$configurationKey, $table, $pid, $action, $fields, $itemConfiguration, $this]); + $this->dispatchRandomdataEvent('generateItemCustomAction', [$configurationKey, $table, $pid, $action, $fields, $itemConfiguration, $this]); break; } } catch(\Throwable $e) { @@ -269,9 +258,9 @@ protected function getItemPid($configurationKey, array $itemConfiguration) $pageQueryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); $page = $pageQueryBuilder->count('*')->from('pages')->where( $pageQueryBuilder->expr()->eq('uid', $pid) - )->execute()->fetchColumn(0); - - if ($page !== 1) { + )->executeQuery()->fetchFirstColumn(); + + if ($page[0] !== 1) { throw new PidNotFoundForItemException('Page with uid "' . $pid . '" not found in database for item "' . $configurationKey . '"', 1554380475); } } @@ -403,15 +392,16 @@ protected function generateAndReplaceRecords($configurationKey, $table, $pid, ar { /** @var QueryBuilder $queryBuilder */ $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table); - $records = $queryBuilder->select('uid')->from($table)->where($queryBuilder->expr()->eq('pid', $pid))->execute(); - + $records = $queryBuilder->select('uid')->from($table)->where($queryBuilder->expr()->eq('pid', $pid))->executeQuery()->fetchAllAssociative(); + if (!empty($records)) { $dataMap = [$table => []]; - + $this->addToDataMap = []; $this->addToCmdMap = []; - + foreach ($records as $record) { + $data = []; foreach ($fields as $field => $fieldConfiguration) { $fieldConfiguration['__table'] = $table; @@ -479,7 +469,7 @@ public function generateData($configurationKey, $field, array $fieldConfiguratio } /** - * Dispatch signal slot + * Dispatch Randomdata vent * * @param string $name * @param array $arguments @@ -487,11 +477,10 @@ public function generateData($configurationKey, $field, array $fieldConfiguratio * @throws InvalidSlotReturnException * @return void */ - protected function dispatchSignalSlot($name, array $arguments) + protected function dispatchRandomdataEvent($name, array $arguments) { - /** @var Dispatcher $signalSlotDispatcher */ - $signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class); - $signalSlotDispatcher->dispatch(__CLASS__, $name, $arguments); + $eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class); + $eventDispatcher->dispatch(new RandomdataEvent($name, $arguments)); } /** diff --git a/README.md b/README.md index 156ee2c..4655aff 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,14 @@ TYPO3 extensions to generate new random data or replace existing data with rando This extensions uses https://github.com/FakerPHP/Faker and was inspired by https://github.com/georgringer/faker. Thanks go out to the builders, contributors and maintainers of those projects. ## Requirements -- TYPO3 CMS 9.5, 10.4 or 11.5 -- PHP 7.2+ +- TYPO3 11.5, 12.4 or 13.4 +- PHP 8.1+ - Licence: GPL 3.0 +## install +``` +composer require windinternet/randomdata +``` ## Manual After installing randomdata in TYPO3 you can run it using the following command: @@ -207,4 +211,4 @@ You can create a custom provider from your own extension by adding a class which You can set your custom provider in the configuration yaml file by setting the full class name in the `provider` option. For example `provider: \My\Custom\Provider` ## Custom action -If you need anything other than `insert` or `replace` as action, you can use the `generateItemCustomAction` signal slot. You also need to set your action in the `$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['randomdata']['allowedActions']` array. +If you need anything other than `insert` or `replace` as action, you can use the `RandomdataEvent` Event. You also need to set your action in the `$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['randomdata']['allowedActions']` array. diff --git a/composer.json b/composer.json index 2cc6274..940facc 100755 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "typo3-cms-extension", "license": "GPL-3.0-or-later", "require": { - "typo3/cms-core": "^9.5.30 || ^10.4.20 || ^11.3.3", + "typo3/cms-core": "^11.5 || ^12.4 || ^13.4", "fakerphp/faker": "^1.19" }, "replace": { diff --git a/ext_localconf.php b/ext_localconf.php index 7cd2e96..7f6d74d 100755 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -1,6 +1,10 @@