1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlphaSnow\AliyunOss; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\AdapterInterface; |
6
|
|
|
use League\Flysystem\Util; |
7
|
|
|
use OSS\Core\OssException; |
8
|
|
|
use OSS\OssClient; |
9
|
|
|
|
10
|
|
|
trait AliyunOssReadTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
public function has($path) |
16
|
|
|
{ |
17
|
|
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
18
|
|
|
|
19
|
|
|
return $this->client->doesObjectExist($this->bucket, $object); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function read($path) |
26
|
|
|
{ |
27
|
|
|
$result = $this->readObject($path); |
|
|
|
|
28
|
|
|
$result['contents'] = (string)$result['raw_contents']; |
29
|
|
|
unset($result['raw_contents']); |
30
|
|
|
return $result; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function readStream($path) |
37
|
|
|
{ |
38
|
|
|
$result = $this->readObject($path); |
|
|
|
|
39
|
|
|
$result['stream'] = $result['raw_contents']; |
40
|
|
|
rewind($result['stream']); |
41
|
|
|
// Ensure the EntityBody object destruction doesn't close the stream |
42
|
|
|
// $result['raw_contents']->detachStream(); |
43
|
|
|
unset($result['raw_contents']); |
44
|
|
|
|
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function listContents($directory = '', $recursive = false) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$dirObjects = $this->listDirObjects($directory, true); |
|
|
|
|
54
|
|
|
$contents = $dirObjects["objects"]; |
55
|
|
|
|
56
|
|
|
$result = array_map([$this, 'normalizeResponse'], $contents); |
57
|
|
|
$result = array_filter($result, function ($value) { |
58
|
|
|
return $value['path'] !== false; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
return Util::emulateDirectories($result); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function getMetadata($path) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$objectMeta = $this->client->getObjectMeta($this->bucket, $object); |
73
|
|
|
} catch (OssException $e) { |
74
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $objectMeta; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getSize($path) |
85
|
|
|
{ |
86
|
|
|
$object = $this->getMetadata($path); |
87
|
|
|
$object['size'] = $object['content-length']; |
88
|
|
|
return $object; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function getMimetype($path) |
95
|
|
|
{ |
96
|
|
|
if ($object = $this->getMetadata($path)) { |
97
|
|
|
$object['mimetype'] = $object['content-type']; |
98
|
|
|
} |
99
|
|
|
return $object; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getTimestamp($path) |
106
|
|
|
{ |
107
|
|
|
if ($object = $this->getMetadata($path)) { |
108
|
|
|
$object['timestamp'] = strtotime($object['last-modified']); |
109
|
|
|
} |
110
|
|
|
return $object; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function getVisibility($path) |
117
|
|
|
{ |
118
|
|
|
$object = $this->applyPathPrefix($path); |
|
|
|
|
119
|
|
|
try { |
120
|
|
|
$acl = $this->client->getObjectAcl($this->bucket, $object); |
121
|
|
|
} catch (OssException $e) { |
122
|
|
|
$this->logErr(__FUNCTION__, $e); |
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($acl == OssClient::OSS_ACL_TYPE_PUBLIC_READ) { |
127
|
|
|
$res['visibility'] = AdapterInterface::VISIBILITY_PUBLIC; |
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
$res['visibility'] = AdapterInterface::VISIBILITY_PRIVATE; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $res; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.