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