|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under The MIT License |
|
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
11
|
|
|
* @link https://github.com/Phauthentic |
|
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace Phauthentic\Authentication\HttpFactory; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
17
|
|
|
use Psr\Http\Message\StreamInterface; |
|
18
|
|
|
use RuntimeException; |
|
19
|
|
|
use Zend\Diactoros\Stream; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Zend Stream Factory |
|
23
|
|
|
*/ |
|
24
|
|
|
class ZendStreamFactory implements StreamFactoryInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public function createStream(string $content = ''): StreamInterface |
|
30
|
|
|
{ |
|
31
|
|
|
$resource = fopen('php://memory', 'rw'); |
|
32
|
|
|
$this->checkResource($resource); |
|
33
|
|
|
|
|
34
|
|
|
fwrite($resource, $content); |
|
35
|
|
|
rewind($resource); |
|
36
|
|
|
|
|
37
|
|
|
return $this->createStreamFromResource($resource); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Checks if the given variable is a resource and if not throws an exception |
|
42
|
|
|
* |
|
43
|
|
|
* @param mixed $resource Resource |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function checkResource($resource) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!is_resource($resource)) { |
|
49
|
|
|
throw new RuntimeException('Failed to open stream.'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface |
|
57
|
|
|
{ |
|
58
|
|
|
$resource = fopen($file, $mode); |
|
59
|
|
|
$this->checkResource($resource); |
|
60
|
|
|
|
|
61
|
|
|
return $this->createStreamFromResource($resource); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function createStreamFromResource($resource): StreamInterface |
|
68
|
|
|
{ |
|
69
|
|
|
return new Stream($resource); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|