MediaWiki master
JsonLoader.php
Go to the documentation of this file.
1<?php
8
9use Psr\Log\LoggerInterface;
10
21
29 public function __construct(
30 private readonly LoggerInterface $logger,
31 ) {
32 }
33
44 public function load( string $filePath, string $context = '', bool $allowMissing = false ): array {
45 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
46 $data = @file_get_contents( $filePath );
47 if ( $data === false ) {
48 if ( !$allowMissing ) {
49 $this->logger->error(
50 'Failed to read file contents for {context} at {filePath}',
51 [
52 'filePath' => $filePath,
53 'context' => $context,
54 ]
55 );
56 }
57
58 return [];
59 }
60
61 $decoded = json_decode( $data, true );
62 if ( !is_array( $decoded ) ) {
63 $this->logger->error(
64 'Expected an array from {filePath} for {context}, but received invalid data. JSON error: {error}',
65 [
66 'filePath' => $filePath,
67 'context' => $context,
68 'error' => json_last_error_msg(),
69 ]
70 );
71
72 return [];
73 }
74
75 return $decoded;
76 }
77}
load(string $filePath, string $context='', bool $allowMissing=false)
Loads and decodes a JSON file.
__construct(private readonly LoggerInterface $logger,)
Initializes the JsonLoader with a logger.