ex
Fork of mbed-os-example-mbed5-blinky by
dumi_doc-master/schema/vendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php@44:c1d8923072ba, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:27:22 2017 +0800
- Revision:
- 44:c1d8923072ba
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 44:c1d8923072ba | 1 | <?php |
| TMBOY | 44:c1d8923072ba | 2 | |
| TMBOY | 44:c1d8923072ba | 3 | /* |
| TMBOY | 44:c1d8923072ba | 4 | * This file is part of the JsonSchema package. |
| TMBOY | 44:c1d8923072ba | 5 | * |
| TMBOY | 44:c1d8923072ba | 6 | * For the full copyright and license information, please view the LICENSE |
| TMBOY | 44:c1d8923072ba | 7 | * file that was distributed with this source code. |
| TMBOY | 44:c1d8923072ba | 8 | */ |
| TMBOY | 44:c1d8923072ba | 9 | |
| TMBOY | 44:c1d8923072ba | 10 | namespace JsonSchema\Uri\Retrievers; |
| TMBOY | 44:c1d8923072ba | 11 | |
| TMBOY | 44:c1d8923072ba | 12 | use JsonSchema\Exception\ResourceNotFoundException; |
| TMBOY | 44:c1d8923072ba | 13 | |
| TMBOY | 44:c1d8923072ba | 14 | /** |
| TMBOY | 44:c1d8923072ba | 15 | * Tries to retrieve JSON schemas from a URI using file_get_contents() |
| TMBOY | 44:c1d8923072ba | 16 | * |
| TMBOY | 44:c1d8923072ba | 17 | * @author Sander Coolen <sander@jibber.nl> |
| TMBOY | 44:c1d8923072ba | 18 | */ |
| TMBOY | 44:c1d8923072ba | 19 | class FileGetContents extends AbstractRetriever |
| TMBOY | 44:c1d8923072ba | 20 | { |
| TMBOY | 44:c1d8923072ba | 21 | protected $messageBody; |
| TMBOY | 44:c1d8923072ba | 22 | |
| TMBOY | 44:c1d8923072ba | 23 | /** |
| TMBOY | 44:c1d8923072ba | 24 | * {@inheritDoc} |
| TMBOY | 44:c1d8923072ba | 25 | * @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve() |
| TMBOY | 44:c1d8923072ba | 26 | */ |
| TMBOY | 44:c1d8923072ba | 27 | public function retrieve($uri) |
| TMBOY | 44:c1d8923072ba | 28 | { |
| TMBOY | 44:c1d8923072ba | 29 | $errorMessage = null; |
| TMBOY | 44:c1d8923072ba | 30 | set_error_handler(function ($errno, $errstr) use (&$errorMessage) { |
| TMBOY | 44:c1d8923072ba | 31 | $errorMessage = $errstr; |
| TMBOY | 44:c1d8923072ba | 32 | }); |
| TMBOY | 44:c1d8923072ba | 33 | $response = file_get_contents($uri); |
| TMBOY | 44:c1d8923072ba | 34 | restore_error_handler(); |
| TMBOY | 44:c1d8923072ba | 35 | |
| TMBOY | 44:c1d8923072ba | 36 | if ($errorMessage) { |
| TMBOY | 44:c1d8923072ba | 37 | throw new ResourceNotFoundException($errorMessage); |
| TMBOY | 44:c1d8923072ba | 38 | } |
| TMBOY | 44:c1d8923072ba | 39 | |
| TMBOY | 44:c1d8923072ba | 40 | if (false === $response) { |
| TMBOY | 44:c1d8923072ba | 41 | throw new ResourceNotFoundException('JSON schema not found at '.$uri); |
| TMBOY | 44:c1d8923072ba | 42 | } |
| TMBOY | 44:c1d8923072ba | 43 | |
| TMBOY | 44:c1d8923072ba | 44 | if ($response == '' |
| TMBOY | 44:c1d8923072ba | 45 | && substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/' |
| TMBOY | 44:c1d8923072ba | 46 | ) { |
| TMBOY | 44:c1d8923072ba | 47 | throw new ResourceNotFoundException('JSON schema not found at '.$uri); |
| TMBOY | 44:c1d8923072ba | 48 | } |
| TMBOY | 44:c1d8923072ba | 49 | |
| TMBOY | 44:c1d8923072ba | 50 | $this->messageBody = $response; |
| TMBOY | 44:c1d8923072ba | 51 | if (!empty($http_response_header)) { |
| TMBOY | 44:c1d8923072ba | 52 | $this->fetchContentType($http_response_header); |
| TMBOY | 44:c1d8923072ba | 53 | } else { |
| TMBOY | 44:c1d8923072ba | 54 | // Could be a "file://" url or something else - fake up the response |
| TMBOY | 44:c1d8923072ba | 55 | $this->contentType = null; |
| TMBOY | 44:c1d8923072ba | 56 | } |
| TMBOY | 44:c1d8923072ba | 57 | |
| TMBOY | 44:c1d8923072ba | 58 | return $this->messageBody; |
| TMBOY | 44:c1d8923072ba | 59 | } |
| TMBOY | 44:c1d8923072ba | 60 | |
| TMBOY | 44:c1d8923072ba | 61 | /** |
| TMBOY | 44:c1d8923072ba | 62 | * @param array $headers HTTP Response Headers |
| TMBOY | 44:c1d8923072ba | 63 | * @return boolean Whether the Content-Type header was found or not |
| TMBOY | 44:c1d8923072ba | 64 | */ |
| TMBOY | 44:c1d8923072ba | 65 | private function fetchContentType(array $headers) |
| TMBOY | 44:c1d8923072ba | 66 | { |
| TMBOY | 44:c1d8923072ba | 67 | foreach ($headers as $header) { |
| TMBOY | 44:c1d8923072ba | 68 | if ($this->contentType = self::getContentTypeMatchInHeader($header)) { |
| TMBOY | 44:c1d8923072ba | 69 | return true; |
| TMBOY | 44:c1d8923072ba | 70 | } |
| TMBOY | 44:c1d8923072ba | 71 | } |
| TMBOY | 44:c1d8923072ba | 72 | |
| TMBOY | 44:c1d8923072ba | 73 | return false; |
| TMBOY | 44:c1d8923072ba | 74 | } |
| TMBOY | 44:c1d8923072ba | 75 | |
| TMBOY | 44:c1d8923072ba | 76 | /** |
| TMBOY | 44:c1d8923072ba | 77 | * @param string $header |
| TMBOY | 44:c1d8923072ba | 78 | * @return string|null |
| TMBOY | 44:c1d8923072ba | 79 | */ |
| TMBOY | 44:c1d8923072ba | 80 | protected static function getContentTypeMatchInHeader($header) |
| TMBOY | 44:c1d8923072ba | 81 | { |
| TMBOY | 44:c1d8923072ba | 82 | if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) { |
| TMBOY | 44:c1d8923072ba | 83 | return trim($match[1]); |
| TMBOY | 44:c1d8923072ba | 84 | } |
| TMBOY | 44:c1d8923072ba | 85 | |
| TMBOY | 44:c1d8923072ba | 86 | return null; |
| TMBOY | 44:c1d8923072ba | 87 | } |
| TMBOY | 44:c1d8923072ba | 88 | } |
