-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathColumnTitleAnnotationTranslationExtractor.php
More file actions
83 lines (71 loc) · 2.87 KB
/
ColumnTitleAnnotationTranslationExtractor.php
File metadata and controls
83 lines (71 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace APY\DataGridBundle\Translation;
use APY\DataGridBundle\Grid\Mapping\Driver\Annotation;
use APY\DataGridBundle\Grid\Mapping\Metadata\Manager;
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
use JMS\TranslationBundle\Model\FileSource;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
class ColumnTitleAnnotationTranslationExtractor implements FileVisitorInterface, \PHPParser_NodeVisitor
{
private $annotated;
private $catalogue;
private $parsedClassName;
public function beforeTraverse(array $nodes)
{
$this->annotated = false;
$this->parsedClassName = null;
}
public function enterNode(\PHPParser_Node $node)
{
if ($node instanceof \PHPParser_Node_Stmt_Namespace) {
// Base namespace
$this->parsedClassName = $node->name->toString();
} elseif ($node instanceof \PHPParser_Node_Stmt_UseUse) {
// Don't worry about classes that don't import the grid mapper
if ('APY_DataGridBundle_Grid_Mapping' == $node->name->toString('_')) {
$this->annotated = true;
}
} elseif ($node instanceof \PHPParser_Node_Stmt_Class) {
// Append class name to base namespace
$this->parsedClassName .= '\\' . $node->name;
}
}
public function leaveNode(\PHPParser_Node $node)
{
}
public function afterTraverse(array $nodes)
{
}
public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue)
{
}
public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast)
{
$this->catalogue = $catalogue;
// Traverse document to assemble class name
$traverser = new \PHPParser_NodeTraverser();
$traverser->addVisitor($this);
$traverser->traverse($ast);
if ($this->annotated) {
// Get annotations for the class
$annotationDriver = new Annotation(new DoctrineAnnotationReader());
$manager = new Manager();
$manager->addDriver($annotationDriver, -1);
$metadata = $manager->getMetadata($this->parsedClassName);
// Save messages for title
foreach ($metadata->getFields() as $field) {
$mappedField = $metadata->getFieldMapping($field);
if ((!isset($mappedField['visible']) || $mappedField['visible']) && isset($mappedField['title'])) {
$message = new Message($mappedField['title']);
$message->addSource(new FileSource((string) $file));
$catalogue->add($message);
}
}
}
}
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $node)
{
}
}