Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add LoggerAwareInterface, display error messages on parsing errors

  • Loading branch information...
commit 42c1313de8245e6fab3afeff9a7bc3b19151ab4c 1 parent 7944633
@rande rande authored
View
33 Logger/LoggerAwareInterface.php
@@ -0,0 +1,33 @@
+<?php
+
+/*
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * 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\Logger;
+
+use Symfony\Component\HttpKernel\Log\LoggerInterface;
+
+/**
+ * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+interface LoggerAwareInterface
+{
+ /**
+ * @abstract
+ * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
+ */
+ public function setLogger(LoggerInterface $logger);
+}
View
17 Translation/Extractor/File/AuthenticationMessagesExtractor.php
@@ -20,19 +20,15 @@
use JMS\TranslationBundle\Exception\RuntimeException;
use JMS\TranslationBundle\Model\FileSource;
-
use JMS\TranslationBundle\Model\Message;
-
use JMS\TranslationBundle\Annotation\Meaning;
-
use JMS\TranslationBundle\Annotation\Desc;
-
use JMS\TranslationBundle\Annotation\Ignore;
-
use JMS\TranslationBundle\Model\MessageCatalogue;
-
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
use Doctrine\Common\Annotations\DocParser;
+use JMS\TranslationBundle\Logger\LoggerAwareInterface;
+use Symfony\Component\HttpKernel\Log\LoggerInterface;
class AuthenticationMessagesExtractor implements FileVisitorInterface, \PHPParser_NodeVisitor
{
@@ -125,7 +121,14 @@ public function enterNode(\PHPParser_Node $node)
return;
}
- throw new RuntimeException(sprintf('Could not extract id from return value, expected scalar string but got %s (in %s on line %d).', get_class($node->expr), $this->file, $node->expr->getLine()));
+ $message = sprintf('Could not extract id from return value, expected scalar string but got %s (in %s on line %d).', get_class($node->expr), $this->file, $node->expr->getLine());
+ if ($this->logger) {
+ $this->logger->err($message);
+
+ return;
+ }
+
+ throw new RuntimeException($message);
}
$message = Message::create($node->expr->value, $this->domain)
View
31 Translation/Extractor/File/DefaultPhpFileExtractor.php
@@ -27,6 +27,8 @@
use JMS\TranslationBundle\Annotation\Ignore;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
use JMS\TranslationBundle\Model\MessageCatalogue;
+use JMS\TranslationBundle\Logger\LoggerAwareInterface;
+use Symfony\Component\HttpKernel\Log\LoggerInterface;
/**
* This parser can extract translation information from PHP files.
@@ -35,12 +37,13 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
-class DefaultPhpFileExtractor implements FileVisitorInterface, \PHPParser_NodeVisitor
+class DefaultPhpFileExtractor implements LoggerAwareInterface, FileVisitorInterface, \PHPParser_NodeVisitor
{
private $traverser;
private $catalogue;
private $file;
private $docParser;
+ private $logger;
public function __construct(DocParser $docParser)
{
@@ -49,6 +52,14 @@ public function __construct(DocParser $docParser)
$this->traverser->addVisitor($this);
}
+ /**
+ * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
+ */
+ public function setLogger(LoggerInterface $logger)
+ {
+ $this->logger = $logger;
+ }
+
public function enterNode(\PHPParser_Node $node)
{
if (!$node instanceof \PHPParser_Node_Expr_MethodCall
@@ -76,7 +87,14 @@ public function enterNode(\PHPParser_Node $node)
return;
}
- throw new RuntimeException(sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()));
+ $message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
+
+ if ($this->logger) {
+ $this->logger->err($message);
+ return;
+ }
+
+ throw new RuntimeException($message);
}
$id = $node->args[0]->value->value;
@@ -88,7 +106,14 @@ public function enterNode(\PHPParser_Node $node)
return;
}
- throw new RuntimeException(sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()));
+ $message = sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
+
+ if ($this->logger) {
+ $this->logger->err($message);
+ return;
+ }
+
+ throw new RuntimeException($message);
}
$domain = $node->args[$index]->value->value;
View
18 Translation/Extractor/File/FormExtractor.php
@@ -27,6 +27,8 @@
use Doctrine\Common\Annotations\DocParser;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
+use JMS\TranslationBundle\Logger\LoggerAwareInterface;
+use Symfony\Component\HttpKernel\Log\LoggerInterface;
class FormExtractor implements FileVisitorInterface, \PHPParser_NodeVisitor
{
@@ -38,6 +40,7 @@ class FormExtractor implements FileVisitorInterface, \PHPParser_NodeVisitor
private $useStatements = array();
private $inMethod = false;
private $localFormBuilderVars;
+ private $logger;
public function __construct(DocParser $docParser)
{
@@ -47,6 +50,7 @@ public function __construct(DocParser $docParser)
$this->traverser->addVisitor($this);
}
+
public function enterNode(\PHPParser_Node $node)
{
if ($node instanceof \PHPParser_Node_Stmt_Namespace) {
@@ -160,7 +164,14 @@ public function enterNode(\PHPParser_Node $node)
continue;
}
- throw new RuntimeException(sprintf('Unable to extract translation id for form label from non-string values, but got "%s" in %s on line %d. Please refactor your code to pass a string, or add "/** @Ignore */".', get_class($item->value), $this->file, $item->value->getLine()));
+ $message = sprintf('Unable to extract translation id for form label from non-string values, but got "%s" in %s on line %d. Please refactor your code to pass a string, or add "/** @Ignore */".', get_class($item->value), $this->file, $item->value->getLine());
+ if ($this->logger) {
+ $this->logger->err($message);
+
+ return;
+ }
+
+ throw new RuntimeException($message);
}
$message = new Message($item->value->value);
@@ -214,4 +225,9 @@ private function getFqcn(array $parts)
return $this->namespace.'\\'.implode('\\', $parts);
}
+
+ public function setLogger(LoggerInterface $logger)
+ {
+ $this->logger = $logger;
+ }
}
View
11 Translation/Extractor/FileExtractor.php
@@ -20,6 +20,7 @@
use JMS\TranslationBundle\Exception\InvalidArgumentException;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
+use JMS\TranslationBundle\Logger\LoggerAwareInterface;
use JMS\TranslationBundle\Twig\RemovingNodeVisitor;
@@ -32,7 +33,7 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
-class FileExtractor implements ExtractorInterface
+class FileExtractor implements ExtractorInterface, LoggerAwareInterface
{
private $twig;
private $visitors;
@@ -62,6 +63,14 @@ public function __construct(\Twig_Environment $twig, LoggerInterface $logger, ar
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
+
+ foreach ($this->visitors as $visitor) {
+ if (!$visitor instanceof LoggerAwareInterface) {
+ continue;
+ }
+
+ $visitor->setLogger($logger);
+ }
}
public function setDirectory($directory)
View
40 Translation/ExtractorManager.php
@@ -23,6 +23,7 @@
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileExtractor;
+use JMS\TranslationBundle\Logger\LoggerAwareInterface;
class ExtractorManager implements ExtractorInterface
{
@@ -32,6 +33,11 @@ class ExtractorManager implements ExtractorInterface
private $enabledExtractors = array();
private $logger;
+ /**
+ * @param Extractor\FileExtractor $extractor
+ * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
+ * @param array $customExtractors
+ */
public function __construct(FileExtractor $extractor, LoggerInterface $logger, array $customExtractors = array())
{
$this->fileExtractor = $extractor;
@@ -39,12 +45,26 @@ public function __construct(FileExtractor $extractor, LoggerInterface $logger, a
$this->logger = $logger;
}
+ /**
+ * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
+ */
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
$this->fileExtractor->setLogger($logger);
+
+ foreach ($this->customExtractors as $extractor) {
+ if (!$extractor instanceof LoggerAwareInterface) {
+ continue;
+ }
+
+ $extractor->setLogger($logger);
+ }
}
+ /**
+ * @param array $directories
+ */
public function setDirectories(array $directories)
{
foreach ($directories as $dir) {
@@ -52,6 +72,10 @@ public function setDirectories(array $directories)
}
}
+ /**
+ * @param $directory
+ * @throws \JMS\TranslationBundle\Exception\InvalidArgumentException
+ */
public function addDirectory($directory)
{
if (!is_dir($directory)) {
@@ -61,16 +85,26 @@ public function addDirectory($directory)
$this->directories[] = $directory;
}
+ /**
+ * @param array $dirs
+ */
public function setExcludedDirs(array $dirs)
{
$this->fileExtractor->setExcludedDirs($dirs);
}
+ /**
+ * @param array $names
+ */
public function setExcludedNames(array $names)
{
$this->fileExtractor->setExcludedNames($names);
}
+ /**
+ * @param array $aliases
+ * @throws \JMS\TranslationBundle\Exception\InvalidArgumentException
+ */
public function setEnabledExtractors(array $aliases)
{
foreach ($aliases as $alias => $true) {
@@ -82,11 +116,15 @@ public function setEnabledExtractors(array $aliases)
$this->enabledExtractors = $aliases;
}
+ /**
+ * @return \JMS\TranslationBundle\Model\MessageCatalogue
+ */
public function extract()
{
$catalogue = new MessageCatalogue();
foreach ($this->directories as $directory) {
+ $this->logger->info(sprintf('Extract messages from directory : %s', $directory));
$this->fileExtractor->setDirectory($directory);
$catalogue->merge($this->fileExtractor->extract());
}
@@ -97,6 +135,8 @@ public function extract()
continue;
}
+ $this->logger->info(sprintf('Extract messages with custom extractor : %s', $alias));
+
$catalogue->merge($extractor->extract());
}
View
2  Translation/Updater.php
@@ -185,6 +185,7 @@ private function setConfig(Config $config)
{
$this->config = $config;
+ $this->logger->info(sprintf("Loading scatalogues from `%s`", $config->getTranslationsDir()));
$this->existingCatalogue = $this->loader->loadFromDirectory(
$config->getTranslationsDir(), $config->getLocale()
);
@@ -194,6 +195,7 @@ private function setConfig(Config $config)
$this->extractor->setExcludedNames($config->getExcludedNames());
$this->extractor->setEnabledExtractors($config->getEnabledExtractors());
+ $this->logger->info("Extracting translation keys");
$this->scannedCatalogue = $this->extractor->extract();
$this->scannedCatalogue->setLocale($config->getLocale());
Please sign in to comment.
Something went wrong with that request. Please try again.