forked from burzum/cakephp-file-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageManager.php
More file actions
157 lines (135 loc) · 3.65 KB
/
Copy pathStorageManager.php
File metadata and controls
157 lines (135 loc) · 3.65 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
<?php
/**
* StorageManager - manages and instantiates gaufrette storage engine instances
*
* @author Florian Krämer
* @copyright 2012 Florian Krämer
* @license MIT
*/
class StorageManager {
/**
* Adapter configs
*
* @var array
*/
protected $_adapterConfig = array(
'Local' => array(
'adapterOptions' => array(TMP, true),
'adapterClass' => '\Gaufrette\Adapter\Local',
'class' => '\Gaufrette\Filesystem'
)
);
/**
* Sets the default or active adapter that is used
*
* @var string
*/
protected $_activeAdapter = 'Local';
/**
* Return a singleton instance of the StorageManager.
*
* @return ClassRegistry instance
*/
public static function &getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] = new StorageManager();
}
return $instance[0];
}
/**
* Sets or gets the active storage adapter
*
* @param string $adapter
* @param array $options
* @return mixed
*/
public static function config($adapter = null, $options = array()) {
$_this = StorageManager::getInstance();
if (!empty($adapter) && !empty($options)) {
return $_this->_adapterConfig[$adapter] = $options;
}
if (empty($adapter)) {
return $_this->_adapterConfig[$_this->_activeAdapter];
}
if (isset($_this->_adapterConfig[$adapter])) {
return $_this->_adapterConfig[$adapter];
}
return false;
}
/**
* Sets or gets the active storage adapter
*
* @param string
* @return mixed
*/
public static function activeAdapter($name = null) {
$_this = StorageManager::getInstance();
if (empty($name)) {
return $_this->_activeAdapter;
}
if (isset($_this->_adapterConfig[$name])) {
return $_this->_activeAdapter = $name;
}
return false;
}
/**
* Flush all or a single adapter from the config
*
* @param string $name Config name, if none all adapters are flushed
* @throws RuntimeException
* @return boolean True on success
*/
public static function flush($name = null) {
$_this = StorageManager::getInstance();
if (empty($name)) {
$_this->_adapterConfig = array();
$_this->_activeAdapter = '';
}
if (isset($_this->_adapterConfig[$name])) {
if ($_this->_activeAdapter == $name) {
throw new RuntimeException(__d('file_storage', 'You can not flush the active adapter %s', $name));
}
unset($_this->_adapterConfig[$name]);
return true;
}
return false;
}
/**
* StorageAdapter
*
* @param mixed $adapterName string of adapter configuration or array of settings
* @param boolean $renewObject Creates a new instance of the given adapter in the configuration
* @throws RuntimeException
* @return Gaufrette object as configured by first argument
*/
public static function adapter($adapterName = null, $renewObject = false) {
$_this = StorageManager::getInstance();
if (empty($adapterName)) {
$adapterName = $_this->_activeAdapter;
}
$isConfigured = true;
if (is_string($adapterName)) {
if (!empty($_this->_adapterConfig[$adapterName])) {
$adapter = $_this->_adapterConfig[$adapterName];
} else {
throw new RuntimeException(__d('file_storage', 'Invalid Storage Adapter %s', $adapterName));
}
if (!empty($_this->_adapterConfig[$adapterName]['object']) && $renewObject === false) {
return $_this->_adapterConfig[$adapterName]['object'];
}
}
if (is_array($adapterName)) {
$adapter = $adapterName;
$isConfigured = false;
}
$class = $adapter['adapterClass'];
$Reflection = new ReflectionClass($class);
$adapterObject = $Reflection->newInstanceArgs($adapter['adapterOptions']);
$engineObject = new $adapter['class']($adapterObject);
if ($isConfigured) {
$_this->_adapterConfig[$adapterName]['object'] = &$engineObject;
}
return $engineObject;
}
}