-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManager.php
168 lines (144 loc) · 4.46 KB
/
Manager.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace artkost\attachment;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;
class Manager extends Component
{
/**
* Parameter passed when upload file
*/
const PARAM_NAME = 'file';
/**
* Path of storage in web
* @var string
*/
public $storageUrl = '@web/storage';
/**
* Path of storage in filesystem
* @var string
*/
public $storagePath = '@webroot/storage';
/**
* Temp folder for temporary files
* @var string
*/
public $tempPath = '@webroot/storage/temp';
/**
* @var string
*/
public $attachmentFileTable = '{{%attachment_file}}';
/**
* Instantiated AttachmentFile attributes
*/
protected $modelsInstances = [];
public function init()
{
parent::init();
$this->createDirectory($this->storagePath);
$this->createDirectory($this->tempPath);
}
/**
* Ensure or create a folder
* @param $path
* @throws InvalidConfigException
* @throws \yii\base\Exception
*/
public function createDirectory($path)
{
if (!FileHelper::createDirectory($path)) {
throw new InvalidConfigException("Directory {$path} doesn't exist or cannot be created.");
}
}
/**
* Translates a message to the specified language.
*
* This is a shortcut method of [[\yii\i18n\I18N::translate()]].
*
* The translation will be conducted according to the message category and the target language will be used.
*
* You can add parameters to a translation message that will be substituted with the corresponding value after
* translation. The format for this is to use curly brackets around the parameter name as you can see in the following example:
*
* ```php
* $username = 'Alexander';
* echo \Yii::t('app', 'Hello, {username}!', ['username' => $username]);
* ```
*
* Further formatting of message parameters is supported using the [PHP intl extensions](http://www.php.net/manual/en/intro.intl.php)
* message formatter. See [[\yii\i18n\I18N::translate()]] for more details.
*
* @param string $category the message category.
* @param string $message the message to be translated.
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
* @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current
* [[\yii\base\Application::language|application language]] will be used.
*
* @return string the translated message.
*/
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t('attachment/' . $category, $message, $params, $language);
}
/**
* @return static
*/
public static function getInstance()
{
return Yii::$app->attachmentManager;
}
/**
* @return UploadedFile
*/
public static function getUploadedFile()
{
return UploadedFile::getInstanceByName(self::PARAM_NAME);
}
/**
* @return UploadedFile[]
*/
public static function getUploadedFiles()
{
return UploadedFile::getInstancesByName(self::PARAM_NAME);
}
public function addAttachmentModel($ownerClass, $attribute, $config)
{
$name = $ownerClass . $attribute;
return $this->modelsInstances[$name] = Yii::createObject($config);
}
public function getAttachmentModel($ownerClass, $attribute)
{
$name = $ownerClass . $attribute;
if (!isset($this->modelsInstances[$name])) {
//try to create model that attaches AttachBehavior
Yii::createObject(['class' => $ownerClass]);
if (!isset($this->modelsInstances[$name])) {
return null;
}
}
return $this->modelsInstances[$name];
}
/**
* @return string
*/
public function getStorageUrl()
{
return FileHelper::normalizePath(Yii::getAlias($this->storageUrl), '/') . '/';
}
/**
* @return string
*/
public function getStoragePath()
{
return FileHelper::normalizePath(Yii::getAlias($this->storagePath)) . DIRECTORY_SEPARATOR;
}
/**
* @return string
*/
public function getTempPath()
{
return FileHelper::normalizePath(Yii::getAlias($this->tempPath)) . DIRECTORY_SEPARATOR;
}
}