forked from yii2tech/file-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseBucket.php
119 lines (107 loc) · 2.87 KB
/
BaseBucket.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\filestorage;
use Yii;
use yii\base\Object;
use yii\helpers\Url;
use yii\log\Logger;
/**
* BaseBucket is a base class for the file storage buckets.
*
* @property string $name bucket name.
* @property StorageInterface $storage file storage, which owns the bucket.
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
abstract class BaseBucket extends Object implements BucketInterface
{
/**
* @var string bucket name.
*/
private $_name = '';
/**
* @var StorageInterface file storage, which owns the bucket.
*/
private $_storage;
/**
* Logs a message.
* @see Logger
* @param string $message message to be logged.
* @param int $level the level of the message.
*/
protected function log($message, $level = Logger::LEVEL_INFO)
{
if (!YII_DEBUG && $level === Logger::LEVEL_INFO) {
return;
}
$category = get_class($this) . '(' . $this->getName() . ')';
Yii::getLogger()->log($message, $level, $category);
}
/**
* Sets bucket name.
* @param string $name - bucket name.
* @return bool success.
*/
public function setName($name)
{
$this->_name = $name;
return true;
}
/**
* Gets current bucket name.
* @return string $name - bucket name.
*/
public function getName()
{
return $this->_name;
}
/**
* Sets bucket file storage.
* @param StorageInterface $storage - file storage.
* @return bool success.
*/
public function setStorage(StorageInterface $storage)
{
$this->_storage = $storage;
return true;
}
/**
* Gets bucket file storage.
* @return StorageInterface - bucket file storage.
*/
public function getStorage()
{
return $this->_storage;
}
/**
* @inheritdoc
*/
public function getFileUrl($fileName)
{
$baseUrl = $this->getStorage()->getBaseUrl();
if (is_array($baseUrl)) {
$url = $baseUrl;
$url['bucket'] = $this->getName();
$url['filename'] = $fileName;
return Url::to($url);
}
return $this->composeFileUrl($baseUrl, $fileName);
}
/**
* Composes file URL from the base URL and filename.
* This method is invoked at [[getFileUrl()]] in case base URL does not specify a URL route.
* @param string|null $baseUrl storage base URL.
* @param string $fileName self file name.
* @return string file web URL.
* @since 1.1.0
*/
protected function composeFileUrl($baseUrl, $fileName)
{
return $baseUrl . '/' . urlencode($this->getName()) . '/' . $fileName;
}
}