-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.php
More file actions
223 lines (195 loc) · 5.72 KB
/
Copy pathStorage.php
File metadata and controls
223 lines (195 loc) · 5.72 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
<?php
namespace Cleantalk\Common\Storage;
/**
* @method array_values()
*/
class Storage extends \ArrayObject {
/**
* @var string
*/
public $storage_name;
/**
* @var string
*/
public $directory;
/**
* @var string
*/
private $type;
/**
* @var array
*/
private $map;
public function __construct($name, $data = [], $directory = '', $type = 'php', $map = [])
{
$this->directory = $directory;
$this->type = $type;
$this->map = $map;
$data = ! $data ? $this->get($name) : $data; // Get data from directory if it's unset
parent::__construct( $data, \ArrayObject::STD_PROP_LIST|\ArrayObject::ARRAY_AS_PROPS );
$this->storage_name = $name;
}
/**
* Converts array to Storage type.
* Recursive.
*
* @param string $data_name
* @param array $data
*
* @return Storage
*/
public function convertToStorage($data_name, $data){
foreach ( $data as $name => &$item ) {
if ( is_array( $item ) ) {
$item = $this->convertToStorage( $name, $item );
}
}
return new self($data_name, $data, $this->directory);
}
/**
* Converts \Cleantalk\USP\Common\Storage to array type.
* Recursive.
*
* @param null $data
*
* @return array
*/
public function convertToArray( $data = null ){
$data = is_null( $data ) ? $this : $data;
$tmp = array();
foreach ( $data as $name => $item ) {
$tmp[ $name ] = $item instanceof self
? $this->convertToArray( $item )
: $item;
}
return $tmp;
}
/**
* Get option from file
* If file doesn't exist returns empty array
* If variable in the file doesn't exist returns empty array
*
* @param $option_name
*
* @return array
*/
protected function get($option_name)
{
$filename = $this->directory . DIRECTORY_SEPARATOR . $option_name . '.' . $this->type;
if( file_exists( $filename ) ){
switch( $this->type ){
case 'php':
require $filename;
$out = isset( $$option_name ) ? $$option_name : array();
break;
case 'csv':
$out = array();
$fd = fopen( $filename, 'r' );
while( $line = fgetcsv( $fd, 2000, ',', '\'' ) ){
$out[] = array_combine($this->map, $line);
}
fclose( $fd );
break;
}
}
return isset( $out ) ? $out : array();
}
/**
* Saves option to file as array
* Creates file at /data/{$this->storage_name}.php if it doesn't exist
*/
public function save()
{
$filename = $this->directory . DIRECTORY_SEPARATOR . $this->storage_name . '.' . $this->type;
switch( $this->type ){
case 'php':
file_put_contents( $filename , "<?php\n");
File::inject__variable(
$filename,
$this->storage_name,
$this->convertToArray()
);
break;
case 'csv':
$fp = fopen( $filename, 'w' );
foreach( $this->convertToArray() as &$field ){
fputcsv( $fp, $field, ',', '\'' );
}
fclose( $fp );
break;
}
}
/**
* Unset the option in the State class
* Deletes file (/data/{$this->storage_name}.php) if exists
*
* @param $option_name
*/
public function delete( $option_name = '' )
{
// Try to delete option with passed name
if($option_name){
if ( isset( $this->$option_name ) ) {
unset($this->$option_name);
}
// Delete this storage if no arguments passed
}else{
$option_name = $this->storage_name;
}
// Delete file with option
$filenames[] = $this->directory . $option_name . '.php';
$filenames[] = $this->directory . $option_name . '.csv';
$filenames[] = $this->directory . $option_name . '.gz';
foreach( $filenames as $filename){
if ( file_exists( $filename ) ) {
unlink($filename);
}
}
}
/**
* Check if the storage is not empty
*
* @return bool
*/
public function is_empty(){
foreach ( $this as $this1 ) {
return ! isset( $this1 );
}
return true;
}
/**
* Attempt to get data from internal storage
* then from $this->settings->key
* then from $this->data storage
* then from $this->get( $name ) method
* else rerun null
* @param $name
*
* @return mixed|null
*/
public function __get( $name )
{
if( isset( $this[$name] ) ) {
return $this[$name];
}
$value = $this->get( $name )
? $this->convertToStorage( $name, $this->get( $name ) )
: new Storage( $name, array() );
$this[$name] = $value;
return $value;
}
public function __call($func, $argv)
{
if (!is_callable($func) || substr($func, 0, 6) !== 'array_')
{
throw new \BadMethodCallException(__CLASS__.'->'.$func);
}
return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv));
}
public function __set( $name, $value ){
$this[ $name ] = $value;
}
public function __isset( $name ){
return isset( $this[ $name ] );
}
}