|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BenTools\UriFactory; |
|
4
|
|
|
|
|
5
|
|
|
use BenTools\UriFactory\Adapter\AdapterInterface; |
|
6
|
|
|
use BenTools\UriFactory\Adapter\GuzzleAdapter; |
|
7
|
|
|
use BenTools\UriFactory\Adapter\LeagueUriAdapter; |
|
8
|
|
|
use BenTools\UriFactory\Adapter\ZendDiactorosAdapter; |
|
9
|
|
|
use Psr\Http\Message\UriInterface; |
|
10
|
|
|
|
|
11
|
|
|
class UriFactory implements UriFactoryInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var AdapterInterface[] |
|
16
|
|
|
*/ |
|
17
|
|
|
private $adapters = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* UriFactory constructor. |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->adapters = $this->getDefaultAdapters(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
private function getDefaultAdapters() |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
|
|
GuzzleAdapter::class, |
|
34
|
|
|
ZendDiactorosAdapter::class, |
|
35
|
|
|
LeagueUriAdapter::class, |
|
36
|
|
|
]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $uri |
|
41
|
|
|
* @param UriFactoryInterface|null $factory |
|
42
|
|
|
* @return UriInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
public function createUri(string $uri, UriFactoryInterface $factory = null): UriInterface |
|
45
|
|
|
{ |
|
46
|
|
|
if (null !== $factory) { |
|
47
|
|
|
return $factory->createUri($uri); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Automatic discovery |
|
51
|
|
|
foreach ($this->adapters as $adapter) { |
|
52
|
|
|
if ($adapter::isInstalled()) { |
|
53
|
|
|
return $adapter::factory()->createUri($uri); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
throw new \RuntimeException( |
|
57
|
|
|
"No adapter is installed. Please install guzzlehttp/psr7, zendframework/zend-diactoros or league/uri." |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param UriFactoryInterface|null $factory |
|
63
|
|
|
* @return UriInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public function createUriFromCurrentLocation(UriFactoryInterface $factory = null): UriInterface |
|
66
|
|
|
{ |
|
67
|
|
|
if (!isset($_SERVER['HTTP_HOST'])) { |
|
68
|
|
|
throw new \RuntimeException('$_SERVER[\'HTTP_HOST\'] has not been set.'); |
|
69
|
|
|
} |
|
70
|
|
|
if (!isset($_SERVER['REQUEST_URI'])) { |
|
71
|
|
|
throw new \RuntimeException('$_SERVER[\'REQUEST_URI\'] has not been set.'); |
|
72
|
|
|
} |
|
73
|
|
|
$currentLocation = sprintf( |
|
74
|
|
|
'%s://%s%s', |
|
75
|
|
|
isset($_SERVER['HTTPS']) ? 'https' : 'http', |
|
76
|
|
|
$_SERVER['HTTP_HOST'], |
|
77
|
|
|
$_SERVER['REQUEST_URI'] |
|
78
|
|
|
); |
|
79
|
|
|
return $this->createUri($currentLocation, $factory); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return UriFactory |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function factory(): UriFactoryInterface |
|
86
|
|
|
{ |
|
87
|
|
|
return new self; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|