Webmozart\Json\JsonDecoder::decodeFile PHP Method

decodeFile() public method

Decodes and validates a JSON file.
See also: decode
public decodeFile ( string $path, string | object $schema = null ) : mixed
$path string The path to the JSON file
$schema string | object The schema file or object
return mixed The decoded file
    public function decodeFile($path, $schema = null)
    {
        if (!file_exists($path)) {
            throw new FileNotFoundException(sprintf('The file %s does not exist.', $path));
        }
        $errorMessage = null;
        $errorCode = 0;
        set_error_handler(function ($errno, $errstr) use(&$errorMessage, &$errorCode) {
            $errorMessage = $errstr;
            $errorCode = $errno;
        });
        $content = file_get_contents($path);
        restore_error_handler();
        if (null !== $errorMessage) {
            if (false !== ($pos = strpos($errorMessage, '): '))) {
                // cut "file_get_contents(%path%):" to make message more readable
                $errorMessage = substr($errorMessage, $pos + 3);
            }
            throw new IOException(sprintf('Could not read %s: %s (%s)', $path, $errorMessage, $errorCode), $errorCode);
        }
        try {
            return $this->decode($content, $schema);
        } catch (DecodingFailedException $e) {
            // Add the file name to the exception
            throw new DecodingFailedException(sprintf('An error happened while decoding %s: %s', $path, $e->getMessage()), $e->getCode(), $e);
        } catch (ValidationFailedException $e) {
            // Add the file name to the exception
            throw new ValidationFailedException(sprintf("Validation of %s failed:\n%s", $path, $e->getErrorsAsString()), $e->getErrors(), $e->getCode(), $e);
        } catch (InvalidSchemaException $e) {
            // Add the file name to the exception
            throw new InvalidSchemaException(sprintf('An error happened while decoding %s: %s', $path, $e->getMessage()), $e->getCode(), $e);
        }
    }

Usage Example

 private function decodeFile($path)
 {
     $decoder = new JsonDecoder();
     // We can't use realpath(), which doesn't work inside PHARs.
     // However, we want to display nice paths if the file is not found.
     $schema = $decoder->decodeFile(Path::canonicalize(__DIR__ . '/../../res/schema/package-schema-1.0.json'));
     $configSchema = $schema->properties->config;
     if (!file_exists($path)) {
         throw FileNotFoundException::forPath($path);
     }
     try {
         return $decoder->decodeFile($path, $configSchema);
     } catch (ValidationFailedException $e) {
         throw new InvalidConfigException(sprintf("The configuration in %s is invalid:\n%s", $path, $e->getErrorsAsString()), 0, $e);
     } catch (DecodingFailedException $e) {
         throw new InvalidConfigException(sprintf("The configuration in %s could not be decoded:\n%s", $path, $e->getMessage()), $e->getCode(), $e);
     }
 }
All Usage Examples Of Webmozart\Json\JsonDecoder::decodeFile