-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbArray.php
More file actions
1226 lines (1063 loc) · 29.1 KB
/
Copy pathdbArray.php
File metadata and controls
1226 lines (1063 loc) · 29.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
namespace phprax;
use phprax\dbArray;
/**
* dbArray manages DB file as an array.
*
* PHP version 5
*
* @category DBA
* @package DbArray
* @author James Dornan <james@catch22.com>
* @copyright 2005-2008 James Dornan <james@catch22.com>
* @license http://www.phpractical.com/license/0_50.txt P4PHP
* @version SVN: 1786
* @link http://www.phpractical.com/DbArray
* @see http://www.php.net/dba
* @motto Cleverness catches it's master.
*
* @todo Handle keys as an array, using mtime comparison to refresh.
* @todo Change handler selection into it's own object
* @todo Check into creating a options class for the options
* @todo Set dba_optimize to fire off after counting n number of writes
* @todo camel case things.
* @todo Test access to the same db file by multiple processes
*
* @example
*
* dbArray takes a single array as it's argument.
*
* read - read access
* boolean
* default: false
*
* write - write access
* boolean
* default: false
*
* create - create file if needed, if does not exist
* boolean
* default: true
*
* new - create file if needed, truncate exists
* boolean
* default: false
*
* lock - lock file
* boolean
* default: false
*
* handler - dba_open file type handler
* string
* default: false, program will choose
*
* file - name of file to be opened
* string
* default: false, program exist without a file
*
* import - array of key/value pairs to be imported into db file
* array
* default: false, nothing to import
*
* hidden_keys - db file keys to hide
* array
* default: empty array
*
* // Simple
* $db = new DbArray(['file' => '/tmp/foo.db']);
* $db['key'] = $value;
* $db->delete();
*
* // Nesting is okay
*
* $db1 = new DbArray(['file' => '/tmp/foo.db']);
* $db1['key'] = DbArray::create([
* 'file' => '/tmp/bar.db',
* 'import' => ['A', 'B', 'C']
* ]);
*
* print $db1['key'][0]."\n"; // Will print 'A'
*
* $db1['key']->delete();
* $db1->delete();
*
*/
/**
* dbArray class that allows access to dba files as an array
*
* @category DBA
* @package dbArray
* @author James Dornan <james@catch22.com>
* @copyright 2003-2018 James Dornan <james@catch22.com>
* @license http://www.phpractical.com/license/0_50.txt P4PHP
* @version Release: 0.5
* @link http://www.phpractical.com/DbArray
* @since Class available since Release 0.5
*/
// class dbArray implements \Iterator, \ArrayAccess, \Countable, \Serializable {
// Would like to extend _ also.
// class dbArray extends _ implements \Iterator, \ArrayAccess, \Countable {
class dbArray implements \Iterator, \ArrayAccess, \Countable {
/**
* handler of currect db file
*
* @var string
*/
private $_handler = null;
/**
* false, for use when returning a reference
*
* @var boolean
*/
private $_false = false;
/**
* current dba resource
*
* @var resource
*/
private $_resource = false;
/**
* current key
*
* @var string
*/
private $_key = null;
/**
* get flag
*
* @var boolean
*/
private $_get_key = false;
/**
* flush flag
*
* @var boolean
*/
private $_flush = false;
/**
* flush new flag
*
* @var boolean
*/
private $_flushNew = null;
/**
* current value
*
* @var mixed
*/
private $_lastValue = null;
/**
* current value
*
* @var mixed
*/
private $_value = null;
/**
* debug
*
* @var boolean
*/
private $_debug = true;
/**
* file info
*
* @var object
*/
private $_fileinfo = false;
/**
* storage object
*
* @var object
*/
private $_storage_object = false;
/**
* perferred handlers
*
* @var array
*/
private $_perferred_handlers = [
'gdbm',
'db4',
'flatfile'
];
/**
* hidden keys
*
* @var array
*/
private $_hidden_keys = ['__count'];
/**
* backup of original hidden keys.
*
* @var array
*/
private $_hidden_keys_backup = false;
/**
* Default values
*
* @var array
*/
private $_options = [
'readonly' => false,
'create' => true,
'truncate' => false,
'lock' => false,
'handler' => false,
'file' => false,
'unlink' => false,
'import' => false,
'debug' => false,
'processors' => ['serialize', 'gzip'],
'hidden_keys' => []
];
/**
* db last mtime
*
* @vat int
*/
private $_last_mtime = false;
/**
* write count
*
* @vat int
*/
private $_writeCount = 0;
/**
* constructor
*
* @param string $options user supplied options
*/
public function __construct($options = false)
{
// Check that the dba extnesion is loaded.
$this->_checkExt();
if (!class_exists('SplFileInfo'))
throw new \Exception('SplFileInfo class is required.');
if (!empty($options['file']))
return $this->open($options);
}
/**
* Check for the extension central to this class, dba.
*
* If the extension is not loaded we'll give it one more chance, then throw
* an exception.
*/
private function _checkExt()
{
if (!_::loadExtension('dba'))
throw new \Exception('dba extension missing');
}
/**
* Tasks to complete when the object is destroyed.
*/
public function __destruct()
{
if ($this->_options['unlink'])
$this->delete();
else
$this->_flushValue();
}
/**
* sleep
*
* @return array
*/
public function __sleep()
{
return [
'_options',
'_hidden_keys',
'_hidden_keys_backup',
'_value',
'_key'
];
}
/**
* wakeup
*
* @return void
*/
public function __wakeup()
{
$this->open($this->_options);
}
/**
* method used when object called as a string
*
* @return string
*/
public function __toString()
{
return serialize($this->toArray());
}
/**
* call method
*
* @param string $method name of method being called
* @param array $parameters an array of parameters
*
* @return mixed
*
* @todo Add support for external extensions
*/
public function __call($method, $parameters)
{
if (!is_object($this->_fileinfo) || $method == 'openFile')
return false;
if (method_exists($this->_fileinfo, $method))
return call_user_func_array([$this->_fileinfo, $method], $parameters);
// This would be a good place to call user extensions
return false;
}
/**
* set method
*
* @param string $name name of member
* @param mixed $value value to be assigned to member
*
* @return boolean
*/
public function __set($name, $value)
{
return $this->offsetSet($name, $value);
}
/**
* get method
*
* @param string $name name of member
*
* @return mixed
*/
public function &__get($name)
{
return $this->offsetGet($name, true);
}
/**
* isset method
*
* @param string $name name of member
*
* @return boolean
*/
public function __isset($name)
{
return $this->offsetExists($name);
}
/**
* unset method
*
* @param string $name name of member
*
* @return void
*/
public function __unset($name)
{
return $this->offsetUnset($name);
}
/**
* invoke magic method
*
* @param $data import array
*
* @return mixed
*/
public function __invoke($data = false)
{
// No argument, then export data as an array.
if (empty($data))
return $this->toArray();
// Import an array
if (is_array($data))
return $this->import($data);
// Other actions here.
switch($data) {
default:
echo 'This is a test.' . PHP_EOL;
}
}
/**
* Return mode string for dba_open
*
* @return string
*/
public function getMode()
{
// import option settings into the current symbol table.
extract($this->_options, EXTR_SKIP);
// Illegal combinations
if ($readonly && ($create || $truncate)) {
$create = false;
$truncate = false;
}
if ($create && $truncate) {
throw new \Exception('Cannot combine the create and truncate '
. 'options.');
}
// Set the mode for open, based on the options supplied
if ($readonly) {
$mode = 'r';
} else {
$mode = 'w';
// create if it does not exist
if ($create)
$mode = 'c';
// create if it does not exist, and truncate if it does.
if ($truncate)
$mode = 'n';
}
// locking
// I wonder if this is still required for windows.
if ($lock)
$mode .= \stripos($_ENV['OS'], 'windows') === false ? 'd' : 'l';
return $mode;
}
/**
* Remove db file
*
* @return boolean return value of unlink
*/
public function delete()
{
$this->close();
return file_exists($this->_options['file'])
? \unlink($this->_options['file'])
: true;
}
/**
* open db file
*
* @param string $options user supplied options
*
* @return boolean
*
* @todo This needs to be broken up a bit.
*/
public function open(array $options)
{
// In case we are reusing this object with a new file.
$this->close();
// Merge supplied options with defaults
$this->_options = \array_merge($this->_options, $options);
// set debug flag
$this->_debug = $this->_options['debug'] === true;
// Check to see if this will be a new file.
$created = !\file_exists($this->_options['file']);
$this->_resource = \dba_popen($this->_options['file'], $this->getMode(),
$this->getHandler());
if ($this->_resource === false)
throw new \Exception('cannot open file, ' . $this->_options['file']);
// Backup of hidden keys so that they may be reset later.
if ($this->_hidden_keys_backup === false)
$this->_hidden_keys_backup = $this->_hidden_keys;
// If count is missing, add it.
if ($created) {
$this['__count'] = 0;
} elseif (!isset($this['__count'])) {
// Add the missing __count for use with the \count function
$keys = $this->keys();
// current cont is equal to the number of records in the file
// minus the number of hidden keys within those records.
$this['__count'] = count($keys) - count(array_intersect($keys,
$this->_hidden_keys));
if ($count > 0)
$this->rewind();
}
// See if we are importing an array into the db file.
if ($this->_options['import']) {
$this->import($this->_options['import']);
$this->_options['import'] = false;
$this->rewind();
}
// Set up our file into object for more information about the file
// this class is representing.
$this->_fileinfo = new \SplFileInfo($this->_options['file']);
// Get the last mtime of the file.
$this->_last_mtime = $this->getMTime();
// Load storage processors.
if (is_array($this->_options['processors']))
$this->_storage_object = new dbArray\storage($this->_options['processors']);
return true;
}
/**
* clean up and close the db file.
*
* @return bool
*/
public function close()
{
// nothing to close
if (!$this->_resource)
return true;
// first flush any refence changes
$this->_flushValue();
// If it's not read only we need to optimize and sync
if (!$this->_options['readonly']) {
\dba_optimize($this->_resource);
\dba_sync($this->_resource);
}
$resource = $this->_resource;
$this->_resource = false;
$this->_fileinfo = false;
return @\dba_close($resource);
}
/**
* create new DbArray
*
* @param array $options array of options used my the constrctor
*
* @return object
*/
public static function create($options)
{
return new self($options);
}
/**
* return the preferrred handler
*
* @return string db file type
*/
public function getHandler()
{
if (!empty($this->_options['handler']))
return $this->_options['handler'];
$handler = $this->getExtHandler($this->_options['file']);
if (!empty($handler))
return $handler;
return $this->getPerferredHandler();
}
/**
* return the name of the current file.
*
* @return string current file name
*/
public function getFilename()
{
return $this->_options['file'];
}
/**
* return name of handler from file externsion, if any.
*
* @param string $file name of file
*
* @return mixed valid dba handler or false
*/
public function getExtHandler($file)
{
$path_parts = \pathinfo($file);
$extension = \strtolower($path_parts['extension']);
if (\in_array($extension, \dba_handlers()))
return $extension;
else
return false;
}
/**
* return the perferred handler
*
* @return string
*/
public function getPerferredHandler()
{
$handlers = \dba_handlers();
foreach ($this->_perferred_handlers as $handler) {
if (\in_array($handler, $handlers, true))
return $handler;
}
}
/**
* check, whether a handler exists
*
* @param string $handler string name for a dba handler
*
* @return boolean
*/
public function handlerExists($handler)
{
return \in_array($handler, \dba_handlers(), true);
}
/**
* get hidden keys array
*
* @return array keys hidden when using next.
*/
public function getHiddenkeys()
{
return $this->_hidden_keys;
}
/**
* add hidden key(s)
*
* @param mixed $keys key or array of keys to be hidden
*
* @return void
*/
public function addHiddenkeys($keys)
{
if (\is_object($keys))
$keys = (array) $keys;
else if (!\is_array($keys))
$keys = [$keys];
foreach ($keys as $index=>$key) {
if (isset($this[$key])) {
$this['__count']--;
$this->_flushValue();
}
}
$this->_hidden_keys = \array_merge($this->_hidden_keys, $keys);
}
/**
* remove key(s) form the hidden list
*
* @param mixed $keys key or array of keys to be unhidden
*
* @return void
*/
public function removeHiddenkeys($keys)
{
if (\is_object($keys))
$keys = (array)$keys;
else if (!\is_array($keys))
$keys = [$keys];
foreach ($keys as $index=>$key) {
if (isset($this[$key])) {
$this['__count']++;
$this->_flushValue();
}
}
$this->_hidden_keys = \array_diff($this->_hidden_keys, $keys);
}
/**
* reset the hidden key list
*
* @return void
*/
public function resetHiddenKeys()
{
// If there is no reason to reset, we wont.
if ($this->_hidden_keys == $this->_hidden_keys_backup)
return;
// Adjust the count
foreach(\array_merge($this->_hidden_keys, $this->_hidden_keys_backup)
as $key) {
// If the key never had an impact on the count we have nothing to do
if (!isset($this[$key]))
continue;
if (
\in_array($key, $this->_hidden_keys)
&& !\in_array($key, $this->_hidden_keys_backup)
) {
$this['__count']++;
}
if (
!\in_array($key, $this->_hidden_keys)
&& \in_array($key, $this->_hidden_keys_backup)
) {
$this['__count']--;
}
$this->_flushValue();
}
$this->_hidden_keys = $this->_hidden_keys_backup;
}
/**
* should key be hidden
*
* @param string $key array key
*
* @return boolean
*/
public function isHidden($key)
{
return \in_array($key, $this->_hidden_keys, true);
}
/**
* check, whether a value exists
*
* @param string $offset array key
*
* @return boolean
*/
public function offsetExists($offset)
{
// first flush any refence changes
$this->_flushValue();
return \dba_exists($offset, $this->_resource);
}
/**
* write
*
* @param mixed $data to be stored in db file
*
* @return string
*/
private function _write($data)
{
return $this->_storage_object ?
$this->_storage_object->write($data) : $data;
}
/**
* read
*
* @param string $data output from dba_fetch
*
* @return mixed
*/
private function _read($data)
{
return $this->_storage_object ?
$this->_storage_object->read($data) : $data;
}
/**
* _flushValue
*
*/
private function _flushValue()
{
$this->_d(__METHOD__);
// If last action was not an offsetGet, then return.
if (!$this->_flush) {
$this->_d('Get flag not true.');
return;
}
// set the flag back.
$this->_flush = false;
if ($this->_lastValue === $this->_value)
return;
$this->_d('Get Key = ');
\ob_start();
\var_dump($this->_get_key);
$this->_d(\ob_get_contents());
\ob_end_clean();
$this->_d('Value = ');
\ob_start();
\var_dump($this->_value);
$this->_d(\ob_get_contents());
\ob_end_clean();
$this->offsetSet($this->_get_key, $this->_value);
if (!$this->_options['readonly'])
\dba_sync($this->_resource);
}
/**
* get a value from a db file using the key.
*
* This is tricky, so please try to follow. This function will return the
* value found at the requested offset as a reference. The returned
* reference can be altered by the interpeter after being returned. We
* have no idea if the variable returend has been altered or not. We're
* going to have to check, somehow.
*
* First step is to flag that we have performed an offset get which might
* be altered. This is done only if the dba file is opened writable.
*
* Next we need to flush the changed variable to the dba file. This is done
* by checking if we need to run flushValue at the start of almost any method.
* Using a trick function did not appear to work well enough, and was like
* using a fully automatic machine gun to swat a fly.
*
* @param string $offset array key
* @param boolean $flush should we flag for later flushing, default true
*
* @return mixed
*/
public function &offsetGet($offset, $flush = true)
{
$this->_d(__METHOD__ . ' "' . $offset . '"');
// first flush any refence changes
if ($this->_flush)
$this->_flushValue();
// set the _flush flag to true, if $flush is true and this is a file we
// can write to.
// If the readonly option is not true _flush will be se to true.
if ($flush)
$this->_flush = !$this->_options['readonly'];
// If the request is for the current value, just return that.
if ($offset !== null && $this->_key === $offset) {
$this->_flushNew = false;
$this->_lastValue = $this->_value;
$this->_get_key = $offset;
return $this->_value;
}
if (($value = \dba_fetch($offset, $this->_resource)) !== false) {
$this->_flushNew = false;
$this->_value = ($offset === '__count' ? $value : $this->_read($value));
$this->_lastValue = $this->_value;
$this->_key = $offset;
$this->_get_key = $offset;
return $this->_value;
}
$this->_flushNew = true;
$this->_get_key = $offset;
$this->_key = false;
$this->_value = null;
$this->_lastValue = false;
return $this->_value;
}
/**
* set a property
*
* @param string $offset array key
* @param mixed $value array value
*
* @return boolean
*/
public function offsetSet($offset, $value)
{
$this->_d(__METHOD__);
// first flush any refence changes
$this->_flushValue();
// I know. Can you tell me a better way?
// Search for the lowest unused numeric offset.
if ($offset === null || $offset == '')
for ($offset = 0; isset($this[$offset]); $offset++) {}
// Check to see if this a hiddent offset
$hidden = $this->isHidden($offset);
$this->_d(__METHOD__ . ' Offset: ' . $offset);
// $this->_d(__METHOD__ . ' Value: ' . $value);
// I suspect that the count will have to be updated using a different
// distinct method to make the code more clear, simple, and readable.
// Also, should updating the count change the _key and _value variables
// at all?
if ($offset === '__count') {
if (!\is_numeric($value)) {
throw new \Exception('illegal non-numeric value for __count "'
. $value . '"');
}
$writevalue = (int) $value;
} else {
$writevalue = $this->_write($value);
}
if (isset($this[$offset])) {
if ($this->_options['handler'] == 'cdb')
throw new \Exception('Illegal cdb update, set or get only.');
if (!\dba_replace($offset, $writevalue, $this->_resource)) {
throw new \Exception('Replace - offset ' . $offset .
' with value of ' . $value);
}
} else {
if (!\dba_insert($offset, $writevalue, $this->_resource)) {
throw new \Exception('Insert - offset ' . $offset .
' with value of ' . $value);
}
// increment the count of visible elements
if (!$hidden) {
$this['__count']++;
$this->_flushValue();
}
}
// For visible values we'll set the current key and value properties for
// use in key, current, and others.
$this->_key = $hidden ? null : $offset;
$this->_value = $hidden ? null : $value;
return true;
}
/**
* unset a property
*
* @param string $offset array key
*
* @return void
*/
public function offsetUnset($offset)
{
$this->_flushValue();
if (\dba_delete($offset, $this->_resource)) {
if (!$this->isHidden($offset)) {
$this['__count']--;
$this->_flushValue();
}
}
}
/**
* Return the array "pointer" to the first element