|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * (c) shopware AG <info@shopware.com> |
| 4 | + * For the full copyright and license information, please view the LICENSE |
| 5 | + * file that was distributed with this source code. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Swag\PayPal\DevOps\Command; |
| 9 | + |
| 10 | +use Shopware\Core\Framework\Log\Package; |
| 11 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 12 | +use Symfony\Component\Console\Command\Command; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | +use Symfony\Component\DomCrawler\Crawler; |
| 16 | +use Symfony\Component\HttpClient\HttpClient; |
| 17 | + |
| 18 | +#[AsCommand( |
| 19 | + name: 'swag:paypal:scrape:locales', |
| 20 | + description: 'Scrapes the PayPal developer website for locales and updates "Swag\PayPal\Util\PaypalLocales"', |
| 21 | +)] |
| 22 | +#[Package('checkout')] |
| 23 | +class LocaleScraperCommand extends Command |
| 24 | +{ |
| 25 | + public const REGION_KEY = 'region'; |
| 26 | + public const LOCALE_CODE_KEY = 'locale_code'; |
| 27 | + public const PRIORITY = 'priority'; |
| 28 | + private const PAYPAL_LOCALES_PAGE = 'https://developer.paypal.com/reference/locale-codes/'; |
| 29 | + |
| 30 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 31 | + { |
| 32 | + $client = HttpClient::create(); |
| 33 | + $response = $client->request('GET', self::PAYPAL_LOCALES_PAGE); |
| 34 | + $html = $response->getContent(); |
| 35 | + |
| 36 | + $crawler = new Crawler($html); |
| 37 | + $locales = []; |
| 38 | + |
| 39 | + $crawler->filter('table tbody tr')->each(function (Crawler $row) use (&$locales): void { |
| 40 | + $columns = $row->filter('td'); |
| 41 | + if ($columns->count() < 3) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + $countryCode = trim($columns->eq(1)->text()); |
| 46 | + |
| 47 | + $locales[$countryCode][] = [ |
| 48 | + self::PRIORITY => trim($columns->eq(2)->text()), |
| 49 | + self::LOCALE_CODE_KEY => trim($columns->eq(3)->text()), |
| 50 | + ]; |
| 51 | + }); |
| 52 | + |
| 53 | + $localesClassContent = ''; |
| 54 | + foreach ($locales as $countryCode => $localeOptions) { |
| 55 | + $localeString = ''; |
| 56 | + foreach ($localeOptions as $locale) { |
| 57 | + $localeString .= $this->getLocaleString($locale); |
| 58 | + } |
| 59 | + $localesClassContent .= \sprintf( |
| 60 | + " '%s' => [\n%s\n ],\n", |
| 61 | + $countryCode, |
| 62 | + trim($localeString, "\n") |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + $localesClass = \sprintf($this->getClassTemplate(), self::PAYPAL_LOCALES_PAGE, \trim($localesClassContent, "\n")); |
| 67 | + |
| 68 | + $localesClassPath = __DIR__ . '/../../Util/SupportedLocales.php'; |
| 69 | + $result = \file_put_contents($localesClassPath, $localesClass, \LOCK_EX); |
| 70 | + if ($result === false) { |
| 71 | + throw new \RuntimeException(\sprintf('File "%s" could not be written', $localesClassPath)); |
| 72 | + } |
| 73 | + |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + private function getLocaleString(array $locale): string |
| 78 | + { |
| 79 | + return <<<EOD |
| 80 | + {$locale[self::PRIORITY]} => '{$locale[self::LOCALE_CODE_KEY]}', |
| 81 | +
|
| 82 | +EOD; |
| 83 | + } |
| 84 | + |
| 85 | + private function getClassTemplate(): string |
| 86 | + { |
| 87 | + return <<<EOD |
| 88 | +<?php declare(strict_types=1); |
| 89 | +/* |
| 90 | + * (c) shopware AG <info@shopware.com> |
| 91 | + * For the full copyright and license information, please view the LICENSE |
| 92 | + * file that was distributed with this source code. |
| 93 | + */ |
| 94 | +
|
| 95 | +namespace Swag\PayPal\Util; |
| 96 | +
|
| 97 | +use Shopware\Core\Framework\Log\Package; |
| 98 | +
|
| 99 | +/** |
| 100 | + * @url %s |
| 101 | + */ |
| 102 | +#[Package('checkout')] |
| 103 | +final class SupportedLocales |
| 104 | +{ |
| 105 | + public const LOCALES = [ |
| 106 | +%s |
| 107 | + ]; |
| 108 | +} |
| 109 | +
|
| 110 | +EOD; |
| 111 | + } |
| 112 | +} |
0 commit comments