Skip to content

Commit d28877b

Browse files
authored
Merge pull request #6 from andrew72ru/develop
Updates and refactoring
2 parents cd7ea4a + 2a7e31b commit d28877b

File tree

9 files changed

+84
-67
lines changed

9 files changed

+84
-67
lines changed

composer.json

+20-20
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@
1212
],
1313
"require": {
1414
"php": "^7.2",
15-
"doctrine/common": "^2.8",
15+
"doctrine/common": "^2.12",
1616
"doctrine/doctrine-bundle": "^1.8|^2.0",
17-
"doctrine/orm": "^2.6|^3.0",
18-
"doctrine/persistence": "^1.0",
19-
"ramsey/uuid": "^3.8 || ^4",
17+
"doctrine/orm": "^2.7|^3.0",
18+
"doctrine/persistence": "^1.3",
19+
"ramsey/uuid": "^3.8|^4",
2020
"ramsey/uuid-doctrine": "^1.5",
21-
"symfony/config": "^4.1|^5.0",
22-
"symfony/dependency-injection": "^4.1|^5.0",
23-
"symfony/doctrine-bridge": "^4.1|^5.0",
24-
"symfony/finder": "^4.1|^5.0",
25-
"symfony/framework-bundle": "^4.1|^5.0",
26-
"symfony/polyfill-mbstring": "^1",
27-
"symfony/translation": "^4.1|^5.0",
28-
"symfony/twig-bridge": "^4.1|^5.0",
29-
"symfony/twig-bundle": "^4.1|^5.0",
30-
"twig/twig": "^2.4",
31-
"symfony/yaml": "^4.1|^5.0"
21+
"symfony/config": "^4.1|^5",
22+
"symfony/dependency-injection": "^4.1|^5",
23+
"symfony/doctrine-bridge": "^4.1|^5",
24+
"symfony/finder": "^4.1|^5",
25+
"symfony/framework-bundle": "^4.1|^5",
26+
"symfony/polyfill-mbstring": "*",
27+
"symfony/translation": "^4.1|^5",
28+
"symfony/twig-bridge": "^4.1|^5",
29+
"symfony/twig-bundle": "^4.1|^5",
30+
"twig/twig": "^2.4|^3",
31+
"symfony/yaml": "^4.1|^5"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^8.1",
35-
"symfony/console": "^4.1|^5.0",
36-
"symfony/css-selector": "^4.1|^5.0",
37-
"symfony/dom-crawler": "^4.1|^5.0",
38-
"symfony/phpunit-bridge": "^4.1|^5.0",
39-
"symfony/var-dumper": "^4.1|^5.0"
35+
"symfony/console": "^4.1|^5",
36+
"symfony/css-selector": "^4.1|^5",
37+
"symfony/dom-crawler": "^4.1|^5",
38+
"symfony/phpunit-bridge": "^4.1|^5",
39+
"symfony/var-dumper": "^4.1|^5"
4040
},
4141
"extra": {
4242
"branch-alias": {

psalm.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="5"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
</projectFiles>
15+
</psalm>

src/Command/MigrateToDatabaseCommand.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
1818
use Symfony\Component\Console\Style\SymfonyStyle;
19-
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
2020
use Symfony\Component\Translation\Translator;
2121
use Symfony\Contracts\Translation\TranslatorInterface;
2222

@@ -47,7 +47,7 @@ class MigrateToDatabaseCommand extends Command
4747
protected static $defaultName = 'creative:db-i18n:migrate';
4848

4949
/**
50-
* @var ContainerInterface
50+
* @var ParameterBagInterface
5151
*/
5252
private $container;
5353

@@ -74,17 +74,17 @@ class MigrateToDatabaseCommand extends Command
7474
/**
7575
* MigrateToDatabaseCommand constructor.
7676
*
77-
* @param ContainerInterface $container
78-
* @param TranslatorInterface $translator
79-
* @param ManagerRegistry $doctrine
80-
* @param string|null $name
77+
* @param ParameterBagInterface $container
78+
* @param TranslatorInterface $translator
79+
* @param ManagerRegistry $doctrine
80+
* @param string|null $name
8181
*/
82-
public function __construct(ContainerInterface $container, TranslatorInterface $translator, ManagerRegistry $doctrine, string $name = null)
82+
public function __construct(ParameterBagInterface $container, TranslatorInterface $translator, ManagerRegistry $doctrine, string $name = null)
8383
{
8484
parent::__construct($name);
8585
$this->container = $container;
8686
$this->translator = $translator;
87-
$this->entityClass = $this->container->getParameter('db_i18n.entity');
87+
$this->entityClass = $this->container->get('db_i18n.entity');
8888
$this->doctrine = $doctrine;
8989
}
9090

@@ -113,7 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
113113
throw new RuntimeException('Translator service of application has no \'getCatalogue\' method');
114114
}
115115

116-
if (!$this->container->hasParameter('locales') || !is_array($this->container->getParameter('locales'))) {
116+
if (!$this->container->has('locales') || !is_array($this->container->get('locales'))) {
117117
throw new RuntimeException('Application container must have a \'locales\' parameter, and this parameter must be an array');
118118
}
119119

@@ -125,7 +125,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
125125
$catalogue = $this->translator->getCatalogue($locale);
126126

127127
$forExport = $catalogue->all($domain);
128-
$exported = $this->exportToDatabase($forExport, $locale, $this->container->getParameter('db_i18n.domain'));
128+
$exported = $this->exportToDatabase($forExport, $locale, $this->container->get('db_i18n.domain'));
129129

130130
$io->writeln(sprintf(
131131
'Loaded form %s: %u messages, exported to database: %s',
@@ -210,7 +210,7 @@ protected function checkEntityExists(string $locale, string $key): EntityInterfa
210210
*/
211211
protected function getLocale(string $filename): ?string
212212
{
213-
$locales = $this->container->getParameter('locales');
213+
$locales = $this->container->get('locales');
214214
$locale = null;
215215
foreach ($locales as $localeParam) {
216216
if (strpos($filename, $localeParam) !== false) {
@@ -236,7 +236,7 @@ protected function locateFile(string $path): string
236236
if (strpos($path, '/') === 0) {
237237
$realPath = $path;
238238
} else {
239-
$realPath = $this->container->getParameter('kernel.root_dir') . '/../' . $path;
239+
$realPath = $this->container->get('kernel.root_dir') . '/../' . $path;
240240
}
241241

242242
if (!is_file($realPath) || !is_readable($realPath)) {

src/Loader/DbLoader.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
1313
use Doctrine\Persistence\ManagerRegistry;
1414
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
1516
use Symfony\Component\Translation\Exception\InvalidResourceException;
1617
use Symfony\Component\Translation\Exception\NotFoundResourceException;
1718
use Symfony\Component\Translation\Loader\LoaderInterface;
@@ -35,13 +36,13 @@ class DbLoader implements LoaderInterface, DbLoaderInterface
3536

3637
/**
3738
* DbLoader constructor.
38-
* @param ContainerInterface $container
39-
* @param ManagerRegistry $doctrine
39+
* @param ParameterBagInterface $container
40+
* @param ManagerRegistry $doctrine
4041
*/
41-
public function __construct(ContainerInterface $container, ManagerRegistry $doctrine)
42+
public function __construct(ParameterBagInterface $container, ManagerRegistry $doctrine)
4243
{
4344
$this->doctrine = $doctrine;
44-
$this->entityClass = $container->getParameter('db_i18n.entity');
45+
$this->entityClass = $container->get('db_i18n.entity');
4546
}
4647

4748
/**
@@ -56,7 +57,7 @@ public function __construct(ContainerInterface $container, ManagerRegistry $doct
5657
* @throws NotFoundResourceException when the resource cannot be found
5758
* @throws InvalidResourceException when the resource cannot be loaded
5859
*/
59-
public function load($resource, $locale, $domain = 'messages')
60+
public function load($resource, string $locale, string $domain = 'messages'): MessageCatalogue
6061
{
6162
$messages = $this->getRepository()->findByDomainAndLocale($domain, $locale);
6263

src/Repository/TranslationRepository.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Creative\DbI18nBundle\Repository;
44

55
use Creative\DbI18nBundle\Entity\Translation;
6+
use Creative\DbI18nBundle\Interfaces\EntityInterface;
67
use Creative\DbI18nBundle\Interfaces\TranslationRepositoryInterface;
78
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
89
use Doctrine\Common\Collections\ArrayCollection;
10+
use Doctrine\Common\Collections\Collection;
911
use Doctrine\Persistence\ManagerRegistry;
1012

1113
/**
@@ -30,7 +32,8 @@ public function __construct(ManagerRegistry $registry)
3032
* @param string $domain
3133
* @param string $locale
3234
*
33-
* @return array|\Creative\DbI18nBundle\Interfaces\EntityInterface[]|\Doctrine\Common\Collections\Collection|mixed
35+
* @return EntityInterface[]|Collection
36+
* @psalm-return Collection<array-key, EntityInterface[]>
3437
*/
3538
public function findByDomainAndLocale(string $domain, string $locale)
3639
{

src/Resources/config/config.yaml

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ services:
77
resource: '../../../src'
88
exclude: '../../../src/{Entity,Resources}'
99

10+
doctrine.orm.naming_strategy.underscore:
11+
class: Doctrine\ORM\Mapping\UnderscoreNamingStrategy
12+
arguments:
13+
$numberAware: true
14+
1015
translation.loader.db:
1116
class: Creative\DbI18nBundle\Loader\DbLoader
1217
public: '%kernel.debug%'
1318
arguments:
14-
- '@service_container'
19+
- '@parameter_bag'
1520
- '@doctrine'
1621
tags:
1722
- { name: translation.loader, alias: db }
@@ -23,7 +28,7 @@ services:
2328

2429
Creative\DbI18nBundle\Command\MigrateToDatabaseCommand:
2530
arguments:
26-
- '@service_container'
31+
- '@parameter_bag'
2732
- '@translator.default'
2833
- '@doctrine'
2934
tags: [ console.command ]

tests/Kernel.php

-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ public function registerBundles()
2929
];
3030
}
3131

32-
/**
33-
* @inheritDoc
34-
*/
35-
protected function configureRoutes(\Symfony\Component\Routing\RouteCollectionBuilder $routes)
36-
{
37-
}
38-
3932
/**
4033
* @inheritDoc
4134
*/

tests/Loader/DbLoaderTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DbLoaderTest extends KernelTestCase
1919
public function testIsServiceConfigured(): void
2020
{
2121
self::bootKernel();
22-
$this->assertInstanceOf(DbLoader::class, self::$container->get('translation.loader.db'));
22+
self::assertInstanceOf(DbLoader::class, self::$container->get('translation.loader.db'));
2323
}
2424

2525
protected function setUp(): void
@@ -51,7 +51,7 @@ public function testLoadCatalogue(): void
5151
{
5252
$service = self::$container->get('translation.loader.db');
5353
$cat = $service->load(null, 'en', 'db_messages');
54-
$this->assertInstanceOf(MessageCatalogue::class, $cat);
55-
$this->assertSame('This is a translation of key', $cat->get('translatable.key', 'db_messages'));
54+
self::assertInstanceOf(MessageCatalogue::class, $cat);
55+
self::assertSame('This is a translation of key', $cat->get('translatable.key', 'db_messages'));
5656
}
5757
}

tests/TranslationTest.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,53 @@ public function setUp(): void
2525

2626
public function testGetId()
2727
{
28-
$this->assertNull($this->entity->getId());
28+
self::assertNull($this->entity->getId());
2929
}
3030

3131
public function testSetDomain()
3232
{
33-
$this->assertInstanceOf(Translation::class, $this->entity->setDomain('domain'));
33+
self::assertInstanceOf(Translation::class, $this->entity->setDomain('domain'));
3434
}
3535

3636
public function testSetLocale()
3737
{
38-
$this->assertInstanceOf(Translation::class, $this->entity->setLocale('ru'));
38+
self::assertInstanceOf(Translation::class, $this->entity->setLocale('ru'));
3939
}
4040

4141
public function testGetKey()
4242
{
43-
$this->assertNull($this->entity->getKey());
43+
self::assertNull($this->entity->getKey());
4444
}
4545

4646
public function testGetDomain()
4747
{
48-
$this->assertNull($this->entity->getDomain());
48+
self::assertNull($this->entity->getDomain());
4949
$this->entity->setDomain('domain');
50-
$this->assertEquals('domain', $this->entity->getDomain());
50+
self::assertEquals('domain', $this->entity->getDomain());
5151
}
5252

5353
public function testGetTranslation()
5454
{
55-
$this->assertNull($this->entity->getTranslation());
55+
self::assertNull($this->entity->getTranslation());
5656
$this->entity->setTranslation('translation');
57-
$this->assertEquals('translation', $this->entity->getTranslation());
57+
self::assertEquals('translation', $this->entity->getTranslation());
5858
}
5959

6060
public function testGetLocale()
6161
{
62-
$this->assertNull($this->entity->getLocale());
62+
self::assertNull($this->entity->getLocale());
6363
$this->entity->setLocale('en');
64-
$this->assertEquals('en', $this->entity->getLocale());
64+
self::assertEquals('en', $this->entity->getLocale());
6565
}
6666

6767
public function testSetTranslation()
6868
{
69-
$this->assertInstanceOf(Translation::class, $this->entity->setTranslation('translation'));
69+
self::assertInstanceOf(Translation::class, $this->entity->setTranslation('translation'));
7070
}
7171

7272
public function testSetKey()
7373
{
74-
$this->assertInstanceOf(Translation::class, $this->entity->setKey('key'));
74+
self::assertInstanceOf(Translation::class, $this->entity->setKey('key'));
7575
}
7676

7777
public function testLoad()
@@ -83,10 +83,10 @@ public function testLoad()
8383
'domain' => 'domain',
8484
];
8585

86-
$this->assertInstanceOf(Translation::class, $this->entity->load($params));
87-
$this->assertEquals('en', $this->entity->getLocale());
88-
$this->assertEquals('key', $this->entity->getKey());
89-
$this->assertEquals('translation', $this->entity->getTranslation());
90-
$this->assertEquals('domain', $this->entity->getDomain());
86+
self::assertInstanceOf(Translation::class, $this->entity->load($params));
87+
self::assertEquals('en', $this->entity->getLocale());
88+
self::assertEquals('key', $this->entity->getKey());
89+
self::assertEquals('translation', $this->entity->getTranslation());
90+
self::assertEquals('domain', $this->entity->getDomain());
9191
}
9292
}

0 commit comments

Comments
 (0)