Skip to content

Commit 606ad61

Browse files
authored
Adding a symfony storage (#26)
* Adding a symfony storage * Adding flysystem adapter * remove depes * Adding flysystem * Style
0 parents  commit 606ad61

File tree

9 files changed

+304
-0
lines changed

9 files changed

+304
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/build/
2+
/composer.lock
3+
/phpspec.yml
4+
/phpunit.xml
5+
/vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) PHP Translation team <tobias.nyholm@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Symfony Storage
2+
3+
[![Latest Version](https://img.shields.io/github/release/php-translation/symfony-storage.svg?style=flat-square)](https://github.com/php-translation/symfony-storage/releases)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/php-translation/symfony-storage.svg?style=flat-square)](https://packagist.org/packages/php-translation/symfony-storage)
5+
6+
This is an PHP-translation storage adapter that uses Symfony's translation writer
7+
and loader.
8+
9+
### Install
10+
11+
```bash
12+
composer require php-translation/symfony-storage
13+
```
14+
15+
### Documentation
16+
17+
Read our documentation at [http://php-translation.readthedocs.io](http://php-translation.readthedocs.io/en/latest/).
18+
19+
### Contribute
20+
21+
Do you want to make a change? This repository is READ ONLY. Submit your
22+
pull request to [php-translation/platform-adapter](https://github.com/php-translation/platform-adapter).

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "php-translation/symfony-storage",
3+
"description": "Adapter for Symfony storage.",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Tobias Nyholm",
8+
"email": "tobias.nyholm@gmail.com"
9+
}
10+
],
11+
"require": {
12+
"php": "^5.5 || ^7.0",
13+
"php-translation/common": "^0.1",
14+
"symfony/translation": "^2.7 || ^3.0"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^4.5 || ^5.4"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Translation\\SymfonyStorage\\": "src/"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"Translation\\SymfonyStorage\\Tests\\": "tests/"
27+
}
28+
},
29+
"scripts": {
30+
"test": "vendor/bin/phpunit",
31+
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
32+
}
33+
}

phpunit.xml.dist

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="false"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
codecoverage="true"
13+
bootstrap="./vendor/autoload.php"
14+
>
15+
16+
<formatter type="clover" usefile="false"/>
17+
<testsuites>
18+
<testsuite name="Tests">
19+
<directory>./tests</directory>
20+
</testsuite>
21+
</testsuites>
22+
23+
<filter>
24+
<whitelist>
25+
<directory suffix=".php">./</directory>
26+
<exclude>
27+
<directory>vendor</directory>
28+
<directory>Tests</directory>
29+
</exclude>
30+
</whitelist>
31+
</filter>
32+
</phpunit>

src/FileStorage.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader;
15+
use Symfony\Component\Translation\MessageCatalogue;
16+
use Symfony\Component\Translation\Writer\TranslationWriter;
17+
use Translation\Common\Model\Message;
18+
use Translation\Common\Storage;
19+
20+
/**
21+
* This storage uses Symfony's writer and loader.
22+
*
23+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
24+
*/
25+
final class FileStorage implements Storage
26+
{
27+
/**
28+
* @var TranslationWriter
29+
*/
30+
private $writer;
31+
32+
/**
33+
* @var TranslationLoader
34+
*/
35+
private $loader;
36+
37+
/**
38+
* @var string directory path
39+
*/
40+
private $dir;
41+
42+
/**
43+
* @var MessageCatalogue[] Fetched catalogies
44+
*/
45+
private $catalogues;
46+
47+
/**
48+
* @param TranslationWriter $writer
49+
* @param mixed $loader
50+
* @param array $dir
51+
*/
52+
public function __construct(TranslationWriter $writer, $loader, array $dir)
53+
{
54+
if (!$loader instanceof SymfonyTranslationLoader && !$loader instanceof TranslationLoader) {
55+
throw new \LogicException('Second parameter of FileStorage must be a Symfony translation loader or implement Translation\SymfonyStorage\TranslationLoader');
56+
}
57+
58+
$this->writer = $writer;
59+
$this->loader = $loader;
60+
$this->dir = $dir;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function get($locale, $domain, $key)
67+
{
68+
$catalogue = $this->getCatalogue($locale);
69+
70+
$translation = $catalogue->get($key, $domain);
71+
72+
return new Message($key, $domain, $locale, $translation);
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function create(Message $m)
79+
{
80+
$catalogue = $this->getCatalogue($m->getLocale());
81+
if (!$catalogue->defines($m->getKey(), $m->getDomain())) {
82+
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
83+
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
84+
}
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function update(Message $m)
91+
{
92+
$catalogue = $this->getCatalogue($m->getLocale());
93+
$catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
94+
$this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function delete($locale, $domain, $key)
101+
{
102+
$catalogue = $this->getCatalogue($locale);
103+
$messages = $catalogue->all($domain);
104+
unset($messages[$key]);
105+
106+
$catalogue->replace($messages, $domain);
107+
$this->writeCatalogue($catalogue, $locale, $domain);
108+
}
109+
110+
/**
111+
* Save catalogue back to file.
112+
*
113+
* @param MessageCatalogue $catalogue
114+
* @param string $domain
115+
*/
116+
private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
117+
{
118+
$resources = $catalogue->getResources();
119+
foreach ($resources as $resource) {
120+
$path = $resource->getResource();
121+
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
122+
$this->writer->writeTranslations($catalogue, $matches[1], ['path' => str_replace($matches[0], '', $path)]);
123+
}
124+
}
125+
}
126+
127+
/**
128+
* @param string $locale
129+
*
130+
* @return MessageCatalogue
131+
*/
132+
private function getCatalogue($locale)
133+
{
134+
if (empty($this->catalogues[$locale])) {
135+
$this->loadCatalogue($locale, $this->dir);
136+
}
137+
138+
return $this->catalogues[$locale];
139+
}
140+
141+
/**
142+
* Load catalogue from files.
143+
*
144+
* @param string $locale
145+
* @param array $dirs
146+
*/
147+
private function loadCatalogue($locale, array $dirs)
148+
{
149+
$currentCatalogue = new MessageCatalogue($locale);
150+
foreach ($dirs as $path) {
151+
if (is_dir($path)) {
152+
$this->loader->loadMessages($path, $currentCatalogue);
153+
}
154+
}
155+
156+
$this->catalogues[$locale] = $currentCatalogue;
157+
}
158+
}

src/TranslationLoader.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
16+
/**
17+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
18+
*/
19+
interface TranslationLoader
20+
{
21+
/**
22+
* Loads translation messages from a directory to the catalogue.
23+
*
24+
* @param string $directory the directory to look into
25+
* @param MessageCatalogue $catalogue the catalogue
26+
*/
27+
public function loadMessages($directory, MessageCatalogue $catalogue);
28+
}

tests/.gitkeep

Whitespace-only changes.

travis.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PHP-versions to test:
2+
5.5
3+
5.6
4+
7.0
5+
7.1

0 commit comments

Comments
 (0)