diff --git a/README.md b/README.md
index ecb4196..775a3fc 100644
--- a/README.md
+++ b/README.md
@@ -70,12 +70,12 @@ ID folder. Код можно увидеть в адресе console.yandex.cloud
### Перевести с помощью очередей (Queues)
> \KorElf\TranslateLaravel\Facades\Translate::runJob(
->
\KorElf\TranslateLaravel\DTO\RunTranslateDto \$params,
->
\KorElf\TranslateLaravel\Contracts\TranslationCompletedListener \$completedListener
+>
\KorElf\TranslateLaravel\DTO\RunTranslateDto \$params,
+>
\KorElf\TranslateLaravel\DTO\AfterTranslateDto \$afterTranslateDto
): \Illuminate\Foundation\Bus\PendingDispatch
**\$params** - параметры перевода.
-
**\$completedListener** - после завершения перевода отправляет результат в этот объект. Объект должен соблюдать контракт **\KorElf\TranslateLaravel\Contracts\TranslationCompletedListener**.
+
**\$afterTranslateDto** - после завершения перевода отправляет результат в объект \$afterTranslateDto->\$className. Объект должен соблюдать контракт **\KorElf\TranslateLaravel\Contracts\TranslationCompletedListener**.
**Пример:**
> Создаём файл TranslationListener например в папке app/Services
@@ -89,25 +89,41 @@ ID folder. Код можно увидеть в адресе console.yandex.cloud
>
> final class TranslationListener implements TranslationCompletedListener
> {
-> public function onTranslationCompleted(array $translatedText): void
+> /**
+> * Объекты создаются и пробрасываются автоматически через контейнер `$application->make`,
+> * благодаря механизму автоматического внедрения зависимостей (Dependency Injection).
+> * Вы можете передавать свои собственные классы, поместив их в качестве параметров конструктора.
+> * Контейнер создаст эти объекты автоматически, основываясь на разрешении типа (type-hint).
+> */
+> // public function __construct(
+> // private readonly SaveContentCommand $saveContentCommand
+> // ) { }
+>
+> public function onTranslationCompleted(array $translatedText, array $data = []): void
> {
+> Log::info($data);
> foreach ($translatedText as $translatedTextKey => $translatedTextValue) {
> Log::info($translatedTextKey . ': ' . $translatedTextValue);
> }
+> // $this->saveContentCommand->execute($data['contentId'], $translatedText);
> }
> }
>
> Потом например в каком-то контроллере (а лучше конечно в сервисе) пишем такой метод
>
-> public function sendingForTranslation(\App\Services\TranslationListener $translationListener): View
+> public function sendingForTranslation(): View
> {
> // Вначале создаём объект с параметрами
> $params = (new \KorElf\TranslateLaravel\DTO\RunTranslateDto)
> ->addParamText('title', 'Заголовок', 'en', 'ru')
> ->addParamHtml('content', '
Привет, Мир!
', 'en', 'ru'); > +> $translationCompletedListener = \App\Services\TranslationListener::class; +> $data = ['contentId' => 1]; +> $afterTranslateDto = \KorElf\TranslateLaravel\DTO\AfterTranslateDto($translationCompletedListener, $data); +> > // Отправляем на очередь -> \KorElf\TranslateLaravel\Facades\Translate::runJob($params, $translationListener); +> \KorElf\TranslateLaravel\Facades\Translate::runJob($params, $afterTranslateDto); > > return view('success'); > } diff --git a/src/Contracts/TranslationCompletedListener.php b/src/Contracts/TranslationCompletedListener.php index e899ee1..47048ff 100644 --- a/src/Contracts/TranslationCompletedListener.php +++ b/src/Contracts/TranslationCompletedListener.php @@ -4,5 +4,5 @@ namespace KorElf\TranslateLaravel\Contracts; interface TranslationCompletedListener { - public function onTranslationCompleted(array $translatedText): void; + public function onTranslationCompleted(array $translatedText, array $data = []): void; } \ No newline at end of file diff --git a/src/DTO/AfterTranslateDto.php b/src/DTO/AfterTranslateDto.php new file mode 100644 index 0000000..2290e26 --- /dev/null +++ b/src/DTO/AfterTranslateDto.php @@ -0,0 +1,32 @@ + + */ + public function getClassName(): string + { + return $this->className; + } + + public function getData(): array + { + return $this->data; + } + + +} \ No newline at end of file diff --git a/src/Facades/Translate.php b/src/Facades/Translate.php index 4d92399..352e220 100644 --- a/src/Facades/Translate.php +++ b/src/Facades/Translate.php @@ -3,7 +3,7 @@ namespace KorElf\TranslateLaravel\Facades; use Illuminate\Support\Facades\Facade; -use KorElf\TranslateLaravel\Contracts\TranslationCompletedListener; +use KorElf\TranslateLaravel\DTO\AfterTranslateDto; use KorElf\TranslateLaravel\DTO\RunTranslateDto; /** @@ -11,7 +11,7 @@ use KorElf\TranslateLaravel\DTO\RunTranslateDto; * @method static \KorElf\TranslateLaravel\Contracts\Translate resolve(string $name) * @method static string|array translateText(string|array $text, string $targetLanguageCode, ?string $sourceLanguageCode = null) * @method static string|array translateHtml(string|array $text, string $targetLanguageCode, ?string $sourceLanguageCode = null) - * @method static \Illuminate\Foundation\Bus\PendingDispatch runJob(RunTranslateDto $params, TranslationCompletedListener $completedListener) + * @method static \Illuminate\Foundation\Bus\PendingDispatch runJob(RunTranslateDto $params, AfterTranslateDto $afterTranslateDto) * @method static \KorElf\TranslateLaravel\DTO\Languages listLanguages() * @method static string getDefaultDriver() * @method static void setDefaultDriver(string $name) diff --git a/src/Jobs/AfterTranslate.php b/src/Jobs/AfterTranslate.php index ccbdee7..5b3665b 100644 --- a/src/Jobs/AfterTranslate.php +++ b/src/Jobs/AfterTranslate.php @@ -5,10 +5,11 @@ namespace KorElf\TranslateLaravel\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeEncrypted; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Application; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\Cache; -use KorElf\TranslateLaravel\Contracts\TranslationCompletedListener; +use KorElf\TranslateLaravel\DTO\AfterTranslateDto; use KorElf\TranslateLaravel\DTO\Translated; use KorElf\TranslateLaravel\Exceptions\AfterTranslateException; @@ -20,15 +21,12 @@ final class AfterTranslate implements ShouldQueue, ShouldBeEncrypted * Create a new job instance. */ public function __construct( - private readonly string $groupName, - private readonly TranslationCompletedListener $listener, - private readonly Translated $translated, + private readonly string $groupName, + private readonly Translated $translated, + private readonly AfterTranslateDto $afterTranslateDto ) { } - /** - * Execute the job. - */ - public function handle(): void + public function handle(Application $application): void { $translated = []; $data = Cache::get($this->groupName, []); @@ -43,11 +41,15 @@ final class AfterTranslate implements ShouldQueue, ShouldBeEncrypted $translated[$key] = implode(' ', $data[$key]); } - if (empty($data)) { + if (empty($data) || !empty($errors)) { throw new AfterTranslateException('Part or all of the text has not been translated. Keys: ' . implode(', ', $errors)); } - Cache::forget($this->groupName); + $objectAfterTranslate = $application->make($this->afterTranslateDto->getClassName()); + if (!$objectAfterTranslate instanceof \KorElf\TranslateLaravel\Contracts\TranslationCompletedListener) { + throw new AfterTranslateException('The class must implement the \KorElf\TranslateLaravel\Contracts\TranslationCompletedListener interface.'); + } + $objectAfterTranslate->onTranslationCompleted($translated, $this->afterTranslateDto->getData()); - $this->listener->onTranslationCompleted($translated); + Cache::forget($this->groupName); } } \ No newline at end of file diff --git a/src/Jobs/RunTranslate.php b/src/Jobs/RunTranslate.php index 935a89c..7f03c2b 100644 --- a/src/Jobs/RunTranslate.php +++ b/src/Jobs/RunTranslate.php @@ -10,7 +10,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Bus; use Illuminate\Support\Str; -use KorElf\TranslateLaravel\Contracts\TranslationCompletedListener; +use KorElf\TranslateLaravel\DTO\AfterTranslateDto; use KorElf\TranslateLaravel\DTO\ProcessTranslateDto; use korElf\TranslateLaravel\DTO\RunTranslateDto; use KorElf\TranslateLaravel\DTO\Translated; @@ -24,8 +24,8 @@ final class RunTranslate implements ShouldQueue, ShouldBeEncrypted * Create a new job instance. */ public function __construct( - private readonly RunTranslateDto $runTranslateDto, - private readonly TranslationCompletedListener $translationCompletedListener, + private readonly RunTranslateDto $runTranslateDto, + private readonly AfterTranslateDto $afterTranslateDto, ) { } /** @@ -55,7 +55,7 @@ final class RunTranslate implements ShouldQueue, ShouldBeEncrypted } $translated->add($key, count($texts)); } - $chains[] = new AfterTranslate($groupName, $this->translationCompletedListener, $translated); + $chains[] = new AfterTranslate($groupName, $translated, $this->afterTranslateDto); Bus::chain($chains)->dispatch(); } diff --git a/src/Translate/TranslateManager.php b/src/Translate/TranslateManager.php index 2a56ef3..5b32add 100644 --- a/src/Translate/TranslateManager.php +++ b/src/Translate/TranslateManager.php @@ -4,10 +4,10 @@ namespace KorElf\TranslateLaravel\Translate; use Illuminate\Contracts\Foundation\Application; use Illuminate\Foundation\Bus\PendingDispatch; -use KorElf\TranslateLaravel\Contracts\CallbackAfterTranslated; -use KorElf\TranslateLaravel\Contracts\Translate; use InvalidArgumentException; use KorElf\TranslateLaravel\Contracts\TranslationCompletedListener; +use KorElf\TranslateLaravel\Contracts\Translate; +use KorElf\TranslateLaravel\DTO\AfterTranslateDto; use KorElf\TranslateLaravel\DTO\RunTranslateDto; use KorElf\TranslateLaravel\Jobs\RunTranslate; @@ -68,9 +68,15 @@ final class TranslateManager /** * Run through queues. */ - public function runJob(RunTranslateDto $params, TranslationCompletedListener $completedListener): PendingDispatch + public function runJob(RunTranslateDto $params, AfterTranslateDto $afterTranslateDto): PendingDispatch { - return RunTranslate::dispatch($params, $completedListener); + $className = $afterTranslateDto->getClassName(); + if (!is_subclass_of($className, TranslationCompletedListener::class)) { + throw new InvalidArgumentException('The class ' . $className . ' must implement the \KorElf\TranslateLaravel\Contracts\Translate\TranslationCompletedListener interface.'); + } + unset($className); + + return RunTranslate::dispatch($params, $afterTranslateDto); } public function getLimit(?string $driver = null): array