Skip to content

Create legacy class instead of method to improve performance #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions src/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
use Translation\Common\Model\Message;
use Translation\Common\Storage;
use Translation\Common\TransferableStorage;
Expand All @@ -28,7 +29,7 @@
final class FileStorage implements Storage, TransferableStorage
{
/**
* @var TranslationWriter
* @var TranslationWriterInterface|LegacyTranslationWriter
*/
private $writer;

Expand Down Expand Up @@ -60,6 +61,10 @@ final class FileStorage implements Storage, TransferableStorage
*/
public function __construct(TranslationWriter $writer, $loader, array $dir, array $options = [])
{
// Create a legacy writer which is a wrapper for TranslationWriter
if (!$writer instanceof TranslationWriterInterface) {
$writer = new LegacyTranslationWriter($writer);
}
// Create a legacy loader which is a wrapper for TranslationReader
if ($loader instanceof TranslationReader) {
$loader = new LegacyTranslationLoader($loader);
Expand Down Expand Up @@ -164,7 +169,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
$path = (string) $resource;
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
$options['path'] = str_replace($matches[0], '', $path);
$this->writeTranslations($catalogue, $matches[1], $options);
$this->writer->write($catalogue, $matches[1], $options);
$written = true;
}
}
Expand All @@ -176,7 +181,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)

$options['path'] = reset($this->dir);
$format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
$this->writeTranslations($catalogue, $format, $options);
$this->writer->write($catalogue, $format, $options);
}

/**
Expand Down Expand Up @@ -210,23 +215,4 @@ private function loadCatalogue($locale, array $dirs)

$this->catalogues[$locale] = $currentCatalogue;
}

/**
* This method calls the new TranslationWriter::write() if exist,
* otherwise fallback to TranslationWriter::writeTranslations() call
* to avoid BC breaks.
*
* @param MessageCatalogue $catalogue
* @param string $format
* @param array $options
*/
private function writeTranslations(MessageCatalogue $catalogue, $format, array $options)
{
if (method_exists($this->writer, 'write')) {
$this->writer->write($catalogue, $format, $options);
} else {
// This method is deprecated since 3.4, maintained to avoid BC breaks
$this->writer->writeTranslations($catalogue, $format, $options);
}
}
}
2 changes: 1 addition & 1 deletion src/LegacyTranslationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/**
* This loader is just a legacy wrapper for Symfony TranslationReader
* and provider a BC layer for Symfony 4.
* and provide a BC layer for Symfony 4.
*
* @author Victor Bocharsky <bocharsky.bw@gmail.com>
*/
Expand Down
39 changes: 39 additions & 0 deletions src/LegacyTranslationWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\SymfonyStorage;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;

/**
* This writer is just a legacy wrapper for Symfony TranslationWriter
* and provide a BC layer for Symfony 4.
*
* @author Victor Bocharsky <bocharsky.bw@gmail.com>
*/
class LegacyTranslationWriter
{
/**
* @var TranslationWriter
*/
private $writer;

public function __construct(TranslationWriter $writer)
{
$this->writer = $writer;
}

public function write(MessageCatalogue $catalogue, $format, $options = [])
{
$this->writer->writeTranslations($catalogue, $format, $options);
}
}