Skip to content

Domain annotation form extractor #219

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 50 additions & 0 deletions Annotation/Domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TranslationBundle\Annotation;

use JMS\TranslationBundle\Exception\RuntimeException;

/**
* @Annotation
*
* @author Pol-Valentin Cami <[email protected]>
*/
final class Domain
{
/** @var string @Required */
public $text;

public function __construct()
{
if (0 === func_num_args()) {
return;
}
$values = func_get_arg(0);

if (isset($values['value'])) {
$values['text'] = $values['value'];
}

if (!isset($values['text'])) {
throw new RuntimeException(sprintf('The "text" attribute for annotation "@Domain" must be set.'));
}

$this->text = $values['text'];
}
}
58 changes: 58 additions & 0 deletions Annotation/Domains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TranslationBundle\Annotation;

use JMS\TranslationBundle\Exception\RuntimeException;

/**
* @Annotation
*
* @author Pol-Valentin Cami <[email protected]>
*/
final class Domains
{
/** @var array @Required */
public $texts;

public function __construct($values)
{
if (0 === func_num_args()) {
return;
}
$values = func_get_arg(0);

foreach ($values['value'] as $var) {
if( ! is_scalar($var)) {
throw new \InvalidArgumentException(sprintf(
'@Domains supports only scalar values "%s" given.',
is_object($var) ? get_class($var) : gettype($var)
));
}
}
if (isset($values['value'])) {
$values['text'] = $values['value'];
}

if (!isset($values['text'])) {
throw new RuntimeException(sprintf('The "text" attribute for annotation "@Domain" must be set.'));
}

$this->texts = $values['text'];
}
}
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
<argument key="desc">JMS\TranslationBundle\Annotation\Desc</argument>
<argument key="meaning">JMS\TranslationBundle\Annotation\Meaning</argument>
<argument key="ignore">JMS\TranslationBundle\Annotation\Ignore</argument>
<argument key="domain">JMS\TranslationBundle\Annotation\Domain</argument>
<argument key="domains">JMS\TranslationBundle\Annotation\Domains</argument>
</argument>
</call>
<call method="setIgnoreNotImportedAnnotations">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TranslationBundle\Tests\Translation\Extractor\File\Fixture;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\AbstractType;
use JMS\TranslationBundle\Annotation\Domain;
use JMS\TranslationBundle\Annotation\Domains;

class MyFormTypeWithDomainAnnotation extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('field_with_domain_label', 'text', array(
'label' => /** @Domain("messages_domain") */
'form.label.field_with_domain_label'
))
->add('field_with_domains_label', 'text', array(
'label' => /** @Domains({"messages_domains_one", "messages_domains_two"}) */
'form.label.field_with_domains_label'
));
}
}
24 changes: 24 additions & 0 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace JMS\TranslationBundle\Tests\Translation\Extractor\File;

use JMS\TranslationBundle\Annotation\Domain;
use JMS\TranslationBundle\Exception\RuntimeException;
use Doctrine\Common\Annotations\DocParser;
use JMS\TranslationBundle\Translation\Extractor\File\FormExtractor;
Expand Down Expand Up @@ -274,6 +275,27 @@ public function testExtractWithWithSubscriberAndListener()
$this->assertEquals($expected, $catalogue);
}

public function testExtractWithDomainAnnotation()
{
$expected = new MessageCatalogue();
$path = __DIR__.'/Fixture/MyFormTypeWithDomainAnnotation.php';

$message = new Message('form.label.field_with_domain_label', 'messages_domain');
$message->addSource(new FileSource($path, 33));
$expected->add($message);

$message = new Message('form.label.field_with_domains_label', 'messages_domains_one');
$message->addSource(new FileSource($path, 37));
$expected->add($message);

$message = new Message('form.label.field_with_domains_label', 'messages_domains_two');
$message->addSource(new FileSource($path, 37));
$expected->add($message);

$catalogue = $this->extract('MyFormTypeWithDomainAnnotation.php');
$this->assertEquals($expected, $catalogue);
}

/**
* Run extractor tests with and without a default domain as a form option
* with the same extractor instance to see that the default domain isn't
Expand All @@ -292,6 +314,8 @@ protected function setUp()
'desc' => 'JMS\TranslationBundle\Annotation\Desc',
'meaning' => 'JMS\TranslationBundle\Annotation\Meaning',
'ignore' => 'JMS\TranslationBundle\Annotation\Ignore',
'domain' => 'JMS\TranslationBundle\Annotation\Domain',
'domains' => 'JMS\TranslationBundle\Annotation\Domains',
));
$docParser->setIgnoreNotImportedAnnotations(true);

Expand Down
13 changes: 12 additions & 1 deletion Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use JMS\TranslationBundle\Annotation\Meaning;
use JMS\TranslationBundle\Annotation\Desc;
use JMS\TranslationBundle\Annotation\Ignore;
use JMS\TranslationBundle\Annotation\Domain;
use JMS\TranslationBundle\Annotation\Domains;
use Doctrine\Common\Annotations\DocParser;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
Expand Down Expand Up @@ -357,7 +359,12 @@ private function parseItem($item, $domain = null)
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
} else if ($annot instanceof Domain) {
$domain = $annot->text;
} else if ($annot instanceof Domains) {
$domains = $annot->texts;
}

}
}

Expand All @@ -382,13 +389,17 @@ private function parseItem($item, $domain = null)
$source = new FileSource((string) $this->file, $item->value->getLine());
$id = $item->value->value;

if (null === $domain) {
if (null === $domain && empty($domains)) {
$this->defaultDomainMessages[] = array(
'id' => $id,
'source' => $source,
'desc' => $desc,
'meaning' => $meaning
);
} elseif(!empty($domains)) {
foreach($domains as $domain){
$this->addToCatalogue($id, $source, $domain, $desc, $meaning);
}
} else {
$this->addToCatalogue($id, $source, $domain, $desc, $meaning);
}
Expand Down