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