| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Coffreo project "coffreo/jms-translation-js-extractor-bundle" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Coffreo SAS <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Coffreo\JMSTranslationJsExtractorBundle\Extractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Coffreo\JsTranslationExtractor\Extractor\JsTranslationExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Coffreo\JsTranslationExtractor\Extractor\JsTranslationExtractorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Coffreo\JsTranslationExtractor\Model\TranslationCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Coffreo\JsTranslationExtractor\Model\TranslationString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use JMS\TranslationBundle\Model\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use JMS\TranslationBundle\Model\MessageCatalogue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use JMS\TranslationBundle\Translation\FileSourceFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | final class JsFileExtractor implements FileVisitorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var FileSourceFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $fileSourceFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var JsTranslationExtractorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     private $extractor; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @var string[] | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     private $extensions; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * JsFileExtractor constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param FileSourceFactory                    $fileSourceFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param string[]                             $extensions | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @param JsTranslationExtractorInterface|null $extractor | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 5 |  |     public function __construct(FileSourceFactory $fileSourceFactory, $extensions = null, JsTranslationExtractorInterface $extractor = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 5 |  |         $this->fileSourceFactory = $fileSourceFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |         $this->extensions = $extensions ?: ['js', 'jsx']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         $this->extractor = $extractor ?: new JsTranslationExtractor(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 | 5 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * Called for non-specially handled files. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * This is not called if handled by a more specific method. | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param \SplFileInfo     $file | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param MessageCatalogue $catalogue | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |     public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |         if (!\in_array($file->getExtension(), $this->extensions, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |         $translations = $this->findTranslations($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |         foreach ($translations as $translation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             /** @var $translation TranslationString */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |             $message = new Message($translation->getMessage(), $translation->getContext('domain') ?: 'messages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |             $message->setDesc($translation->getContext('desc')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |             $message->setDesc($translation->getContext('meaning')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 3 |  |             $message->addSource($this->fileSourceFactory->create($file, $translation->getLine())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |             $catalogue->add($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param \SplFileInfo $file | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @return TranslationCollection | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |     public function findTranslations(\SplFileInfo $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |         return $this->extractor->extract(file_get_contents($file), new TranslationCollection()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * Called when a PHP file is encountered. | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * The visitor already gets a parsed AST passed along. | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @param \SplFileInfo     $file | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @param MessageCatalogue $catalogue | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * @param array            $ast | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |     public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * Called when a Twig file is encountered. | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * The visitor already gets a parsed AST passed along. | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * @param \SplFileInfo     $file | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @param MessageCatalogue $catalogue | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @param \Twig_Node       $ast | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |     public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 | 1 |  |     } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  |