Skip to content

Commit 6bd766d

Browse files
authored
Merge pull request #12 from sabbelasichon/feature/TYPO3-9-10-Compatibility
Add TYPO3 9 and 10 compatibility version
2 parents 5708001 + c58f775 commit 6bd766d

File tree

6 files changed

+120
-126
lines changed

6 files changed

+120
-126
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor

Configuration/Commands.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'siteimport:fromfile' => [
7+
'class' => \Bmack\SiteImporter\Command\SiteImportCommand::class,
8+
'schedulable' => false,
9+
'runLevel' => \Helhum\Typo3Console\Core\Booting\RunLevel::LEVEL_MINIMAL,
10+
'bootingSteps' => [],
11+
],
12+
];

Configuration/Console/Commands.php

-11
This file was deleted.

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
}
1616
},
1717
"require": {
18-
"php": ">=7.0 <7.5",
18+
"php": ">=7.2 <7.5",
1919
"symfony/yaml": "^2.7 || ^3.0 || ^4.0",
20-
"typo3/cms-core": "^7.6 || ^8.7 || ^9.0",
21-
"helhum/typo3-console": "^4.0 || ^5.0"
22-
}
20+
"typo3/cms-core": "^9.0 || ^10.0",
21+
"helhum/typo3-console": "^5.0 || ^6.0",
22+
"ext-json": "*"
23+
}
2324
}

src/Command/SiteImportCommand.php

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

src/Command/SiteImportCommandController.php

-111
This file was deleted.

0 commit comments

Comments
 (0)