Skip to content

Commit

Permalink
minor #82 Minor translation extractor config enhancements (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 1.x-dev branch (closes #82).

Discussion
----------

Minor translation extractor config enhancements

Just some minor improvements after #81.

@danut007ro: If you have some time to review those changes, I'll appreciate!

Commits
-------

6539ae1 Add Sf 5.1 in allowed failures
a3abaaf Minor translation extractor config enhancements
  • Loading branch information
ogizanagi committed Apr 17, 2020
2 parents 6cfee92 + 6539ae1 commit f4f9c91
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 63 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ matrix:
- SYMFONY_VERSION="5.0.*"
- CHECK_CODE_STYLE="yes"

# bleeding edge (unreleased dev versions where failures are allowed):
- php: 7.4
env: SYMFONY_VERSION="5.1.*"

allow_failures:
- env: SYMFONY_VERSION="5.1.*"

cache:
directories:
- $HOME/.composer/cache
Expand Down
12 changes: 3 additions & 9 deletions src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,9 @@ public function getConfigTreeBuilder()
->fixXmlConfig('path')
->children()
->arrayNode('paths')
->example(['%kernel.project_dir%/src/Enum' => 'App\Enum'])
->useAttributeAsKey('path')
->arrayPrototype()
->beforeNormalization()
->ifString()->then(static function (string $v): array { return ['namespace' => $v]; })
->end()
->children()
->scalarNode('namespace')->isRequired()->cannotBeEmpty()->end()
->end()
->example(['App\Enum' => '%kernel.project_dir%/src/Enum'])
->useAttributeAsKey('namespace')
->scalarPrototype()
->end()
->end()
->scalarNode('domain')
Expand Down
54 changes: 26 additions & 28 deletions src/Bridge/Symfony/Bundle/Resources/config/schema/elao_enum.xsd
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
<?xml version="1.0" encoding="UTF-8" ?>

<xsd:schema xmlns="http://elao.com/schema/dic/elao_enum"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://elao.com/schema/dic/elao_enum"
elementFormDefault="qualified">
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://elao.com/schema/dic/elao_enum"
elementFormDefault="qualified">

<xsd:element name="config" type="config" />
<xsd:element name="config" type="config"/>

<xsd:complexType name="config">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="serializer" type="serializer" minOccurs="0" maxOccurs="1" />
<xsd:element name="argument_value_resolver" type="argument_value_resolver" minOccurs="0" maxOccurs="1" />
<xsd:element name="doctrine" type="doctrine" minOccurs="0" maxOccurs="1" />
<xsd:element name="translation_extractor" type="translation_extractor" minOccurs="0" maxOccurs="1" />
<xsd:element name="serializer" type="serializer" minOccurs="0" maxOccurs="1"/>
<xsd:element name="argument_value_resolver" type="argument_value_resolver" minOccurs="0" maxOccurs="1"/>
<xsd:element name="doctrine" type="doctrine" minOccurs="0" maxOccurs="1"/>
<xsd:element name="translation_extractor" type="translation_extractor" minOccurs="0" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>

<xsd:complexType name="serializer">
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="enabled" type="xsd:boolean"/>
</xsd:complexType>

<xsd:complexType name="argument_value_resolver">
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="enabled" type="xsd:boolean"/>
</xsd:complexType>

<xsd:complexType name="doctrine">
<xsd:sequence>
<xsd:element name="type" type="doctrine_type" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="type" type="doctrine_type" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="doctrine_type">
<xsd:attribute name="class" type="xsd:string" use="required" />
<xsd:attribute name="type" type="doctrine_type_type" default="string" />
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="class" type="xsd:string" use="required"/>
<xsd:attribute name="type" type="doctrine_type_type" default="string"/>
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

<xsd:simpleType name="doctrine_type_type">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="string" />
<xsd:enumeration value="int" />
<xsd:enumeration value="string"/>
<xsd:enumeration value="int"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="translation_extractor">
<xsd:sequence>
<xsd:element name="path" type="translation_extractor_path" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="ignore" type="translation_extractor_ignore" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="path" type="translation_extractor_path" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element name="ignore" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="domain" type="xsd:string" />
<xsd:attribute name="filename_pattern" type="xsd:string" />
<xsd:attribute name="enabled" type="xsd:boolean"/>
<xsd:attribute name="domain" type="xsd:string"/>
<xsd:attribute name="filename_pattern" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="translation_extractor_path">
<xsd:attribute name="path" type="xsd:string" use="required" />
<xsd:attribute name="namespace" type="xsd:string" use="required" />
<xsd:complexType name="translation_extractor_path" mixed="true">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="path" type="xsd:string"/>
</xsd:choice>
<xsd:attribute name="namespace" type="xsd:string" use="required"/>
</xsd:complexType>

<xsd:simpleType name="translation_extractor_ignore">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>
29 changes: 14 additions & 15 deletions src/Bridge/Symfony/Translation/Extractor/EnumExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EnumExtractor implements ExtractorInterface
/**
* @var array<string, string>
*/
private $paths;
private $namespacesToDirs;

/**
* @var string
Expand Down Expand Up @@ -49,26 +49,26 @@ class EnumExtractor implements ExtractorInterface
private $hasRun = false;

/**
* @param array<string, string> $paths
* @param array<string, string> $namespacesToDirs
* @param array<string> $ignore
*/
public function __construct(array $paths, string $domain, string $fileNamePattern, array $ignore)
public function __construct(array $namespacesToDirs, string $domain, string $fileNamePattern, array $ignore)
{
$this->paths = $paths;
$this->namespacesToDirs = $namespacesToDirs;
$this->domain = $domain;
$this->fileNamePattern = $fileNamePattern;
$this->ignore = $ignore;
}

public function setPrefix($prefix): void
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}

