-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathStapler.php
More file actions
executable file
·251 lines (221 loc) · 6.87 KB
/
Stapler.php
File metadata and controls
executable file
·251 lines (221 loc) · 6.87 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Codesleeve\Stapler;
use Codesleeve\Stapler\Interfaces\Attachment as AttachmentInterface;
use Codesleeve\Stapler\Interfaces\Config as ConfigInterface;
use Codesleeve\Stapler\File\Image\Resizer;
use League\Flysystem\Filesystem as LeagueFilesystem;
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;
use Google\Cloud\Storage\StorageClient;
use Aws\S3\S3Client;
/**
* Easy file attachment management for Eloquent (Laravel 4).
*
* Credits to the guys at thoughtbot for creating the
* paperclip plugin (rails) from which this package is inspired.
* https://github.com/thoughtbot/paperclip
*
* @version v1.1.1
*
* @author Travis Bennett <tandrewbennett@hotmail.com>
*
* @link
*/
class Stapler
{
/**
* Holds the hash value for the current STAPLER_NULL constant.
*
* @var string
*/
protected static $staplerNull;
/**
* An instance of the interpolator class for processing interpolations.
*
* @var \Codesleeve\Stapler\Interfaces\Interpolator
*/
protected static $interpolator;
/**
* An instance of the validator class for validating attachment configurations.
*
* @var \Codesleeve\Stapler\Interfaces\Validator
*/
protected static $validator;
/**
* An instance of the resizer class for processing images.
*
* @var \Codesleeve\Stapler\Interfaces\Resizer
*/
protected static $resizer;
/**
* A configuration object instance.
*
* @var ConfigInterface
*/
protected static $config;
/**
* An array of image processing libs.
* Each time an new image processing lib (GD, Gmagick, or Imagick)
* is used, we'll cache it here in order to prevent
* memory leaks.
*
* @var array
*/
protected static $imageProcessors = [];
/**
* A key value store of S3 clients.
* Because S3 clients are model-attachment specific, each
* time we create a new one (for a given model/attachment combo)
* we'll need to cache it here in order to prevent
* memory leaks.
*
* @var array
*/
protected static $s3Clients = [];
/**
* Boot up stapler.
* Here, we'll register any needed constants and prime up
* the settings required by the package.
*/
public static function boot()
{
static::$staplerNull = sha1(time());
if (!defined('STAPLER_NULL')) {
define('STAPLER_NULL', static::$staplerNull);
}
}
/**
* Return a shared of instance of the Interpolator class.
* If there's currently no instance in memory we'll create one
* and then hang it as a property on this class.
*
* @return \Codesleeve\Stapler\Interfaces\Interpolator
*/
public static function getInterpolatorInstance()
{
if (static::$interpolator === null) {
$className = static::$config->get('bindings.interpolator');
static::$interpolator = new $className();
}
return static::$interpolator;
}
/**
* Return a shared of instance of the Validator class.
* If there's currently no instance in memory we'll create one
* and then hang it as a property on this class.
*
* @return \Codesleeve\Stapler\Interfaces\Validator
*/
public static function getValidatorInstance()
{
if (static::$validator === null) {
$className = static::$config->get('bindings.validator');
static::$validator = new $className();
}
return static::$validator;
}
/**
* Return a resizer object instance.
*
* @param string $type
*
* @return \Codesleeve\Stapler\Interfaces\Resizer
*/
public static function getResizerInstance($type)
{
$imagineInstance = static::getImagineInstance($type);
if (static::$resizer === null) {
$className = static::$config->get('bindings.resizer');
static::$resizer = new $className($imagineInstance);
} else {
static::$resizer->setImagine($imagineInstance);
}
return static::$resizer;
}
/**
* Return an instance of Imagine interface.
*
* @param string $type
*
* @return \Imagine\Image\ImagineInterface
*/
public static function getImagineInstance($type)
{
if (!isset(static::$imageProcessors[$type])) {
static::$imageProcessors[$type] = new $type();
}
return static::$imageProcessors[$type];
}
/**
* Return an S3Client object for a specific attachment type.
* If no instance has been defined yet we'll buld one and then
* cache it on the s3Clients property (for the current request only).
*
* @param AttachmentInterface $attachedFile
*
* @return S3Client
*/
public static function getS3ClientInstance(AttachmentInterface $attachedFile)
{
$modelName = $attachedFile->getInstanceClass();
$attachmentName = $attachedFile->getConfig()->name;
$key = "$modelName.$attachmentName";
if (array_key_exists($key, static::$s3Clients)) {
return static::$s3Clients[$key];
}
static::$s3Clients[$key] = static::buildS3Client($attachedFile);
return static::$s3Clients[$key];
}
/**
* Return an Filesystem object for Google Cloud Storage.
*
* @param AttachmentInterface $attachedFile
*
* @return \League\Flysystem\Filesystem
*/
public static function getGCSClientInstance(AttachmentInterface $attachedFile)
{
$storageClient = new StorageClient([
'projectId' => $attachedFile->google_cloud_project_id,
'keyFilePath' => $attachedFile->google_cloud_key_file,
]);
$bucket = $storageClient->bucket($attachedFile->google_cloud_storage_bucket);
$adapter = new GoogleStorageAdapter($storageClient, $bucket);
$filesystem = new LeagueFilesystem($adapter);
return $filesystem;
}
/**
* Return a configuration object instance.
* If no instance is currently set, we'll return an instance
* of Codesleeve\Stapler\Config\NativeConfig.
*
* @return ConfigInterface
*/
public static function getConfigInstance()
{
if (!static::$config) {
static::$config = new Config\NativeConfig();
}
return static::$config;
}
/**
* Set the configuration object instance.
*
* @param ConfigInterface $config
*/
public static function setConfigInstance(ConfigInterface $config)
{
static::$config = $config;
}
/**
* Build an S3Client instance using the information defined in
* this class's attachedFile object.
*
* @param $attachedFile
*
* @return S3Client
*/
protected static function buildS3Client(AttachmentInterface $attachedFile)
{
return S3Client::factory($attachedFile->s3_client_config);
}
}