-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathLocalUriRetriever.php
87 lines (71 loc) · 2.04 KB
/
LocalUriRetriever.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
* This file is part of the Webmozart JSON package.
*
* (c) Bernhard Schussek <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Json\UriRetriever;
use JsonSchema\Uri\Retrievers\FileGetContents;
use JsonSchema\Uri\Retrievers\UriRetrieverInterface;
use Webmozart\PathUtil\Path;
/**
* @since 1.3
*
* @author Bernhard Schussek <[email protected]>
*/
class LocalUriRetriever implements UriRetrieverInterface
{
/**
* @var string[]
*/
private $mappings;
/**
* @var string
*/
private $baseDir;
/**
* @var UriRetrieverInterface
*/
private $filesystemRetriever;
/**
* @var UriRetrieverInterface
*/
private $fallbackRetriever;
/**
* @var UriRetrieverInterface
*/
private $lastUsedRetriever;
public function __construct($baseDir = null, array $mappings = array(), UriRetrieverInterface $fallbackRetriever = null)
{
$this->baseDir = $baseDir ? Path::canonicalize($baseDir) : null;
$this->mappings = $mappings;
$this->filesystemRetriever = new FileGetContents();
$this->fallbackRetriever = $fallbackRetriever ?: $this->filesystemRetriever;
}
/**
* {@inheritdoc}
*/
public function retrieve($uri)
{
if (isset($this->mappings[$uri])) {
$uri = $this->mappings[$uri];
if (Path::isLocal($uri)) {
$uri = 'file://'.($this->baseDir ? Path::makeAbsolute($uri, $this->baseDir) : $uri);
}
$this->lastUsedRetriever = $this->filesystemRetriever;
return $this->filesystemRetriever->retrieve($uri);
}
$this->lastUsedRetriever = $this->fallbackRetriever;
return $this->fallbackRetriever->retrieve($uri);
}
/**
* {@inheritdoc}
*/
public function getContentType()
{
return $this->lastUsedRetriever ? $this->lastUsedRetriever->getContentType() : null;
}
}