Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@ require __DIR__ . '/../vendor/autoload.php';

use Respect\Dev\Commands\LintDocsCommand;
use Respect\Dev\Commands\LintMixinCommand;
use Respect\Dev\Commands\SmokeTestsCheckCompleteCommand;
use Respect\Dev\Commands\LintSpdxCommand;
use Respect\Dev\Commands\SmokeTestsCheckCompleteCommand;
use Respect\Dev\Commands\UpdateDomainSuffixesCommand;
use Respect\Dev\Commands\UpdateDomainToplevelCommand;
use Respect\Dev\Commands\UpdatePostalCodesCommand;
use Respect\Dev\Differ\ConsoleDiffer;
use Respect\Dev\Helpers\DataSaver;
use Respect\Dev\Markdown\CompositeLinter;
use Respect\Dev\Markdown\Linters\AssertionMessageLinter;
use Respect\Dev\Markdown\Linters\ValidatorChangelogLinter;
use Respect\Dev\Markdown\Linters\ValidatorHeaderLinter;
use Respect\Dev\Markdown\Linters\ValidatorIndexLinter;
use Respect\Dev\Markdown\Linters\ValidatorRelatedLinter;
use Respect\Dev\Markdown\Linters\ValidatorTemplatesLinter;
use Respect\Dev\Markdown\Linters\ValidatorChangelogLinter;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use Symfony\Component\Console\Application;

