Skip to content

Commit a4ac5bc

Browse files
committed
Used dependency injection
1 parent 247715a commit a4ac5bc

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

drupal_translation_extractor.services.yml

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,49 @@ services:
22
_defaults:
33
autowire: true
44

5-
Drupal\drupal_translation_extractor\Translation\Dumper\PoFileDumper:
5+
Drupal\drupal_translation_extractor\Translation\Extractor\Visitor\TransMethodVisitor:
6+
7+
Drupal\drupal_translation_extractor\Translation\Extractor\Visitor\TranslatableMarkupVisitor:
8+
9+
Drupal\drupal_translation_extractor\Translation\Extractor\PhpExtractor:
10+
arguments:
11+
$visitors:
12+
- '@Drupal\drupal_translation_extractor\Translation\Extractor\Visitor\TransMethodVisitor'
13+
- '@Drupal\drupal_translation_extractor\Translation\Extractor\Visitor\TranslatableMarkupVisitor'
14+
15+
Drupal\drupal_translation_extractor\Translation\TwigExtractor:
616

717
Drupal\drupal_translation_extractor\ItkTranslationExtractorTwigExtension:
818
tags:
919
- { name: twig.extension }
20+
21+
drupal_translation_extractor.extractor:
22+
class: Symfony\Component\Translation\Extractor\ChainExtractor
23+
calls:
24+
- [
25+
"addExtractor",
26+
[
27+
"php",
28+
'@Drupal\drupal_translation_extractor\Translation\Extractor\PhpExtractor',
29+
],
30+
]
31+
- [
32+
"addExtractor",
33+
[
34+
"twig",
35+
'@Drupal\drupal_translation_extractor\Translation\TwigExtractor',
36+
],
37+
]
38+
39+
Drupal\drupal_translation_extractor\Translation\Dumper\PoFileDumper:
40+
41+
drupal_translation_extractor.translation_writer:
42+
class: Symfony\Component\Translation\Writer\TranslationWriter
43+
calls:
44+
- [
45+
"addDumper",
46+
[
47+
"po",
48+
'@Drupal\drupal_translation_extractor\Translation\Dumper\PoFileDumper',
49+
],
50+
]

drush.services.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ services:
22
drupal_translation_extractor.translation_extract:
33
class: Drupal\drupal_translation_extractor\Command\TranslationExtractCommand
44
arguments:
5-
- "@twig"
65
- "@extension.path.resolver"
76
- "@locale.storage"
7+
- "@drupal_translation_extractor.extractor"
8+
- "@drupal_translation_extractor.translation_writer"
89
- "@Drupal\\drupal_translation_extractor\\Translation\\Dumper\\PoFileDumper"
910
tags:
1011
- { name: console.command }

src/Command/TranslationExtractCommand.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
namespace Drupal\drupal_translation_extractor\Command;
66

77
use Drupal\Core\Extension\ExtensionPathResolver;
8-
use Drupal\drupal_translation_extractor\Translation\Dumper\PoFileDumper;
98
use Drupal\drupal_translation_extractor\Translation\Dumper\PoItem;
10-
use Drupal\drupal_translation_extractor\Translation\TwigExtractor;
119
use Drupal\locale\StringStorageInterface;
1210
use Symfony\Component\Console\Attribute\AsCommand;
1311
use Symfony\Component\Console\Command\Command;
@@ -18,10 +16,10 @@
1816
use Symfony\Component\Console\Style\SymfonyStyle;
1917
use Symfony\Component\Translation\Catalogue\MergeOperation;
2018
use Symfony\Component\Translation\Catalogue\TargetOperation;
19+
use Symfony\Component\Translation\Extractor\ExtractorInterface;
2120
use Symfony\Component\Translation\MessageCatalogue;
2221
use Symfony\Component\Translation\MessageCatalogueInterface;
23-
use Symfony\Component\Translation\Writer\TranslationWriter;
24-
use Twig\Environment;
22+
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
2523

2624
/**
2725
* Lifted from Symfony's `translation:extract` command.
@@ -43,27 +41,12 @@ final class TranslationExtractCommand extends Command
4341
];
4442
private const NO_FILL_PREFIX = "\0NoFill\0";
4543

46-
/**
47-
* The writer.
48-
*/
49-
private TranslationWriter $writer;
50-
51-
/**
52-
* The extractor.
53-
*/
54-
private TwigExtractor $extractor;
55-
5644
public function __construct(
57-
private readonly Environment $twig,
5845
private readonly ExtensionPathResolver $extensionPathResolver,
5946
private readonly StringStorageInterface $stringStorage,
60-
PoFileDumper $poFileDumper,
47+
private readonly ExtractorInterface $extractor,
48+
private readonly TranslationWriterInterface $writer,
6149
) {
62-
$this->extractor = new TwigExtractor($this->twig);
63-
64-
$this->writer = new TranslationWriter();
65-
$this->writer->addDumper('po', $poFileDumper);
66-
6750
parent::__construct();
6851
}
6952

src/Translation/TwigExtractor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\drupal_translation_extractor\ItkTranslationExtractorTwigExtension;
66
use Drupal\drupal_translation_extractor\Translation\Dumper\PoItem;
77
use Symfony\Bridge\Twig\Translation\TwigExtractor as BaseTwigExtractor;
8+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
89
use Symfony\Component\Translation\MessageCatalogue;
910
use Twig\Environment;
1011
use Twig\Source;
@@ -17,6 +18,7 @@ class TwigExtractor extends BaseTwigExtractor
1718
private string $prefix = '';
1819

1920
public function __construct(
21+
#[Autowire(service: 'twig')]
2022
private Environment $twig,
2123
) {
2224
parent::__construct($this->twig);

0 commit comments

Comments
 (0)