| Total Complexity | 62 |
| Total Lines | 521 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Complex classes like GettextCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GettextCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class GettextCommand extends Command |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | public const CODE_CHANGES = 2; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The Po results |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $poResult = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The template paths |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $templatePaths = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The locale path |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $localePath = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The name of default domain if not specified. Used for pot and po file names. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $defaultDomain = 'default'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The locales to generate. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $locales = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @inheritDoc |
||
| 78 | */ |
||
| 79 | public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser |
||
| 80 | { |
||
| 81 | return parent::buildOptionParser($parser) |
||
| 82 | ->setDescription([ |
||
| 83 | 'Create or update i18n po/pot files', |
||
| 84 | '', |
||
| 85 | '`bin/cake gettext`: update files for current app', |
||
| 86 | '`bin/cake gettext -app <app path>`: update files for the app', |
||
| 87 | '`bin/cake gettext -plugin <plugin name>`: update files for the plugin', |
||
| 88 | ]) |
||
| 89 | ->addOption('app', [ |
||
| 90 | 'help' => 'The app path, for i18n update.', |
||
| 91 | 'short' => 'a', |
||
| 92 | 'required' => false, |
||
| 93 | ]) |
||
| 94 | ->addOption('plugin', [ |
||
| 95 | 'help' => 'The plugin name, for i18n update.', |
||
| 96 | 'short' => 'p', |
||
| 97 | 'required' => false, |
||
| 98 | ]) |
||
| 99 | ->addOption('ci', [ |
||
| 100 | 'help' => 'Run in CI mode. Exit with error if PO files are changed.', |
||
| 101 | 'required' => false, |
||
| 102 | 'boolean' => true, |
||
| 103 | ]) |
||
| 104 | ->addOption('locales', [ |
||
| 105 | 'help' => 'Comma separated list of locales to generate. Leave empty to use configuration `I18n.locales`', |
||
| 106 | 'short' => 'l', |
||
| 107 | 'default' => implode(',', array_keys((array)Configure::read('I18n.locales'))), |
||
| 108 | ]); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get po result. |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getPoResult(): array |
||
| 117 | { |
||
| 118 | return $this->poResult; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get templatePaths. |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function getTemplatePaths(): array |
||
| 127 | { |
||
| 128 | return $this->templatePaths; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get localePath |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getLocalePath(): string |
||
| 137 | { |
||
| 138 | return $this->localePath; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Update gettext po files. |
||
| 143 | * |
||
| 144 | * @param \Cake\Console\Arguments $args The command arguments. |
||
| 145 | * @param \Cake\Console\ConsoleIo $io The console io |
||
| 146 | * @return null|void|int The exit code or null for success |
||
| 147 | */ |
||
| 148 | public function execute(Arguments $args, ConsoleIo $io) |
||
| 149 | { |
||
| 150 | $resCmd = []; |
||
| 151 | exec('which msgmerge 2>&1', $resCmd); |
||
| 152 | if (empty($resCmd[0])) { |
||
| 153 | $io->abort('ERROR: msgmerge not available. Please install gettext utilities.'); |
||
| 154 | } |
||
| 155 | |||
| 156 | $io->out('Updating .pot and .po files...'); |
||
| 157 | |||
| 158 | $this->locales = array_filter(explode(',', $args->getOption('locales'))); |
||
|
|
|||
| 159 | $this->setupPaths($args); |
||
| 160 | foreach ($this->templatePaths as $path) { |
||
| 161 | $io->out(sprintf('Search in: %s', $path)); |
||
| 162 | $this->parseDir($path); |
||
| 163 | } |
||
| 164 | |||
| 165 | $io->out('Creating master .pot file'); |
||
| 166 | $hasChanges = $this->writeMasterPot($io); |
||
| 167 | $this->ttagExtract($args, $io); |
||
| 168 | |||
| 169 | $io->hr(); |
||
| 170 | $io->out('Merging master .pot with current .po files'); |
||
| 171 | $io->hr(); |
||
| 172 | |||
| 173 | $this->writePoFiles($io); |
||
| 174 | |||
| 175 | $io->out('Done'); |
||
| 176 | |||
| 177 | if ($args->getOption('ci') && $hasChanges) { |
||
| 178 | return GettextCommand::CODE_CHANGES; |
||
| 179 | } |
||
| 180 | |||
| 181 | return GettextCommand::CODE_SUCCESS; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Setup template paths and locale path |
||
| 186 | * |
||
| 187 | * @param \Cake\Console\Arguments $args The command arguments. |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | private function setupPaths(Arguments $args): void |
||
| 191 | { |
||
| 192 | $plugin = $args->getOption('plugin'); |
||
| 193 | $localesPaths = (array)App::path('locales'); |
||
| 194 | if ($plugin && is_string($plugin)) { |
||
| 195 | $paths = [ |
||
| 196 | Plugin::classPath($plugin), |
||
| 197 | Plugin::configPath($plugin), |
||
| 198 | ]; |
||
| 199 | $this->templatePaths = array_merge($paths, App::path(View::NAME_TEMPLATE, $plugin)); |
||
| 200 | $this->defaultDomain = $plugin; |
||
| 201 | $localePaths = App::path('locales', $plugin); |
||
| 202 | $this->localePath = (string)Hash::get($localePaths, '0'); |
||
| 203 | |||
| 204 | return; |
||
| 205 | } |
||
| 206 | $app = $args->getOption('app'); |
||
| 207 | $basePath = $app ?? getcwd(); |
||
| 208 | $this->templatePaths = [$basePath . DS . 'src', $basePath . DS . 'config']; |
||
| 209 | $this->templatePaths = array_merge($this->templatePaths, App::path(View::NAME_TEMPLATE)); |
||
| 210 | $this->templatePaths = array_filter($this->templatePaths, function ($path) { |
||
| 211 | return strpos($path, 'plugins') === false; |
||
| 212 | }); |
||
| 213 | $this->localePath = (string)Hash::get($localesPaths, 0); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Write `master.pot` file |
||
| 218 | * |
||
| 219 | * @param \Cake\Console\ConsoleIo $io The console io |
||
| 220 | * @return bool True if file was updated, false otherwise |
||
| 221 | */ |
||
| 222 | private function writeMasterPot(ConsoleIo $io): bool |
||
| 223 | { |
||
| 224 | $updated = false; |
||
| 225 | |||
| 226 | foreach ($this->poResult as $domain => $poResult) { |
||
| 227 | $potFilename = sprintf('%s/%s.pot', $this->localePath, $domain); |
||
| 228 | $io->out(sprintf('Writing new .pot file: %s', $potFilename)); |
||
| 229 | $pot = new File($potFilename, true); |
||
| 230 | |||
| 231 | $contents = $pot->read(); |
||
| 232 | |||
| 233 | // remove headers from pot file |
||
| 234 | $contents = preg_replace('/^msgid ""\nmsgstr ""/', '', $contents); |
||
| 235 | $contents = trim(preg_replace('/^"([^"]*?)"$/m', '', $contents)); |
||
| 236 | |||
| 237 | $lines = []; |
||
| 238 | ksort($poResult); |
||
| 239 | foreach ($poResult as $res => $contexts) { |
||
| 240 | sort($contexts); |
||
| 241 | foreach ($contexts as $ctx) { |
||
| 242 | if (!empty($ctx)) { |
||
| 243 | $lines[] = sprintf('msgctxt "%s"%smsgid "%s"%smsgstr ""', $ctx, "\n", $res, "\n"); |
||
| 244 | } else { |
||
| 245 | $lines[] = sprintf('msgid "%s"%smsgstr ""', $res, "\n"); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | $result = implode("\n\n", $lines); |
||
| 251 | if ($contents !== $result) { |
||
| 252 | $pot->write(sprintf("%s\n%s\n", $this->header('pot'), $result)); |
||
| 253 | $updated = true; |
||
| 254 | } |
||
| 255 | |||
| 256 | $pot->close(); |
||
| 257 | } |
||
| 258 | |||
| 259 | return $updated; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Write `.po` files |
||
| 264 | * |
||
| 265 | * @param \Cake\Console\ConsoleIo $io The console io |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | private function writePoFiles(ConsoleIo $io): void |
||
| 269 | { |
||
| 270 | if (empty($this->locales)) { |
||
| 271 | $io->info('No locales set, .po files generation skipped'); |
||
| 272 | |||
| 273 | return; |
||
| 274 | } |
||
| 275 | |||
| 276 | $header = $this->header('po'); |
||
| 277 | foreach ($this->locales as $loc) { |
||
| 278 | $potDir = $this->localePath . DS . $loc; |
||
| 279 | if (!file_exists($potDir)) { |
||
| 280 | mkdir($potDir); |
||
| 281 | } |
||
| 282 | $io->out(sprintf('Language: %s', $loc)); |
||
| 283 | |||
| 284 | foreach (array_keys($this->poResult) as $domain) { |
||
| 285 | $potFilename = sprintf('%s/%s.pot', $this->localePath, $domain); |
||
| 286 | $poFile = sprintf('%s/%s.po', $potDir, $domain); |
||
| 287 | if (!file_exists($poFile)) { |
||
| 288 | $newPoFile = new File($poFile, true); |
||
| 289 | $newPoFile->write($header); |
||
| 290 | $newPoFile->close(); |
||
| 291 | } |
||
| 292 | $io->out(sprintf('Merging %s', $poFile)); |
||
| 293 | $mergeCmd = sprintf('msgmerge --backup=off -N -U %s %s', $poFile, $potFilename); |
||
| 294 | exec($mergeCmd); |
||
| 295 | $this->analyzePoFile($poFile, $io); |
||
| 296 | $io->hr(); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Header lines for po/pot file |
||
| 303 | * |
||
| 304 | * @param string $type The file type (can be 'po', 'pot') |
||
| 305 | * @return string |
||
| 306 | * @codeCoverageIgnore |
||
| 307 | */ |
||
| 308 | private function header(string $type = 'po'): string |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Analyze po file and translate it |
||
| 343 | * |
||
| 344 | * @param string $filename The po file name |
||
| 345 | * @param \Cake\Console\ConsoleIo $io The console io |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | private function analyzePoFile($filename, ConsoleIo $io): void |
||
| 349 | { |
||
| 350 | $lines = file($filename); |
||
| 351 | $numItems = $numNotTranslated = 0; |
||
| 352 | foreach ($lines as $k => $l) { |
||
| 353 | if (strpos($l, 'msgid "') === 0) { |
||
| 354 | $numItems++; |
||
| 355 | } |
||
| 356 | if (strpos($l, 'msgstr ""') === 0) { |
||
| 357 | if (!isset($lines[$k + 1])) { |
||
| 358 | $numNotTranslated++; |
||
| 359 | } elseif (strpos($lines[$k + 1], '"') !== 0) { |
||
| 360 | $numNotTranslated++; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | } |
||
| 364 | $translated = $numItems - $numNotTranslated; |
||
| 365 | $percent = 0; |
||
| 366 | if ($numItems > 0) { |
||
| 367 | $percent = number_format($translated * 100. / $numItems, 1); |
||
| 368 | } |
||
| 369 | $io->out(sprintf('Translated %d of %d items - %s %%', $translated, $numItems, $percent)); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Remove leading and trailing quotes from string |
||
| 374 | * |
||
| 375 | * @param string $str The string |
||
| 376 | * @return string The new string |
||
| 377 | */ |
||
| 378 | private function unquoteString($str): string |
||
| 379 | { |
||
| 380 | return substr($str, 1, -1); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * "fix" string - strip slashes, escape and convert new lines to \n |
||
| 385 | * |
||
| 386 | * @param string $str The string |
||
| 387 | * @return string The new string |
||
| 388 | */ |
||
| 389 | private function fixString($str): string |
||
| 390 | { |
||
| 391 | $str = stripslashes($str); |
||
| 392 | $str = str_replace('"', '\"', $str); |
||
| 393 | $str = str_replace("\n", '\n', $str); |
||
| 394 | $str = str_replace('|||||', "'", $str); // special sequence used in parseContent to temporarily replace "\'" |
||
| 395 | |||
| 396 | return $str; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Parse file and rips gettext strings |
||
| 401 | * |
||
| 402 | * @param string $file The file name |
||
| 403 | * @param string $extension The file extension |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | private function parseFile($file, $extension) |
||
| 407 | { |
||
| 408 | if (!in_array($extension, ['php', 'twig'])) { |
||
| 409 | return; |
||
| 410 | } |
||
| 411 | $content = file_get_contents($file); |
||
| 412 | if (empty($content)) { |
||
| 413 | return; |
||
| 414 | } |
||
| 415 | |||
| 416 | $functions = [ |
||
| 417 | '__' => 0, // __( string $singular , ... $args ) |
||
| 418 | '__n' => 0, // __n( string $singular , string $plural , integer $count , ... $args ) |
||
| 419 | '__d' => 1, // __d( string $domain , string $msg , ... $args ) |
||
| 420 | '__dn' => 1, // __dn( string $domain , string $singular , string $plural , integer $count , ... $args ) |
||
| 421 | '__x' => 1, // __x( string $context , string $singular , ... $args ) |
||
| 422 | '__xn' => 1, // __xn( string $context , string $singular , string $plural , integer $count , ... $args ) |
||
| 423 | '__dx' => 2, // __dx( string $domain , string $context , string $msg , ... $args ) |
||
| 424 | '__dxn' => 2, // __dxn( string $domain , string $context , string $singular , string $plural , integer $count , ... $args ) |
||
| 425 | ]; |
||
| 426 | |||
| 427 | // temporarily replace "\'" with "|||||", fixString will replace "|||||" with "\'" |
||
| 428 | // this fixes wrongly matched data in the following regexp |
||
| 429 | $content = str_replace("\'", '|||||', $content); |
||
| 430 | |||
| 431 | $options = [ |
||
| 432 | 'open_parenthesis' => preg_quote('('), |
||
| 433 | 'quote' => preg_quote("'"), |
||
| 434 | 'double_quote' => preg_quote('"'), |
||
| 435 | ]; |
||
| 436 | |||
| 437 | foreach ($functions as $fname => $singularPosition) { |
||
| 438 | $capturePath = "'[^']*'"; |
||
| 439 | $doubleQuoteCapture = str_replace("'", $options['double_quote'], $capturePath); |
||
| 440 | $quoteCapture = str_replace("'", $options['quote'], $capturePath); |
||
| 441 | |||
| 442 | // phpcs:disable |
||
| 443 | $rgxp = '/' . $fname . '\s*' . $options['open_parenthesis'] . str_repeat('((?:' . $doubleQuoteCapture . ')|(?:' . $quoteCapture . '))\s*[,)]\s*', $singularPosition + 1) . '/'; |
||
| 444 | // phpcs:enable |
||
| 445 | |||
| 446 | $matches = []; |
||
| 447 | preg_match_all($rgxp, $content, $matches); |
||
| 448 | |||
| 449 | $limit = count($matches[0]); |
||
| 450 | for ($i = 0; $i < $limit; $i++) { |
||
| 451 | $domain = $this->defaultDomain; |
||
| 452 | $ctx = ''; |
||
| 453 | $str = $this->unquoteString($matches[1][$i]); |
||
| 454 | |||
| 455 | if (strpos($fname, '__d') === 0) { |
||
| 456 | $domain = $this->unquoteString($matches[1][$i]); |
||
| 457 | |||
| 458 | if (strpos($fname, '__dx') === 0) { |
||
| 459 | $ctx = $this->unquoteString($matches[2][$i]); |
||
| 460 | $str = $this->unquoteString($matches[3][$i]); |
||
| 461 | } else { |
||
| 462 | $str = $this->unquoteString($matches[2][$i]); |
||
| 463 | } |
||
| 464 | } elseif (strpos($fname, '__x') === 0) { |
||
| 465 | $ctx = $this->unquoteString($matches[1][$i]); |
||
| 466 | $str = $this->unquoteString($matches[2][$i]); |
||
| 467 | } |
||
| 468 | |||
| 469 | $str = $this->fixString($str); |
||
| 470 | if (empty($str)) { |
||
| 471 | continue; |
||
| 472 | } |
||
| 473 | |||
| 474 | if (!array_key_exists($domain, $this->poResult)) { |
||
| 475 | $this->poResult[$domain] = []; |
||
| 476 | } |
||
| 477 | |||
| 478 | if (!array_key_exists($str, $this->poResult[$domain])) { |
||
| 479 | $this->poResult[$domain][$str] = ['']; |
||
| 480 | } |
||
| 481 | |||
| 482 | if (!in_array($ctx, $this->poResult[$domain][$str])) { |
||
| 483 | $this->poResult[$domain][$str][] = $ctx; |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Parse a directory |
||
| 491 | * |
||
| 492 | * @param string $dir The directory |
||
| 493 | * @return void |
||
| 494 | */ |
||
| 495 | private function parseDir($dir): void |
||
| 496 | { |
||
| 497 | $folder = new Folder($dir); |
||
| 498 | $tree = $folder->tree($dir, false); |
||
| 499 | foreach ($tree as $files) { |
||
| 500 | foreach ($files as $file) { |
||
| 501 | if (!is_dir($file)) { |
||
| 502 | $f = new File($file); |
||
| 503 | $info = $f->info(); |
||
| 504 | if (isset($info['extension'])) { |
||
| 505 | $this->parseFile($file, $info['extension']); |
||
| 506 | } |
||
| 507 | } |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Extract translations from javascript files using ttag, if available. |
||
| 514 | * |
||
| 515 | * @param \Cake\Console\Arguments $args The command arguments. |
||
| 516 | * @param \Cake\Console\ConsoleIo $io The console io |
||
| 517 | * @return void |
||
| 518 | * @codeCoverageIgnore |
||
| 519 | */ |
||
| 520 | private function ttagExtract(Arguments $args, ConsoleIo $io): void |
||
| 555 | } |
||
| 556 | } |
||
| 557 |