1<?php 2 3/** 4 * 2007-2016 PrestaShop. 5 * 6 * NOTICE OF LICENSE 7 * 8 * This source file is subject to the Open Software License (OSL 3.0) 9 * that is bundled with this package in the file LICENSE.txt. 10 * It is also available through the world-wide-web at this URL: 11 * http://opensource.org/licenses/osl-3.0.php 12 * If you did not receive a copy of the license and are unable to 13 * obtain it through the world-wide-web, please send an email 14 * to license@prestashop.com so we can send you a copy immediately. 15 * 16 * DISCLAIMER 17 * 18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 19 * versions in the future. If you wish to customize PrestaShop for your 20 * needs please refer to http://www.prestashop.com for more information. 21 * 22 * @author PrestaShop SA <contact@prestashop.com> 23 * @copyright 2007-2015 PrestaShop SA 24 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 25 * International Registered Trademark & Property of PrestaShop SA 26 */ 27 28namespace PrestaShop\TranslationToolsBundle\Translation\Extractor; 29 30use Symfony\Component\Finder\Finder; 31use Symfony\Component\Translation\Extractor\AbstractFileExtractor; 32use Symfony\Component\Translation\Extractor\ExtractorInterface; 33use Symfony\Component\Translation\MessageCatalogue; 34use PrestaShop\TranslationToolsBundle\Translation\Manager\OriginalStringManager; 35use PrestaShop\TranslationToolsBundle\Translation\Parser\CrowdinPhpParser; 36 37class CrowdinPhpExtractor extends AbstractFileExtractor implements ExtractorInterface 38{ 39 /** 40 * Prefix for new found message. 41 * 42 * @var string 43 */ 44 private $prefix = ''; 45 46 /** @var CrowdinPhpParser $crodwinPhpParser */ 47 private $crodwinPhpParser; 48 49 /** @var OriginalStringManager $originalStringManager */ 50 private $originalStringManager; 51 52 public function __construct(CrowdinPhpParser $crodwinPhpParser, OriginalStringManager $originalStringManager) 53 { 54 $this->crodwinPhpParser = $crodwinPhpParser; 55 $this->originalStringManager = $originalStringManager; 56 } 57 58 /** 59 * {@inheritdoc} 60 */ 61 public function extract($resource, MessageCatalogue $catalog) 62 { 63 $files = $this->extractFiles($resource); 64 foreach ($files as $file) { 65 $generator = $this->crodwinPhpParser->parseFileTokens($file); 66 for (; $generator->valid(); $generator->next()) { 67 $translation = $generator->current(); 68 $originalTranslation = $this->originalStringManager->get($file, $translation['key']); 69 70 $catalog->set($originalTranslation, $translation['message']); 71 $catalog->setMetadata( 72 $originalTranslation, 73 [ 74 'key' => $translation['key'], 75 'file' => basename($file), 76 ] 77 ); 78 } 79 80 if (PHP_VERSION_ID >= 70000) { 81 // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 82 gc_mem_caches(); 83 } 84 } 85 } 86 87 /** 88 * {@inheritdoc} 89 */ 90 public function setPrefix($prefix) 91 { 92 $this->prefix = $prefix; 93 } 94 95 /** 96 * @param string $file 97 * 98 * @throws \InvalidArgumentException 99 * 100 * @return bool 101 */ 102 protected function canBeExtracted($file) 103 { 104 return $this->isFile($file) && 'php' === pathinfo($file, PATHINFO_EXTENSION); 105 } 106 107 /** 108 * @param string|array $directory 109 * 110 * @return array 111 */ 112 protected function extractFromDirectory($directory) 113 { 114 $finder = new Finder(); 115 116 return $finder->files()->name('*.php')->in($directory); 117 } 118} 119