|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TusPhp\Tus; |
|
4
|
|
|
|
|
5
|
|
|
use TusPhp\Cache\Cacheable; |
|
6
|
|
|
use TusPhp\Cache\CacheFactory; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractTus |
|
11
|
|
|
{ |
|
12
|
|
|
/** @const string Tus protocol version. */ |
|
13
|
|
|
public const TUS_PROTOCOL_VERSION = '1.0.0'; |
|
14
|
|
|
|
|
15
|
|
|
/** @const string Upload type partial. */ |
|
16
|
|
|
public const UPLOAD_TYPE_PARTIAL = 'partial'; |
|
17
|
|
|
|
|
18
|
|
|
/** @const string Upload type final. */ |
|
19
|
|
|
public const UPLOAD_TYPE_FINAL = 'final'; |
|
20
|
|
|
|
|
21
|
|
|
/** @const string Name separator for partial upload. */ |
|
22
|
|
|
protected const PARTIAL_UPLOAD_NAME_SEPARATOR = '_'; |
|
23
|
|
|
|
|
24
|
|
|
/** @const string Upload type normal. */ |
|
25
|
|
|
protected const UPLOAD_TYPE_NORMAL = 'normal'; |
|
26
|
|
|
|
|
27
|
|
|
/** @const string Header Content Type */ |
|
28
|
|
|
protected const HEADER_CONTENT_TYPE = 'application/offset+octet-stream'; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Cacheable */ |
|
31
|
|
|
protected $cache; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
protected $apiPath = '/files'; |
|
35
|
|
|
|
|
36
|
|
|
/** @var EventDispatcherInterface */ |
|
37
|
|
|
protected $dispatcher; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set cache. |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $cache |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \ReflectionException |
|
45
|
|
|
* |
|
46
|
|
|
* @return self |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function setCache($cache) : self |
|
49
|
|
|
{ |
|
50
|
2 |
|
if (\is_string($cache)) { |
|
51
|
2 |
|
$this->cache = CacheFactory::make($cache); |
|
52
|
1 |
|
} elseif ($cache instanceof Cacheable) { |
|
53
|
1 |
|
$this->cache = $cache; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
$prefix = 'tus:' . strtolower((new \ReflectionClass(static::class))->getShortName()) . ':'; |
|
57
|
|
|
|
|
58
|
2 |
|
$this->cache->setPrefix($prefix); |
|
59
|
|
|
|
|
60
|
2 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get cache. |
|
65
|
|
|
* |
|
66
|
|
|
* @return Cacheable |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getCache() : Cacheable |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->cache; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set API path. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $path |
|
77
|
|
|
* |
|
78
|
|
|
* @return self |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function setApiPath(string $path) : self |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->apiPath = $path; |
|
83
|
|
|
|
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get API path. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function getApiPath() : string |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->apiPath; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set and get event dispatcher. |
|
99
|
|
|
* |
|
100
|
|
|
* @return EventDispatcherInterface |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function event() : EventDispatcherInterface |
|
103
|
|
|
{ |
|
104
|
1 |
|
if ( ! $this->dispatcher) { |
|
105
|
1 |
|
$this->dispatcher = new EventDispatcher(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $this->dispatcher; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set event dispatcher. |
|
113
|
|
|
* |
|
114
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
115
|
|
|
* |
|
116
|
|
|
* @return self |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function setDispatcher(EventDispatcherInterface $dispatcher) : self |
|
119
|
|
|
{ |
|
120
|
1 |
|
$this->dispatcher = $dispatcher; |
|
121
|
|
|
|
|
122
|
1 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|