1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tebe\HttpFactory\Factory; |
5
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
use Slim\Http\Headers; |
12
|
|
|
use Slim\Http\Request; |
13
|
|
|
use Slim\Http\Response; |
14
|
|
|
use Slim\Http\Stream; |
15
|
|
|
use Slim\Http\UploadedFile; |
16
|
|
|
use Slim\Http\Uri; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Simple class to create response instances of PSR-7 classes. |
20
|
|
|
*/ |
21
|
|
|
class SlimFactory implements FactoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Check whether Slim Http is available |
25
|
|
|
*/ |
26
|
|
|
public static function isInstalled(): bool |
27
|
|
|
{ |
28
|
|
|
return class_exists('Slim\\Http\\Response') |
29
|
|
|
&& class_exists('Slim\\Http\\Request') |
30
|
|
|
&& class_exists('Slim\\Http\\Stream') |
31
|
|
|
&& class_exists('Slim\\Http\\Uri'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
38
|
|
|
{ |
39
|
|
|
$response = new Response($code); |
40
|
|
|
|
41
|
|
|
return $reasonPhrase !== '' ? $response->withStatus($code, $reasonPhrase) : $response; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
48
|
|
|
{ |
49
|
|
|
return new Request( |
50
|
|
|
$method, |
51
|
|
|
is_string($uri) ? $this->createUri($uri) : $uri, |
52
|
|
|
new Headers(), |
53
|
|
|
[], |
54
|
|
|
$serverParams, |
55
|
|
|
$this->createStream() |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function createUri(string $uri = ''): UriInterface |
63
|
|
|
{ |
64
|
|
|
return Uri::createFromString($uri); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function createStream(string $content = ''): StreamInterface |
71
|
|
|
{ |
72
|
|
|
$stream = $this->createStreamFromFile('php://temp', 'r+'); |
73
|
|
|
$stream->write($content); |
74
|
|
|
|
75
|
|
|
return $stream; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
82
|
|
|
{ |
83
|
|
|
return $this->createStreamFromResource(fopen($filename, $mode)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function createStreamFromResource($resource): StreamInterface |
90
|
|
|
{ |
91
|
|
|
return new Stream($resource); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function createUploadedFile( |
98
|
|
|
StreamInterface $stream, |
99
|
|
|
int $size = null, |
100
|
|
|
int $error = \UPLOAD_ERR_OK, |
101
|
|
|
string $clientFilename = null, |
102
|
|
|
string $clientMediaType = null |
103
|
|
|
): UploadedFileInterface { |
104
|
|
|
if ($size === null) { |
105
|
|
|
$size = $stream->getSize(); |
106
|
|
|
} |
107
|
|
|
$meta = $stream->getMetadata(); |
108
|
|
|
$file = $meta['uri']; |
109
|
|
|
if ($file === 'php://temp') { |
110
|
|
|
// Slim needs an actual path to the file |
111
|
|
|
$file = tempnam(sys_get_temp_dir(), 'factory-test'); |
112
|
|
|
file_put_contents($file, $stream->getContents()); |
113
|
|
|
} |
114
|
|
|
return new UploadedFile($file, $clientFilename, $clientMediaType, $size, $error); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|