-
Notifications
You must be signed in to change notification settings - Fork 775
Simplify how we load and save files in data/
#1629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.