Skip to content

Commit c7ac90d

Browse files
committed
Upgrade code to SS6
1 parent e180f14 commit c7ac90d

11 files changed

+81
-87
lines changed

composer.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@
2424
"require": {
2525
"php": "^8.2",
2626
"ext-zip": "*",
27-
"silverstripe/framework": "^5.2",
28-
"tractorcow/silverstripe-fluent": "^7",
29-
"lekoala/silverstripe-pure-modal": "^1.2.1",
30-
"lekoala/silverstripe-cms-actions": "^1.7.5",
27+
"silverstripe/framework": "^6",
28+
"tractorcow/silverstripe-fluent": "^8",
29+
"lekoala/silverstripe-pure-modal": "^2",
30+
"lekoala/silverstripe-cms-actions": "^2",
3131
"openai-php/client": "^0.10"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^9",
35-
"silverleague/ideannotator": "^3",
36-
"squizlabs/php_codesniffer": "^3.6",
37-
"wernerkrauss/silverstripe-rector": "^0.1"
35+
"squizlabs/php_codesniffer": "^3",
36+
"wernerkrauss/silverstripe-rector": "^1"
3837
},
3938
"autoload": {
4039
"psr-4": {

rector.php

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,31 @@
1212
use Rector\Set\ValueObject\LevelSetList;
1313
use Rector\Set\ValueObject\SetList;
1414

15-
return static function (RectorConfig $rectorConfig): void {
16-
$rectorConfig->paths([
15+
16+
return RectorConfig::configure()
17+
->withPaths([
1718
__DIR__ . '/src/_config.php',
1819
__DIR__ . '/src',
1920
__DIR__ . '/tests/php'
20-
]);
21-
$rectorConfig->autoloadPaths([
22-
]);
23-
// $rectorConfig->bootstrapFiles([
24-
// __DIR__ . '/vendor/stevie-mayhew/silverstripe-svg/code/SVGTemplate.php'
25-
// ]);
26-
27-
28-
// // register a single rule
29-
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
30-
//
31-
// // define sets of rules
32-
$rectorConfig->sets([
21+
])
22+
->withPreparedSets(deadCode: true)
23+
->withSets([
24+
//rector lists
3325
LevelSetList::UP_TO_PHP_82,
3426
SetList::CODE_QUALITY,
3527
SetList::CODING_STYLE,
3628
SetList::TYPE_DECLARATION,
29+
//silverstripe rector
3730
SilverstripeSetList::CODE_STYLE,
38-
SilverstripeLevelSetList::UP_TO_SS_5_2
39-
]);
31+
SilverstripeLevelSetList::UP_TO_SS_6_0
32+
])
33+
->withRules([
34+
EnsureTableNameIsSetRector::class,
35+
])
4036

41-
//Silverstripe rules
42-
$rectorConfig->rule(EnsureTableNameIsSetRector::class);
43-
$rectorConfig->rule(UseCreateRector::class);
4437

45-
// example how to configure rector for custom @config properties
46-
47-
$rectorConfig->ruleWithConfiguration(
48-
AddConfigPropertiesRector::class,
49-
[
50-
\SilverStripe\Admin\LeftAndMain::class => [
51-
// 'url_segment'
52-
],
53-
\SilverStripe\Forms\Form::class => [
54-
'allowed_actions',
55-
'required_fields',
56-
],
57-
]
58-
);
59-
};
38+
// any rules that are included in the selected sets you want to skip
39+
->withSkip([
40+
// ClassPropertyAssignToConstructorPromotionRector::class,
41+
// ReturnNeverTypeRector::class
42+
]);

src/Control/AITranslateController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Netwerkstatt\FluentExIm\Control;
44

5+
use SilverStripe\Model\List\ArrayList;
56
use Netwerkstatt\FluentExIm\Extension\AutoTranslate;
6-
use SilverStripe\Admin\LeftAndMain;
77
use SilverStripe\Control\Controller;
88
use SilverStripe\Control\HTTPRequest;
99
use SilverStripe\Forms\CheckboxField;
@@ -12,7 +12,6 @@
1212
use SilverStripe\Forms\FormAction;
1313
use SilverStripe\Forms\HiddenField;
1414
use SilverStripe\Forms\LiteralField;
15-
use SilverStripe\ORM\ArrayList;
1615
use SilverStripe\View\Requirements;
1716
use SilverStripe\View\SSViewer;
1817
use TractorCow\Fluent\Model\Locale;

src/Extension/AutoTranslate.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Netwerkstatt\FluentExIm\Extension;
44

5+
use SilverStripe\Core\Extension;
6+
use RuntimeException;
7+
use JsonException;
58
use LeKoala\CmsActions\SilverStripeIcons;
69
use LeKoala\PureModal\PureModal;
710
use Netwerkstatt\FluentExIm\Helper\FluentHelper;
@@ -12,7 +15,6 @@
1215
use SilverStripe\Core\Environment;
1316
use SilverStripe\Forms\CheckboxField;
1417
use SilverStripe\Forms\FieldList;
15-
use SilverStripe\ORM\DataExtension;
1618
use SilverStripe\ORM\DataObject;
1719
use SilverStripe\ORM\FieldType\DBDatetime;
1820
use SilverStripe\Versioned\Versioned;
@@ -21,7 +23,7 @@
2123
use TractorCow\Fluent\Model\Locale;
2224
use TractorCow\Fluent\State\FluentState;
2325

24-
class AutoTranslate extends DataExtension
26+
class AutoTranslate extends Extension
2527
{
2628
/**
2729
* @config
@@ -128,7 +130,7 @@ public function onAfterUpdateCMSActions(FieldList $actions)
128130
* @param $data
129131
* @param $form
130132
* @return AITranslationStatus[]
131-
* @throws \JsonException
133+
* @throws JsonException
132134
*/
133135
public function doRecursiveAutoTranslate($data, $form): array
134136
{
@@ -151,15 +153,14 @@ public function doRecursiveAutoTranslate($data, $form): array
151153

152154

153155
/**
154-
* @throws \RuntimeException
155-
* @throws \JsonException
156+
* @throws RuntimeException
157+
* @throws JsonException
156158
* @todo: currently only chatgpt is supported, make it more generic
157-
*
158159
*/
159160
public function autoTranslate(bool $doPublish = false, bool $forceTranslation = false): AITranslationStatus
160161
{
161162
$this->checkIfAutoTranslateFieldsAreTranslatable();
162-
$status = \Netwerkstatt\FluentExIm\Translator\AITranslationStatus::create($this->getOwner());
163+
$status = AITranslationStatus::create($this->getOwner());
163164

164165
/** @var DataObject $owner */
165166
$owner = $this->getOwner();
@@ -229,12 +230,12 @@ public function hasDefaultLocale(): bool
229230
/**
230231
* Check if the required fields are configured as translated fields
231232
* @return void
232-
* @throws \RuntimeException
233+
* @throws RuntimeException
233234
*/
234235
public function checkIfAutoTranslateFieldsAreTranslatable()
235236
{
236237
if (!$this->getOwner()->hasExtension(FluentExtension::class)) {
237-
throw new \RuntimeException($this->getOwner()->ClassName . ' does not have FluentExtension');
238+
throw new RuntimeException($this->getOwner()->ClassName . ' does not have FluentExtension');
238239
}
239240

240241
foreach (['IsAutoTranslated', 'LastTranslation'] as $field) {
@@ -246,7 +247,7 @@ public function checkIfAutoTranslateFieldsAreTranslatable()
246247
}
247248

248249
if (!$isLocalised) {
249-
throw new \RuntimeException($this->getOwner()->ClassName . ' does not have ' . $field . ' as translatable field');
250+
throw new RuntimeException($this->getOwner()->ClassName . ' does not have ' . $field . ' as translatable field');
250251
}
251252
}
252253
}
@@ -335,7 +336,7 @@ private function performTranslation(
335336

336337
public static function getTranslator(): Translatable
337338
{
338-
if (!self::$translator instanceof \Netwerkstatt\FluentExIm\Translator\Translatable) {
339+
if (!self::$translator instanceof Translatable) {
339340
self::$translator = self::getDefaultTranslator();
340341
}
341342

@@ -350,18 +351,18 @@ public static function setTranslator(Translatable $translator): void
350351
/**
351352
* Fallback if no translator is set. Use ChatGPT for now
352353
*
353-
* @throws \RuntimeException
354+
* @throws RuntimeException
354355
* @return Translatable
355356
*/
356357
public static function getDefaultTranslator(): Translatable
357358
{
358359
//@todo use dependency injection later
359360
$apiKey = Environment::getEnv('CHATGPT_API_KEY');
360361
if (!$apiKey) {
361-
throw new \RuntimeException('No API Key found');
362+
throw new RuntimeException('No API Key found');
362363
}
363364

364-
self::$translator = \Netwerkstatt\FluentExIm\Translator\ChatGPTTranslator::create($apiKey);
365+
self::$translator = ChatGPTTranslator::create($apiKey);
365366
return self::$translator;
366367
}
367368
}

src/Extension/LocaleAdmin.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Netwerkstatt\FluentExIm\Extension;
44

5+
use Exception;
6+
use ZipArchive;
57
use Netwerkstatt\FluentExIm\Helper\FluentImportHelper;
68
use SilverStripe\Control\HTTPRequest;
79
use SilverStripe\Core\Extension;
@@ -107,14 +109,14 @@ public function doImport($data, Form $form)
107109

108110
try {
109111
$count = $this->handleYmlFile($content, $data['Locale'], $shouldPublish);
110-
} catch (\Exception $e) {
112+
} catch (Exception $e) {
111113
$form->sessionMessage($e->getMessage(), 'bad');
112114
return $this->getOwner()->redirectBack();
113115
}
114116
}
115117

116118
if (str_ends_with((string) $file['name'], '.zip')) {
117-
$zip = new \ZipArchive();
119+
$zip = new ZipArchive();
118120
$res = $zip->open($file['tmp_name']);
119121
$errorMessages = [];
120122
if ($res === true) {
@@ -129,7 +131,7 @@ public function doImport($data, Form $form)
129131

130132
try {
131133
$count += $this->handleYmlFile($content, $data['Locale'], $shouldPublish);
132-
} catch (\Exception $e) {
134+
} catch (Exception $e) {
133135
$errorMessages[$filename] = $e->getMessage();
134136
}
135137
}

src/Helper/FluentExportHelper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Netwerkstatt\FluentExIm\Helper;
44

5+
use RuntimeException;
6+
use ZipArchive;
57
use Symfony\Component\Yaml\Yaml;
68

79
class FluentExportHelper
@@ -48,7 +50,7 @@ public static function createYmlFile(string $className, array $data, string $loc
4850

4951
$dir = TEMP_PATH . '/fluent-ex-im/';
5052
if (!file_exists($dir) && (!mkdir($dir, 0777, true) && !is_dir($dir))) {
51-
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
53+
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir));
5254
}
5355

5456
$filename = $dir . $className . '_' . $locale . '.yml';
@@ -86,9 +88,9 @@ public static function generateZipArchive(array $filenames, string $locale): str
8688
$filename = sprintf('fluent-export-%s-%s.zip', $locale, date('Y-m-d-H-i-s'));
8789
$zipFilename = TEMP_PATH . '/fluent-ex-im/' . $filename;
8890

89-
$zip = new \ZipArchive();
91+
$zip = new ZipArchive();
9092

91-
$zip->open($zipFilename, \ZipArchive::CREATE);
93+
$zip->open($zipFilename, ZipArchive::CREATE);
9294
foreach ($filenames as $key => $filename) {
9395
$zip->addFile($filename, basename((string)$filename));
9496
}

src/Helper/FluentHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Netwerkstatt\FluentExIm\Helper;
44

5+
use InvalidArgumentException;
56
use SilverStripe\Core\ClassInfo;
67
use SilverStripe\Core\Config\Config;
78
use SilverStripe\ORM\DataObject;
@@ -20,7 +21,7 @@ public static function getTranslatedFieldsForClass(string $className)
2021
{
2122
$class = singleton($className);
2223
if (!$class->hasExtension(FluentExtension::class)) {
23-
throw new \InvalidArgumentException('Class ' . $className . ' does not have FluentExtension');
24+
throw new InvalidArgumentException('Class ' . $className . ' does not have FluentExtension');
2425
}
2526

2627
$fields = $class->getLocalisedTables();

src/Helper/FluentImportHelper.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Netwerkstatt\FluentExIm\Helper;
44

5+
use InvalidArgumentException;
6+
use RuntimeException;
57
use SilverStripe\ORM\DataObject;
68
use SilverStripe\Versioned\Versioned;
79
use TractorCow\Fluent\Extension\FluentExtension;
@@ -23,7 +25,7 @@ public static function importTranslationsForClass(string $className, array $tran
2325
{
2426
$class = singleton($className);
2527
if (!$class->hasExtension(FluentExtension::class)) {
26-
throw new \InvalidArgumentException('Class ' . $className . ' does not have FluentExtension');
28+
throw new InvalidArgumentException('Class ' . $className . ' does not have FluentExtension');
2729
}
2830

2931
$fieldsToTranslate = FluentHelper::getTranslatedFieldsForClass($className);
@@ -32,15 +34,15 @@ public static function importTranslationsForClass(string $className, array $tran
3234
/** @var ?Locale $localeObj */
3335
$localeObj = Locale::get()->filter('Locale', $locale)->first();
3436
if ($localeObj === null || !$localeObj->exists()) {
35-
throw new \InvalidArgumentException('Locale ' . $locale . ' not found');
37+
throw new InvalidArgumentException('Locale ' . $locale . ' not found');
3638
}
3739

3840
$translated = [];
3941
foreach ($translations as $translation) {
4042
/** @var ?DataObject $dataObject */
4143
$dataObject = DataObject::get($className)->byID($translation['ID']);
4244
if ($dataObject === null || !$dataObject->exists()) {
43-
throw new \InvalidArgumentException('DataObject of class ' . $className . ' with ID ' . $translation['ID'] . ' not found');
45+
throw new InvalidArgumentException('DataObject of class ' . $className . ' with ID ' . $translation['ID'] . ' not found');
4446
}
4547

4648
FluentState::singleton()
@@ -55,7 +57,7 @@ public static function importTranslationsForClass(string $className, array $tran
5557
$validationResult = $dataObject->validate();
5658
if (!$validationResult->isValid()) {
5759
$messages = implode(', ', array_map(static fn($message) => $message['message'], $validationResult->getMessages()));
58-
throw new \RuntimeException('Validation failed for ' . $dataObject->ClassName . ' with ID ' . $dataObject->ID . ': ' . $messages);
60+
throw new RuntimeException('Validation failed for ' . $dataObject->ClassName . ' with ID ' . $dataObject->ID . ': ' . $messages);
5961
}
6062

6163
$dataObject->write();
@@ -74,13 +76,13 @@ public static function importTranslationsForClass(string $className, array $tran
7476
public static function validateLocaleTranslationData(array $translationData): bool
7577
{
7678
if (self::$locale === '') {
77-
throw new \RuntimeException('Locale must be set before importing translations');
79+
throw new RuntimeException('Locale must be set before importing translations');
7880
}
7981

8082
foreach (array_keys($translationData) as $locale) {
8183
//check if locale exists and is locale of current object
8284
if ($locale !== self::$locale) {
83-
throw new \RuntimeException(sprintf('Locale %s in file does not match import locale %s', $locale, self::$locale));
85+
throw new RuntimeException(sprintf('Locale %s in file does not match import locale %s', $locale, self::$locale));
8486
}
8587
}
8688

src/Task/AIAutoTranslate.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Netwerkstatt\FluentExIm\Task;
44

5-
use Bramus\Monolog\Formatter\ColoredLineFormatter;
5+
use RuntimeException;
6+
use InvalidArgumentException;
7+
use Exception;
68
use Monolog\Formatter\LineFormatter;
79
use Monolog\Handler\FilterHandler;
810
use Monolog\Handler\StreamHandler;
@@ -74,11 +76,11 @@ public function run($request)
7476
}
7577

7678
if ($currentLocale !== $defaultLocale) {
77-
throw new \RuntimeException('Please run this task in default locale');
79+
throw new RuntimeException('Please run this task in default locale');
7880
}
7981

8082
if ($request->getVar('do_publish') === null) {
81-
throw new \InvalidArgumentException('Please provide do_publish parameter. 1 will publish all translated objects, 0 will only write to stage');
83+
throw new InvalidArgumentException('Please provide do_publish parameter. 1 will publish all translated objects, 0 will only write to stage');
8284
}
8385

8486
$doPublish = (bool) $request->getVar('do_publish');
@@ -126,7 +128,7 @@ private function outputStatus(AITranslationStatus $status)
126128

127129
/**
128130
* Taken from \SilverStripe\Dev\Tasks\MigrateFileTask
129-
* @throws \Exception
131+
* @throws Exception
130132
*/
131133
protected function addLogHandlers()
132134
{

0 commit comments

Comments
 (0)