/**
* @param array|string $resource
*/
public function extract($resource, MessageCatalogue $catalog): void
public function extract($resource, MessageCatalogue $catalog)
{
// Ensure it runs only once.
if ($this->hasRun) {
Expand All @@ -78,9 +78,9 @@ public function extract($resource, MessageCatalogue $catalog): void

$finder = new Finder();

foreach ($this->paths as $dir => $settings) {
foreach ($this->namespacesToDirs as $namespace => $dir) {
// Normalize namespace.
$namespace = rtrim($settings['namespace'], '\\') . '\\';
$namespace = rtrim($namespace, '\\') . '\\';

/** @var SplFileInfo $file */
foreach ($finder->files()->name($this->fileNamePattern)->in($dir) as $file) {
Expand All @@ -106,17 +106,16 @@ public function extract($resource, MessageCatalogue $catalog): void
}

$readables = $class::readables();
foreach ($readables as $k => $enum) {
$enum = (string) $enum;
if ('' === $enum) {
foreach ($readables as $enumValue => $translationKey) {
if ('' === $translationKey) {
continue;
}

$catalog->set($enum, $this->prefix . $enum, $this->domain);
$metadata = $catalog->getMetadata($enum, $this->domain) ?? [];
$catalog->set($translationKey, $this->prefix . $translationKey, $this->domain);
$metadata = $catalog->getMetadata($translationKey, $this->domain) ?? [];
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $file->getPathName());
$metadata['sources'][] = $normalizedFilename . ':' . $k;
$catalog->setMetadata($enum, $metadata, $this->domain);
$metadata['sources'][] = $normalizedFilename . ':' . $enumValue;
$catalog->setMetadata($translationKey, $metadata, $this->domain);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

<elao-enum:config>
<elao-enum:translation_extractor enabled="true" domain="messages_test" filename_pattern="*Enum.php">
<elao-enum:path path="/var/www/elao/src/Enum1" namespace="App\Enum1" />
<elao-enum:path path="/var/www/elao/src/Enum2" namespace="App\Enum2" />
<elao-enum:path namespace="App\Enum1">/var/www/elao/src/Enum1</elao-enum:path>
<elao-enum:path namespace="App\Enum2">/var/www/elao/src/Enum2</elao-enum:path>
<elao-enum:ignore>/var/www/elao/src/Enum/Ignore1</elao-enum:ignore>
<elao-enum:ignore>/var/www/elao/src/Enum/Ignore2</elao-enum:ignore>
</elao-enum:translation_extractor>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
elao_enum:
translation_extractor:
paths:
/var/www/elao/src/Enum1: App\Enum1
/var/www/elao/src/Enum2: { namespace: App\Enum2 }
App\Enum1: /var/www/elao/src/Enum1
App\Enum2: /var/www/elao/src/Enum2
filename_pattern: '*Enum.php'
domain: messages_test
ignore:
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/Integration/Symfony/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ framework:
router:
resource: '%kernel.project_dir%/config/routing.yml'
strict_requirements: '%kernel.debug%'
utf8: true
test: ~
session:
storage_id: 'session.storage.mock_file'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testTranslationExtractorConfig()
$config = $processor->processConfiguration(new Configuration(), [[
'translation_extractor' => [
'enabled' => true,
'paths' => ['%kernel.project_dir%/src/Enum' => 'App\Enum'],
'paths' => ['App\Enum' => '%kernel.project_dir%/src/Enum'],
'domain' => 'messages_test',
'filename_pattern' => '*Enum.php',
'ignore' => ['%kernel.project_dir%/src/Enum/Ignored'],
Expand All @@ -101,7 +101,7 @@ public function testTranslationExtractorConfig()

$this->assertEquals([
'enabled' => true,
'paths' => ['%kernel.project_dir%/src/Enum' => ['namespace' => 'App\Enum']],
'paths' => ['App\Enum' => '%kernel.project_dir%/src/Enum'],
'domain' => 'messages_test',
'filename_pattern' => '*Enum.php',
'ignore' => ['%kernel.project_dir%/src/Enum/Ignored'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public function testTranslationExtractor()
self::assertTrue($container->hasDefinition(EnumExtractor::class), 'translation extractor is loaded');
self::assertEquals([
[
'/var/www/elao/src/Enum1' => ['namespace' => 'App\Enum1'],
'/var/www/elao/src/Enum2' => ['namespace' => 'App\Enum2'],
'App\Enum1' => '/var/www/elao/src/Enum1',
'App\Enum2' => '/var/www/elao/src/Enum2',
],
'messages_test',
'*Enum.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

class EnumExtractorTest extends TestCase
{
private $dirToNamespaceMap = [__DIR__ . '/Enum' => ['namespace' => 'Elao\Enum\Tests\Unit\Bridge\Symfony\Translation\Extractor\Enum']];
private $namespaceToDirMap = ['Elao\Enum\Tests\Unit\Bridge\Symfony\Translation\Extractor\Enum' => __DIR__ . '/Enum'];

/** @var EnumExtractor */
private $extractor;

protected function setUp(): void
{
$this->extractor = new EnumExtractor(
$this->dirToNamespaceMap,
$this->namespaceToDirMap,
'messages_test',
'*Enum.php',
[__DIR__ . '/Enum/Ignore/*']
Expand All @@ -49,7 +49,7 @@ public function testInvalidDirectoryThrowsException()
{
$this->expectException(DirectoryNotFoundException::class);

$extractor = new EnumExtractor(['foo' => ['namespace' => 'bar']], 'domain', '*.php', []);
$extractor = new EnumExtractor(['bar' => 'foo'], 'domain', '*.php', []);
$extractor->extract('baz', new MessageCatalogue('en'));
}

Expand Down

0 comments on commit f4f9c91

Please sign in to comment.