|
| 1 | +<?php |
| 2 | +namespace Gettext\Robo; |
| 3 | + |
| 4 | +use Gettext\Translations; |
| 5 | +use FilesystemIterator; |
| 6 | +use MultipleIterator; |
| 7 | +use RecursiveDirectoryIterator; |
| 8 | +use RecursiveIteratorIterator; |
| 9 | +use RecursiveRegexIterator; |
| 10 | +use RegexIterator; |
| 11 | +use Robo\Task\BaseTask; |
| 12 | +use Robo\Contract\TaskInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Trait to scan files to get gettext entries |
| 16 | + */ |
| 17 | +trait GettextScanner |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Init a gettext task |
| 21 | + */ |
| 22 | + protected function taskGettextScanner() |
| 23 | + { |
| 24 | + return new TaskGettextScanner(); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class TaskGettextScanner extends BaseTask implements TaskInterface |
| 29 | +{ |
| 30 | + protected $iterator; |
| 31 | + protected $targets = []; |
| 32 | + |
| 33 | + public function __construct() |
| 34 | + { |
| 35 | + $this->iterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Add a new source folder |
| 40 | + * |
| 41 | + * @param string $path |
| 42 | + * @param null|string $regex |
| 43 | + * |
| 44 | + * @return $this |
| 45 | + */ |
| 46 | + public function extract($path, $regex = null) |
| 47 | + { |
| 48 | + $directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS); |
| 49 | + $iterator = new RecursiveIteratorIterator($directory); |
| 50 | + |
| 51 | + if ($regex) { |
| 52 | + $iterator = new RegexIterator($iterator, $regex, RecursiveRegexIterator::GET_MATCH); |
| 53 | + } |
| 54 | + |
| 55 | + $this->iterator->attachIterator($iterator); |
| 56 | + |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Add a new target |
| 62 | + * |
| 63 | + * @param string $path |
| 64 | + * |
| 65 | + * @return $this |
| 66 | + */ |
| 67 | + public function generate($path) |
| 68 | + { |
| 69 | + $this->targets[] = $path; |
| 70 | + |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Run the task |
| 76 | + */ |
| 77 | + public function run() |
| 78 | + { |
| 79 | + foreach ($this->targets as $target) { |
| 80 | + $translations = new Translations(); |
| 81 | + |
| 82 | + $this->scan($translations); |
| 83 | + |
| 84 | + if (is_file($target)) { |
| 85 | + $fn = $this->getFunctionName('from', $target, 'File'); |
| 86 | + |
| 87 | + $translations->mergeWith(Translations::$fn($target), Translations::MERGE_HEADERS | Translations::MERGE_LANGUAGE | Translations::MERGE_PLURAL | Translations::MERGE_COMMENTS); |
| 88 | + } |
| 89 | + |
| 90 | + $fn = $this->getFunctionName('to', $target, 'File'); |
| 91 | + $dir = dirname($target); |
| 92 | + |
| 93 | + if (!is_dir($dir)) { |
| 94 | + mkdir($dir, 0777, true); |
| 95 | + } |
| 96 | + |
| 97 | + $translations->$fn($target); |
| 98 | + |
| 99 | + $this->printTaskInfo("Gettext exported to {$target}"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Execute the scan |
| 105 | + * |
| 106 | + * @param Translations $translations |
| 107 | + */ |
| 108 | + private function scan(Translations $translations) |
| 109 | + { |
| 110 | + foreach ($this->iterator as $each) { |
| 111 | + foreach ($each as $file) { |
| 112 | + if ($file === null || !$file->isFile()) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + $target = $file->getPathname(); |
| 117 | + |
| 118 | + if (($fn = $this->getFunctionName('addFrom', $target, 'File'))) { |
| 119 | + $translations->$fn($target); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get the format based in the extension |
| 127 | + * |
| 128 | + * @param string $file |
| 129 | + * |
| 130 | + * @return string|null |
| 131 | + */ |
| 132 | + private function getFunctionName($prefix, $file, $suffix) |
| 133 | + { |
| 134 | + switch (pathinfo($file, PATHINFO_EXTENSION)) { |
| 135 | + case 'php': |
| 136 | + return "{$prefix}PhpCode{$suffix}"; |
| 137 | + |
| 138 | + case 'js': |
| 139 | + return "{$prefix}JsCode{$suffix}"; |
| 140 | + |
| 141 | + case 'po': |
| 142 | + return "{$prefix}Po{$suffix}"; |
| 143 | + |
| 144 | + case 'mo': |
| 145 | + return "{$prefix}Mo{$suffix}"; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments