Skip to content

Commit 9affc71

Browse files
committed
Simplify how we load and save files in data/
We had different ways of saving and loading files from `data/`, so I decided to unify them to simplify things. I repurposed the `DomainInfo` class and named it `DataLoader`, so we can use the same class to load anything from the `data/` directory.
1 parent 819d734 commit 9affc71

11 files changed

Lines changed: 202 additions & 158 deletions

File tree

bin/console

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@ require __DIR__ . '/../vendor/autoload.php';
1212

1313
use Respect\Dev\Commands\LintDocsCommand;
1414
use Respect\Dev\Commands\LintMixinCommand;
15-
use Respect\Dev\Commands\SmokeTestsCheckCompleteCommand;
1615
use Respect\Dev\Commands\LintSpdxCommand;
16+
use Respect\Dev\Commands\SmokeTestsCheckCompleteCommand;
1717
use Respect\Dev\Commands\UpdateDomainSuffixesCommand;
1818
use Respect\Dev\Commands\UpdateDomainToplevelCommand;
1919
use Respect\Dev\Commands\UpdatePostalCodesCommand;
2020
use Respect\Dev\Differ\ConsoleDiffer;
21+
use Respect\Dev\Helpers\DataSaver;
2122
use Respect\Dev\Markdown\CompositeLinter;
2223
use Respect\Dev\Markdown\Linters\AssertionMessageLinter;
24+
use Respect\Dev\Markdown\Linters\ValidatorChangelogLinter;
2325
use Respect\Dev\Markdown\Linters\ValidatorHeaderLinter;
2426
use Respect\Dev\Markdown\Linters\ValidatorIndexLinter;
2527
use Respect\Dev\Markdown\Linters\ValidatorRelatedLinter;
2628
use Respect\Dev\Markdown\Linters\ValidatorTemplatesLinter;
27-
use Respect\Dev\Markdown\Linters\ValidatorChangelogLinter;
2829
use SebastianBergmann\Diff\Differ;
2930
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
3031
use Symfony\Component\Console\Application;
3132

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