return (static function () {
$differ = new ConsoleDiffer(new Differ(new UnifiedDiffOutputBuilder('', addLineNumbers: true)));
$dataSaver = new DataSaver();

$application = new Application('Respect/Validation', '3.0');
$application->addCommand(new LintDocsCommand($differ, new CompositeLinter(
Expand All @@ -43,9 +45,9 @@ return (static function () {
)));
$application->addCommand(new LintMixinCommand($differ));
$application->addCommand(new LintSpdxCommand());
$application->addCommand(new UpdateDomainSuffixesCommand());
$application->addCommand(new UpdateDomainToplevelCommand());
$application->addCommand(new UpdatePostalCodesCommand());
$application->addCommand(new UpdateDomainSuffixesCommand($dataSaver));
$application->addCommand(new UpdateDomainToplevelCommand($dataSaver));
$application->addCommand(new UpdatePostalCodesCommand($dataSaver));
$application->addCommand(new SmokeTestsCheckCompleteCommand());

return $application->run();
Expand Down
32 changes: 13 additions & 19 deletions src-dev/Commands/UpdateDomainSuffixesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@

namespace Respect\Dev\Commands;

use Respect\Dev\Helpers\DataSaver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\VarExporter\VarExporter;

use function array_keys;
use function array_unique;
use function count;
use function dirname;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function glob;
use function implode;
use function is_dir;
use function mb_strtoupper;
use function mkdir;
Expand All @@ -38,8 +36,6 @@
use function trim;
use function unlink;

use const PHP_EOL;

#[AsCommand(
name: 'update:domain-suffixes',
description: 'Update list of public domain suffixes',
Expand All @@ -48,6 +44,12 @@ final class UpdateDomainSuffixesCommand extends Command
{
private const string LIST_URL = 'https://publicsuffix.org/list/public_suffix_list.dat';

public function __construct(
private readonly DataSaver $dataSaver,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand Down Expand Up @@ -100,20 +102,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

sort($suffixList);

$SPDX = '// SPDX';

$fileContent = implode(PHP_EOL, [
'<?php declare(strict_types=1);',
$SPDX . '-FileCopyrightText: 2007–22 Mozilla Foundation',
$SPDX . '-License-Identifier: MPL-2.0-no-copyleft-exception',
'return ' . VarExporter::export($suffixList) . ';' . PHP_EOL,
]);

// Convert IDN TLD to ASCII (Punycode) for filename
$filename = sprintf('%s/public-suffix/%s.php', $dataDir, $tld);
file_put_contents($filename, $fileContent);
$this->dataSaver->save(
$suffixList,
'2007–22 Mozilla Foundation',
'MPL-2.0-no-copyleft-exception',
sprintf('domain/public-suffix/%s.php', $tld),
);

$progressBar->advance();
}
Expand Down
40 changes: 14 additions & 26 deletions src-dev/Commands/UpdateDomainToplevelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,20 @@

namespace Respect\Dev\Commands;

use Respect\Dev\Helpers\DataSaver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\VarExporter\VarExporter;

use function basename;
use function count;
use function dirname;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function sprintf;
use function str_starts_with;
use function trim;

use const PHP_EOL;

#[AsCommand(
name: 'update:domain-toplevel',
description: 'Update list of Top Level Domains (TLD) in the Tld validator',
Expand All @@ -38,6 +32,12 @@ final class UpdateDomainToplevelCommand extends Command
{
private const string LIST_URL = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';

public function __construct(
private readonly DataSaver $dataSaver,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand Down Expand Up @@ -71,26 +71,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$tlds[] = $line;
}

// Create the data file
$dataFilename = dirname(__DIR__, 2) . '/data/domain/tld.php';

$SPDX = '// SPDX';

$fileContent = implode(PHP_EOL, [
'<?php declare(strict_types=1);',
$SPDX . '-FileCopyrightText: (c) https://data.iana.org/TLD/',
$SPDX . '-License-Identifier: MPL-2.0',
'return ' . VarExporter::export($tlds) . ';' . PHP_EOL,
]);

// Write the data file
if (file_put_contents($dataFilename, $fileContent) === false) {
$io->error('Failed to write data file');

return Command::FAILURE;
}
$this->dataSaver->save(
$tlds,
'(c) https://data.iana.org/TLD/',
'MPL-2.0',
'domain/tld.php',
);

$io->success(sprintf('Updated %s successfully', basename($dataFilename)));
$io->success('Updated successfully');
$io->text(sprintf('Total TLDs: %d', count($tlds)));

return Command::SUCCESS;
Expand Down
44 changes: 14 additions & 30 deletions src-dev/Commands/UpdatePostalCodesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,23 @@

namespace Respect\Dev\Commands;

use Respect\Dev\Helpers\DataSaver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\VarExporter\VarExporter;

use function basename;
use function count;
use function dirname;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function ksort;
use function preg_replace;
use function preg_replace_callback;
use function sprintf;
use function str_contains;
use function str_starts_with;
use function strlen;
use function trim;

use const PHP_EOL;

#[AsCommand(
name: 'update:postal-codes',
description: 'Update the list of postal codes in the PostalCode validator',
Expand All @@ -43,6 +35,12 @@ final class UpdatePostalCodesCommand extends Command
{
private const string LIST_URL = 'https://download.geonames.org/export/dump/countryInfo.txt';

public function __construct(
private readonly DataSaver $dataSaver,
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand Down Expand Up @@ -104,28 +102,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$postalCodes[$countryCode] = ['/^' . $countryFormat . '$/', '/' . $countryRegex . '/'];
}

ksort($postalCodes);

// Create the data file
$dataFilename = dirname(__DIR__, 2) . '/data/postal-code.php';

$SPDX = '// SPDX';

$fileContent = implode(PHP_EOL, [
'<?php declare(strict_types=1);',
$SPDX . '-FileCopyrightText: (c) https://download.geonames.org/export/dump/countryInfo.txt',
$SPDX . '-License-Identifier: CC-BY-4.0',
'return ' . preg_replace('/\\\([dws])/', '\\1', VarExporter::export($postalCodes)) . ';' . PHP_EOL,
]);

// Write the data file
if (file_put_contents($dataFilename, $fileContent) === false) {
$io->error('Failed to write data file');

return Command::FAILURE;
}
$this->dataSaver->save(
$postalCodes,
'(c) https://download.geonames.org/export/dump/countryInfo.txt',
'CC-BY-4.0',
'postal-code.php',
);

$io->success(sprintf('Updated %s successfully', basename($dataFilename)));
$io->success('Updated successfully');
$io->text(sprintf('Total postal codes: %d', count($postalCodes)));

return Command::SUCCESS;
Expand Down
50 changes: 50 additions & 0 deletions src-dev/Helpers/DataSaver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/

declare(strict_types=1);

namespace Respect\Dev\Helpers;

use RuntimeException;
use Symfony\Component\VarExporter\VarExporter;

use function array_is_list;
use function dirname;
use function file_put_contents;
use function implode;
use function ksort;
use function preg_replace;
use function str_replace;

use const DIRECTORY_SEPARATOR;
use const PHP_EOL;

final class DataSaver
{
/** @param array<string|int, mixed> $data */
public function save(array $data, string $fileCopyrightText, string $licenseIdentifier, string $path): void
{
if (!array_is_list($data)) {
ksort($data);
}

$fileContent = implode(PHP_EOL, [
// REUSE-IgnoreStart
'<?php declare(strict_types=1);',
'// SPDX-FileCopyrightText: ' . $fileCopyrightText,
'// SPDX-License-Identifier: ' . $licenseIdentifier,
// REUSE-IgnoreEnd
'return ' . preg_replace('/\\\([dws])/', '\\1', VarExporter::export($data)) . ';' . PHP_EOL,
]);

$filename = str_replace('/', DIRECTORY_SEPARATOR, dirname(__DIR__, 2) . '/data/' . $path);
if (file_put_contents($filename, $fileContent) === false) {
throw new RuntimeException('Failed to write data file: ' . $filename);
}
}
}
35 changes: 35 additions & 0 deletions src/Helpers/DataLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/

declare(strict_types=1);

namespace Respect\Validation\Helpers;

use function dirname;
use function file_exists;
use function str_replace;

use const DIRECTORY_SEPARATOR;

final class DataLoader
{
/** @var array<string, array<int|string, mixed>> */
private static array $runtimeCache = [];

/** @return array<string|int, mixed> */
public static function load(string $basePath): array
{
$basePath = str_replace('/', DIRECTORY_SEPARATOR, $basePath);
$path = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $basePath;
if (!isset(static::$runtimeCache[$basePath])) {
static::$runtimeCache[$basePath] = file_exists($path) ? require $path : [];
}

return static::$runtimeCache[$basePath];
}
}
Loading
Loading