Skip to content

Commit a09b4a4

Browse files
bocharsky-bwNyholm
authored andcommitted
Create legacy class instead of method to improve performance (#23)
1 parent bd6f711 commit a09b4a4

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

src/FileStorage.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Translation\MessageCatalogueInterface;
1717
use Symfony\Component\Translation\Reader\TranslationReader;
1818
use Symfony\Component\Translation\Writer\TranslationWriter;
19+
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
1920
use Translation\Common\Model\Message;
2021
use Translation\Common\Storage;
2122
use Translation\Common\TransferableStorage;
@@ -28,7 +29,7 @@
2829
final class FileStorage implements Storage, TransferableStorage
2930
{
3031
/**
31-
* @var TranslationWriter
32+
* @var TranslationWriterInterface|LegacyTranslationWriter
3233
*/
3334
private $writer;
3435

@@ -60,6 +61,10 @@ final class FileStorage implements Storage, TransferableStorage
6061
*/
6162
public function __construct(TranslationWriter $writer, $loader, array $dir, array $options = [])
6263
{
64+
// Create a legacy writer which is a wrapper for TranslationWriter
65+
if (!$writer instanceof TranslationWriterInterface) {
66+
$writer = new LegacyTranslationWriter($writer);
67+
}
6368
// Create a legacy loader which is a wrapper for TranslationReader
6469
if ($loader instanceof TranslationReader) {
6570
$loader = new LegacyTranslationLoader($loader);
@@ -164,7 +169,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
164169
$path = (string) $resource;
165170
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
166171
$options['path'] = str_replace($matches[0], '', $path);
167-
$this->writeTranslations($catalogue, $matches[1], $options);
172+
$this->writer->write($catalogue, $matches[1], $options);
168173
$written = true;
169174
}
170175
}
@@ -176,7 +181,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
176181

177182
$options['path'] = reset($this->dir);
178183
$format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
179-
$this->writeTranslations($catalogue, $format, $options);
184+
$this->writer->write($catalogue, $format, $options);
180185
}
181186

182187
/**
@@ -210,23 +215,4 @@ private function loadCatalogue($locale, array $dirs)
210215

211216
$this->catalogues[$locale] = $currentCatalogue;
212217
}
213-
214-
/**
215-
* This method calls the new TranslationWriter::write() if exist,
216-
* otherwise fallback to TranslationWriter::writeTranslations() call
217-
* to avoid BC breaks.
218-
*
219-
* @param MessageCatalogue $catalogue
220-
* @param string $format
221-
* @param array $options
222-
*/
223-
private function writeTranslations(MessageCatalogue $catalogue, $format, array $options)
224-
{
225-
if (method_exists($this->writer, 'write')) {
226-
$this->writer->write($catalogue, $format, $options);
227-
} else {
228-
// This method is deprecated since 3.4, maintained to avoid BC breaks
229-
$this->writer->writeTranslations($catalogue, $format, $options);
230-
}
231-
}
232218
}

src/LegacyTranslationLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/**
1818
* This loader is just a legacy wrapper for Symfony TranslationReader
19-
* and provider a BC layer for Symfony 4.
19+
* and provide a BC layer for Symfony 4.
2020
*
2121
* @author Victor Bocharsky <bocharsky.bw@gmail.com>
2222
*/

src/LegacyTranslationWriter.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\SymfonyStorage;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
use Symfony\Component\Translation\Writer\TranslationWriter;
16+
17+
/**
18+
* This writer is just a legacy wrapper for Symfony TranslationWriter
19+
* and provide a BC layer for Symfony 4.
20+
*
21+
* @author Victor Bocharsky <bocharsky.bw@gmail.com>
22+
*/
23+
class LegacyTranslationWriter
24+
{
25+
/**
26+
* @var TranslationWriter
27+
*/
28+
private $writer;
29+
30+
public function __construct(TranslationWriter $writer)
31+
{
32+
$this->writer = $writer;
33+
}
34+
35+
public function write(MessageCatalogue $catalogue, $format, $options = [])
36+
{
37+
$this->writer->writeTranslations($catalogue, $format, $options);
38+
}
39+
}

0 commit comments

Comments
 (0)