|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Bmack\SiteImporter\Command; |
| 5 | + |
| 6 | +/* |
| 7 | + * This file is part of the Site Importer package. |
| 8 | + * |
| 9 | + * (c) Benni Mack <[email protected]> |
| 10 | + * |
| 11 | + * For the full copyright and license information, please view the LICENSE |
| 12 | + * file that was distributed with this source code. |
| 13 | + */ |
| 14 | + |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\Yaml\Yaml; |
| 20 | +use TYPO3\CMS\Core\Database\ConnectionPool; |
| 21 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 22 | + |
| 23 | +/** |
| 24 | + * Simple command to import records defined in a config yaml file into the database |
| 25 | + */ |
| 26 | +class SiteImportCommand extends Command |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var int |
| 30 | + */ |
| 31 | + const SUCCESS = 0; |
| 32 | + |
| 33 | + /** |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + protected function configure() |
| 37 | + { |
| 38 | + $this->setDescription('Imports database entries into the database, based on Yaml configuration'); |
| 39 | + $this->addArgument('file', InputArgument::REQUIRED, 'The file to import'); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Imports database entries into the database, based on Yaml configuration |
| 44 | + */ |
| 45 | + public function execute(InputInterface $input, OutputInterface $output) |
| 46 | + { |
| 47 | + $file = $input->getArgument('file'); |
| 48 | + |
| 49 | + $contents = Yaml::parse(file_get_contents($file)); |
| 50 | + $contents = $this->loadImports($contents); |
| 51 | + foreach ($contents as $config) { |
| 52 | + $mode = $config['mode'] ?? 'append'; |
| 53 | + $conn = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($config['table']); |
| 54 | + if ($mode === 'replace') { |
| 55 | + $conn->truncate($config['table']); |
| 56 | + $output->writeln('Emptied database table "'.$config['table'].'"'); |
| 57 | + } |
| 58 | + foreach ($config['entries'] ?? [] as $entry) { |
| 59 | + if ($mode === 'update' && isset($entry['uid'])) { |
| 60 | + $identifiers = ['uid' => $entry['uid']]; |
| 61 | + if ($conn->count('uid', $config['table'], $identifiers)) { |
| 62 | + $conn->update($config['table'], $entry, $identifiers); |
| 63 | + $output->writeln('Updated (mode=update, entry has uid): '.json_encode($entry).' to database table '.$config['table']); |
| 64 | + } else { |
| 65 | + $conn->insert($config['table'], $entry); |
| 66 | + $output->writeln('Added (mode=update, entry does not have uid): '.json_encode($entry).' to database table '.$config['table']); |
| 67 | + } |
| 68 | + } else { |
| 69 | + $conn->insert($config['table'], $entry); |
| 70 | + $output->writeln('Added '.json_encode($entry).' to database table '.$config['table']); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return self::SUCCESS; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Load recursively import files declared in yml files |
| 80 | + * |
| 81 | + * imports: |
| 82 | + * - { resource: 'base.site-importer.yml' } |
| 83 | + * |
| 84 | + * @param array $contents |
| 85 | + */ |
| 86 | + private function loadImports(array $contents): array |
| 87 | + { |
| 88 | + if ( ! empty($contents['imports'])) { |
| 89 | + foreach ($contents['imports'] as $import) { |
| 90 | + $importedContent = Yaml::parseFile($import['resource']); |
| 91 | + if ( ! empty($importedContent) && is_array($importedContent)) { |
| 92 | + $importedContent = $this->loadImports($importedContent); |
| 93 | + $contents = array_merge($contents, $importedContent); |
| 94 | + } |
| 95 | + } |
| 96 | + unset($contents['imports']); |
| 97 | + } |
| 98 | + |
| 99 | + return $contents; |
| 100 | + } |
| 101 | +} |
0 commit comments