Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions Classes/Controller/RandomdataCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions Classes/Event/RandomdataEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace WIND\Randomdata\Event;


final class RandomdataEvent
{

/**
* @var mixed
*/
private $name;

private $arguments;

public function __construct($name, $arguments)
{
$this->arguments = $arguments;
$this->name = $name;
}


/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

public function getArguments()
{
return $this->arguments;
}
}
71 changes: 30 additions & 41 deletions Classes/Service/RandomdataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,10 +62,6 @@ class RandomdataService
*/
protected $faker;

/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var array
Expand All @@ -81,13 +78,7 @@ class RandomdataService
*/
protected $newUid = 0;

/**
* @param ObjectManager $objectManager
*/
public function injectObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}


/**
* Generate random data
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -479,19 +469,18 @@ public function generateData($configurationKey, $field, array $fieldConfiguratio
}

/**
* Dispatch signal slot
* Dispatch Randomdata vent
*
* @param string $name
* @param array $arguments
* @throws InvalidSlotException
* @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));
}

/**
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 7 additions & 3 deletions ext_localconf.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
defined('TYPO3_MODE') or die();
defined('TYPO3') or die();

use TYPO3\CMS\Core\Http\ApplicationType;

call_user_func(static function () {

if (TYPO3_MODE === 'BE') {
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['randomdata']['allowedActions'] = ['insert', 'replace'];
}

});