|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +// @see https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary |
| 5 | +require $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php'; |
| 6 | + |
| 7 | +use Drupal\Component\Gettext\PoStreamReader; |
| 8 | +use Drupal\Core\Extension\ExtensionPathResolver; |
| 9 | +use Drupal\Core\Template\TwigTransTokenParser; |
| 10 | +use Drupal\drupal_translation_extractor\Command\TranslationExtractCommand; |
| 11 | +use Drupal\drupal_translation_extractor\Translation\Dumper\PoFileDumper; |
| 12 | +use Drupal\drupal_translation_extractor\Translation\Extractor\PhpExtractor; |
| 13 | +use Drupal\drupal_translation_extractor\Translation\Extractor\Visitor\TranslatableMarkupVisitor; |
| 14 | +use Drupal\drupal_translation_extractor\Translation\Extractor\Visitor\TransMethodVisitor; |
| 15 | +use Drupal\drupal_translation_extractor\Twig\Extension\ItkTranslationExtractorTwigExtension; |
| 16 | +use Drupal\drupal_translation_extractor\Twig\Translation\Extractor\TwigExtractor; |
| 17 | +use Drupal\locale\StringStorageInterface; |
| 18 | +use Symfony\Component\Console\Application; |
| 19 | +use Symfony\Component\Console\Command\Command; |
| 20 | +use Symfony\Component\Console\Exception\RuntimeException; |
| 21 | +use Symfony\Component\Console\Input\ArgvInput; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | +use Symfony\Component\Translation\Extractor\ChainExtractor; |
| 25 | +use Symfony\Component\Translation\Writer\TranslationWriter; |
| 26 | +use Twig\Environment; |
| 27 | +use Twig\Loader\FilesystemLoader; |
| 28 | + |
| 29 | +$application = createApplication(); |
| 30 | +$command = createCommand(); |
| 31 | + |
| 32 | +$application->addCommand($command); |
| 33 | + |
| 34 | +$application->setDefaultCommand($command->getName(), true); |
| 35 | +$application->run(); |
| 36 | + |
| 37 | +/** |
| 38 | + * Create the application with some bespoke exception handling. |
| 39 | + */ |
| 40 | +function createApplication(): Application |
| 41 | +{ |
| 42 | + return new class extends Application { |
| 43 | + public function __construct() |
| 44 | + { |
| 45 | + parent::__construct('drupal_translation_extractor', '1.0.0'); |
| 46 | + } |
| 47 | + |
| 48 | + protected function doRunCommand( |
| 49 | + Command $command, |
| 50 | + InputInterface $input, |
| 51 | + OutputInterface $output, |
| 52 | + ): int { |
| 53 | + $tempInput = new ArgvInput(); |
| 54 | + try { |
| 55 | + $tempInput->bind($command->getDefinition()); |
| 56 | + } catch (RuntimeException) { |
| 57 | + } |
| 58 | + |
| 59 | + if (false !== $tempInput->getOption('fill-from-string-storage')) { |
| 60 | + parent::renderThrowable( |
| 61 | + new RuntimeException('Option --fill-from-string-storage cannot be used when running outside Drupal context.'), |
| 62 | + $output |
| 63 | + ); |
| 64 | + |
| 65 | + return Command::FAILURE; |
| 66 | + } |
| 67 | + |
| 68 | + return parent::doRunCommand($command, $input, $output); |
| 69 | + } |
| 70 | + |
| 71 | + public function renderThrowable(Throwable $e, OutputInterface $output): void |
| 72 | + { |
| 73 | + if ($e instanceof ExtensionPathResolverException) { |
| 74 | + parent::renderThrowable( |
| 75 | + new RuntimeException('Cannot resolve module and theme paths when running outside Drupal context.'), |
| 76 | + $output |
| 77 | + ); |
| 78 | + } else { |
| 79 | + parent::renderThrowable($e, $output); |
| 80 | + } |
| 81 | + } |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Create the command with injected dependencies. |
| 87 | + */ |
| 88 | +function createCommand(): TranslationExtractCommand |
| 89 | +{ |
| 90 | + $twig = new Environment(new FilesystemLoader()); |
| 91 | + $trans = fn () => null; |
| 92 | + $twig->addFilter(new Twig\TwigFilter('t', $trans)); |
| 93 | + $twig->addFilter(new Twig\TwigFilter('trans', $trans)); |
| 94 | + $twig->addExtension(new ItkTranslationExtractorTwigExtension()); |
| 95 | + $twig->addTokenParser(new TwigTransTokenParser()); |
| 96 | + |
| 97 | + $writer = new TranslationWriter(); |
| 98 | + $writer->addDumper('po', new PoFileDumper()); |
| 99 | + |
| 100 | + $reader = new PoStreamReader(); |
| 101 | + |
| 102 | + $extractor = new ChainExtractor(); |
| 103 | + $extractor->addExtractor('php', |
| 104 | + new PhpExtractor(visitors: [ |
| 105 | + new TransMethodVisitor(), |
| 106 | + new TranslatableMarkupVisitor(), |
| 107 | + ])); |
| 108 | + $extractor->addExtractor('twig', new TwigExtractor($twig)); |
| 109 | + |
| 110 | + $extensionPathResolver = new class extends ExtensionPathResolver { |
| 111 | + public function __construct() |
| 112 | + { |
| 113 | + // No call to parent::__construct() here. |
| 114 | + } |
| 115 | + |
| 116 | + public function getPathname(string $type, string $name): ?string |
| 117 | + { |
| 118 | + throw new ExtensionPathResolverException(__FUNCTION__); |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + $stringStorage = new class implements StringStorageInterface { |
| 123 | + public function getStrings(array $conditions = [], array $options = []) |
| 124 | + { |
| 125 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 126 | + } |
| 127 | + |
| 128 | + public function getTranslations(array $conditions = [], array $options = []) |
| 129 | + { |
| 130 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 131 | + } |
| 132 | + |
| 133 | + public function getLocations(array $conditions = []) |
| 134 | + { |
| 135 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 136 | + } |
| 137 | + |
| 138 | + public function findString(array $conditions) |
| 139 | + { |
| 140 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 141 | + } |
| 142 | + |
| 143 | + public function findTranslation(array $conditions) |
| 144 | + { |
| 145 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 146 | + } |
| 147 | + |
| 148 | + public function save($string) |
| 149 | + { |
| 150 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 151 | + } |
| 152 | + |
| 153 | + public function delete($string) |
| 154 | + { |
| 155 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 156 | + } |
| 157 | + |
| 158 | + public function deleteStrings($conditions) |
| 159 | + { |
| 160 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 161 | + } |
| 162 | + |
| 163 | + public function deleteTranslations($conditions) |
| 164 | + { |
| 165 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 166 | + } |
| 167 | + |
| 168 | + public function countStrings() |
| 169 | + { |
| 170 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 171 | + } |
| 172 | + |
| 173 | + public function countTranslations() |
| 174 | + { |
| 175 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 176 | + } |
| 177 | + |
| 178 | + public function createString($values = []) |
| 179 | + { |
| 180 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 181 | + } |
| 182 | + |
| 183 | + public function createTranslation($values = []) |
| 184 | + { |
| 185 | + throw new StringStorageRuntimeException(__FUNCTION__); |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + return new TranslationExtractCommand($writer, $reader, $extractor, $extensionPathResolver, $stringStorage); |
| 190 | +} |
| 191 | + |
| 192 | +class ExtensionPathResolverException extends \RuntimeException |
| 193 | +{ |
| 194 | +} |
| 195 | + |
| 196 | +class StringStorageRuntimeException extends \RuntimeException |
| 197 | +{ |
| 198 | +} |
0 commit comments