3537
$application = new Application('Respect/Validation', '3.0');
3638
$application->addCommand(new LintDocsCommand($differ, new CompositeLinter(
@@ -43,9 +45,9 @@ return (static function () {
4345
)));
4446
$application->addCommand(new LintMixinCommand($differ));
4547
$application->addCommand(new LintSpdxCommand());
46-
$application->addCommand(new UpdateDomainSuffixesCommand());
47-
$application->addCommand(new UpdateDomainToplevelCommand());
48-
$application->addCommand(new UpdatePostalCodesCommand());
48+
$application->addCommand(new UpdateDomainSuffixesCommand($dataSaver));
49+
$application->addCommand(new UpdateDomainToplevelCommand($dataSaver));
50+
$application->addCommand(new UpdatePostalCodesCommand($dataSaver));
4951
$application->addCommand(new SmokeTestsCheckCompleteCommand());
5052

5153
return $application->run();

src-dev/Commands/UpdateDomainSuffixesCommand.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@
1010

1111
namespace Respect\Dev\Commands;
1212

13+
use Respect\Dev\Helpers\DataSaver;
1314
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Style\SymfonyStyle;
18-
use Symfony\Component\VarExporter\VarExporter;
1919

2020
use function array_keys;
2121
use function array_unique;
2222
use function count;
2323
use function dirname;
2424
use function explode;
2525
use function file_get_contents;
26-
use function file_put_contents;
2726
use function glob;
28-
use function implode;
2927
use function is_dir;
3028
use function mb_strtoupper;
3129
use function mkdir;
@@ -38,8 +36,6 @@
3836
use function trim;
3937
use function unlink;
4038

41-
use const PHP_EOL;
42-
4339
#[AsCommand(
4440
name: 'update:domain-suffixes',
4541
description: 'Update list of public domain suffixes',
@@ -48,6 +44,12 @@ final class UpdateDomainSuffixesCommand extends Command
4844
{
4945
private const string LIST_URL = 'https://publicsuffix.org/list/public_suffix_list.dat';
5046

47+
public function __construct(
48+
private readonly DataSaver $dataSaver,
49+
) {
50+
parent::__construct();
51+
}
52+
5153
protected function execute(InputInterface $input, OutputInterface $output): int
5254
{
5355
$io = new SymfonyStyle($input, $output);
@@ -100,20 +102,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
100102
continue;
101103
}
102104

103-
sort($suffixList);
104-
105-
$SPDX = '// SPDX';
106-
107-
$fileContent = implode(PHP_EOL, [
108-
'<?php declare(strict_types=1);',
109-
$SPDX . '-FileCopyrightText: 2007–22 Mozilla Foundation',
110-
$SPDX . '-License-Identifier: MPL-2.0-no-copyleft-exception',
111-
'return ' . VarExporter::export($suffixList) . ';' . PHP_EOL,
112-
]);
113-
114-
// Convert IDN TLD to ASCII (Punycode) for filename
115-
$filename = sprintf('%s/public-suffix/%s.php', $dataDir, $tld);
116-
file_put_contents($filename, $fileContent);
105+
$this->dataSaver->save(
106+
$suffixList,
107+
'2007–22 Mozilla Foundation',
108+
'MPL-2.0-no-copyleft-exception',
109+
sprintf('domain/public-suffix/%s.php', $tld),
110+
);
117111

118112
$progressBar->advance();
119113
}

src-dev/Commands/UpdateDomainToplevelCommand.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,20 @@
1010

1111
namespace Respect\Dev\Commands;
1212

13+
use Respect\Dev\Helpers\DataSaver;
1314
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Style\SymfonyStyle;
18-
use Symfony\Component\VarExporter\VarExporter;
1919

20-
use function basename;
2120
use function count;
22-
use function dirname;
2321
use function explode;
2422
use function file_get_contents;
25-
use function file_put_contents;
26-
use function implode;
2723
use function sprintf;
2824
use function str_starts_with;
2925
use function trim;
3026

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

35+
public function __construct(
36+
private readonly DataSaver $dataSaver,
37+
) {
38+
parent::__construct();
39+
}
40+
4141
protected function execute(InputInterface $input, OutputInterface $output): int
4242
{
4343
$io = new SymfonyStyle($input, $output);
@@ -71,26 +71,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7171
$tlds[] = $line;
7272
}
7373

74-
// Create the data file
75-
$dataFilename = dirname(__DIR__, 2) . '/data/domain/tld.php';
76-
77-
$SPDX = '// SPDX';
78-
79-
$fileContent = implode(PHP_EOL, [
80-
'<?php declare(strict_types=1);',
81-
$SPDX . '-FileCopyrightText: (c) https://data.iana.org/TLD/',
82-
$SPDX . '-License-Identifier: MPL-2.0',
83-
'return ' . VarExporter::export($tlds) . ';' . PHP_EOL,
84-
]);
85-
86-
// Write the data file
87-
if (file_put_contents($dataFilename, $fileContent) === false) {
88-
$io->error('Failed to write data file');
89-
90-
return Command::FAILURE;
91-
}
74+
$this->dataSaver->save(
75+
$tlds,
76+
'(c) https://data.iana.org/TLD/',
77+
'MPL-2.0',
78+
'domain/tld.php',
79+
);
9280

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

9684
return Command::SUCCESS;

src-dev/Commands/UpdatePostalCodesCommand.php

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,23 @@
1010

1111
namespace Respect\Dev\Commands;
1212

13+
use Respect\Dev\Helpers\DataSaver;
1314
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Console\Style\SymfonyStyle;
18-
use Symfony\Component\VarExporter\VarExporter;
1919

20-
use function basename;
2120
use function count;
22-
use function dirname;
2321
use function explode;
2422
use function file_get_contents;
25-
use function file_put_contents;
26-
use function implode;
27-
use function ksort;
28-
use function preg_replace;
2923
use function preg_replace_callback;
3024
use function sprintf;
3125
use function str_contains;
3226
use function str_starts_with;
3327
use function strlen;
3428
use function trim;
3529

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

38+
public function __construct(
39+
private readonly DataSaver $dataSaver,
40+
) {
41+
parent::__construct();
42+
}
43+
4644
protected function execute(InputInterface $input, OutputInterface $output): int
4745
{
4846
$io = new SymfonyStyle($input, $output);
@@ -104,28 +102,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104102
$postalCodes[$countryCode] = ['/^' . $countryFormat . '$/', '/' . $countryRegex . '/'];
105103
}
106104

107-
ksort($postalCodes);
108-
109-
// Create the data file
110-
$dataFilename = dirname(__DIR__, 2) . '/data/postal-code.php';
111-
112-
$SPDX = '// SPDX';
113-
114-
$fileContent = implode(PHP_EOL, [
115-
'<?php declare(strict_types=1);',
116-
$SPDX . '-FileCopyrightText: (c) https://download.geonames.org/export/dump/countryInfo.txt',
117-
$SPDX . '-License-Identifier: CC-BY-4.0',
118-
'return ' . preg_replace('/\\\([dws])/', '\\1', VarExporter::export($postalCodes)) . ';' . PHP_EOL,
119-
]);
120-
121-
// Write the data file
122-
if (file_put_contents($dataFilename, $fileContent) === false) {
123-
$io->error('Failed to write data file');
124-
125-
return Command::FAILURE;
126-
}
105+
$this->dataSaver->save(
106+
$postalCodes,
107+
'(c) https://download.geonames.org/export/dump/countryInfo.txt',
108+
'CC-BY-4.0',
109+
'postal-code.php',
110+
);
127111

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

131115
return Command::SUCCESS;

src-dev/Helpers/DataSaver.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* SPDX-License-Identifier: MIT
5+
* SPDX-FileCopyrightText: (c) Respect Project Contributors
6+
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Respect\Dev\Helpers;
12+
13+
use RuntimeException;
14+
use Symfony\Component\VarExporter\VarExporter;
15+
16+
use function dirname;
17+
use function file_put_contents;
18+
use function implode;
19+
use function ksort;
20+
use function preg_replace;
21+
22+
use const PHP_EOL;
23+
24+
final class DataSaver
25+
{
26+
/** @param array<string|int, mixed> $data */
27+
public function save(array $data, string $fileCopyrightText, string $licenseIdentifier, string $path): void
28+
{
29+
if (!array_is_list($data)) {
30+
ksort($data);
31+
}
32+
33+
$fileContent = implode(PHP_EOL, [
34+
// REUSE-IgnoreStart
35+
'<?php declare(strict_types=1);',
36+
'// SPDX-FileCopyrightText: ' . $fileCopyrightText,
37+
'// SPDX-License-Identifier: ' . $licenseIdentifier,
38+
// REUSE-IgnoreEnd
39+
'return ' . preg_replace('/\\\([dws])/', '\\1', VarExporter::export($data)) . ';' . PHP_EOL,
40+
]);
41+
42+
$filename = str_replace('/', DIRECTORY_SEPARATOR, dirname(__DIR__, 2) . '/data/' . $path);
43+
if (file_put_contents($filename, $fileContent) === false) {
44+
throw new RuntimeException('Failed to write data file: ' . $filename);
45+
}
46+
}
47+
}

src/Helpers/DataLoader.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* SPDX-License-Identifier: MIT
5+
* SPDX-FileCopyrightText: (c) Respect Project Contributors
6+
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Respect\Validation\Helpers;
12+
13+
use function dirname;
14+
use function file_exists;
15+
use function str_replace;
16+
17+
use const DIRECTORY_SEPARATOR;
18+
19+
final class DataLoader
20+
{
21+
/** @var array<string, array<int|string, mixed>> */
22+
private static array $runtimeCache = [];
23+
24+
/** @return array<string|int, mixed> */
25+
public static function load(string $basePath): array
26+
{
27+
$basePath = str_replace('/', DIRECTORY_SEPARATOR, $basePath);
28+
$path = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $basePath;
29+
if (!isset(static::$runtimeCache[$basePath])) {
30+
static::$runtimeCache[$basePath] = file_exists($path) ? require $path : [];
31+
}
32+
33+
return static::$runtimeCache[$basePath];
34+
}
35+
}

src/Helpers/DomainInfo.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)