diff options
| author | Eole <josselin.35@live.fr> | 2016-01-21 10:29:26 +0100 |
|---|---|---|
| committer | Eole <josselin.35@live.fr> | 2016-01-21 10:29:26 +0100 |
| commit | a44cc1d2e3c0f147e91a5c052ac7fd879e34e706 (patch) | |
| tree | bdd6f72e0ba732c4fcc0479d1cfcf4d0baa5885d /server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php | |
| parent | 35db27b0e62b4cdcb03b0d21bceb4efc769e6161 (diff) | |
Init Server Composer Components
Diffstat (limited to 'server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php')
| -rw-r--r-- | server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php new file mode 100644 index 0000000..bc43de6 --- /dev/null +++ b/server/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php @@ -0,0 +1,87 @@ +<?php + +/* + * This file is part of the JsonSchema package. + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace JsonSchema\Uri\Retrievers; + +use JsonSchema\Exception\ResourceNotFoundException; +use JsonSchema\Validator; + +/** + * Tries to retrieve JSON schemas from a URI using file_get_contents() + * + * @author Sander Coolen <sander@jibber.nl> + */ +class FileGetContents extends AbstractRetriever +{ + protected $messageBody; + + /** + * {@inheritDoc} + * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve() + */ + public function retrieve($uri) + { + $context = stream_context_create(array( + 'http' => array( + 'method' => 'GET', + 'header' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE + ))); + + set_error_handler(function() use ($uri) { + throw new ResourceNotFoundException('JSON schema not found at ' . $uri); + }); + $response = file_get_contents($uri); + restore_error_handler(); + + if (false === $response) { + throw new ResourceNotFoundException('JSON schema not found at ' . $uri); + } + if ($response == '' + && substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/' + ) { + throw new ResourceNotFoundException('JSON schema not found at ' . $uri); + } + + $this->messageBody = $response; + if (! empty($http_response_header)) { + $this->fetchContentType($http_response_header); + } else { + // Could be a "file://" url or something else - fake up the response + $this->contentType = null; + } + + return $this->messageBody; + } + + /** + * @param array $headers HTTP Response Headers + * @return boolean Whether the Content-Type header was found or not + */ + private function fetchContentType(array $headers) + { + foreach ($headers as $header) { + if ($this->contentType = self::getContentTypeMatchInHeader($header)) { + return true; + } + } + + return false; + } + + /** + * @param string $header + * @return string|null + */ + protected static function getContentTypeMatchInHeader($header) + { + if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) { + return trim($match[1]); + } + } +} |
