Registry.php
18.7 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
<?php
/**
* Part of the Joomla Framework Registry Package
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Registry;
use Joomla\Utilities\ArrayHelper;
/**
* Registry class
*
* @since 1.0
*/
class Registry implements \JsonSerializable, \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* Registry Object
*
* @var \stdClass
* @since 1.0
*/
protected $data;
/**
* Flag if the Registry data object has been initialized
*
* @var boolean
* @since 1.5.2
*/
protected $initialized = false;
/**
* Registry instances container.
*
* @var Registry[]
* @since 1.0
* @deprecated 2.0 Object caching will no longer be supported
*/
protected static $instances = array();
/**
* Path separator
*
* @var string
* @since 1.4.0
*/
public $separator = '.';
/**
* Constructor
*
* @param mixed $data The data to bind to the new Registry object.
*
* @since 1.0
*/
public function __construct($data = null)
{
// Instantiate the internal data object.
$this->data = new \stdClass;
// Optionally load supplied data.
if ($data instanceof Registry)
{
$this->merge($data);
}
elseif (is_array($data) || is_object($data))
{
$this->bindData($this->data, $data);
}
elseif (!empty($data) && is_string($data))
{
$this->loadString($data);
}
}
/**
* Magic function to clone the registry object.
*
* @return void
*
* @since 1.0
*/
public function __clone()
{
$this->data = unserialize(serialize($this->data));
}
/**
* Magic function to render this object as a string using default args of toString method.
*
* @return string
*
* @since 1.0
*/
public function __toString()
{
return $this->toString();
}
/**
* Count elements of the data object
*
* @return integer The custom count as an integer.
*
* @link https://secure.php.net/manual/en/countable.count.php
* @since 1.3.0
*/
public function count()
{
return count(get_object_vars($this->data));
}
/**
* Implementation for the JsonSerializable interface.
* Allows us to pass Registry objects to json_encode.
*
* @return object
*
* @since 1.0
* @note The interface is only present in PHP 5.4 and up.
*/
public function jsonSerialize()
{
return $this->data;
}
/**
* Sets a default value if not already assigned.
*
* @param string $key The name of the parameter.
* @param mixed $default An optional value for the parameter.
*
* @return mixed The value set, or the default if the value was not previously set (or null).
*
* @since 1.0
*/
public function def($key, $default = '')
{
$value = $this->get($key, $default);
$this->set($key, $value);
return $value;
}
/**
* Check if a registry path exists.
*
* @param string $path Registry path (e.g. joomla.content.showauthor)
*
* @return boolean
*
* @since 1.0
*/
public function exists($path)
{
// Return default value if path is empty
if (empty($path))
{
return false;
}
// Explode the registry path into an array
$nodes = explode($this->separator, $path);
// Initialize the current node to be the registry root.
$node = $this->data;
$found = false;
// Traverse the registry to find the correct node for the result.
foreach ($nodes as $n)
{
if (is_array($node) && isset($node[$n]))
{
$node = $node[$n];
$found = true;
continue;
}
if (!isset($node->$n))
{
return false;
}
$node = $node->$n;
$found = true;
}
return $found;
}
/**
* Get a registry value.
*
* @param string $path Registry path (e.g. joomla.content.showauthor)
* @param mixed $default Optional default value, returned if the internal value is null.
*
* @return mixed Value of entry or null
*
* @since 1.0
*/
public function get($path, $default = null)
{
// Return default value if path is empty
if (empty($path))
{
return $default;
}
if (!strpos($path, $this->separator))
{
return (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '') ? $this->data->$path : $default;
}
// Explode the registry path into an array
$nodes = explode($this->separator, trim($path));
// Initialize the current node to be the registry root.
$node = $this->data;
$found = false;
// Traverse the registry to find the correct node for the result.
foreach ($nodes as $n)
{
if (is_array($node) && isset($node[$n]))
{
$node = $node[$n];
$found = true;
continue;
}
if (!isset($node->$n))
{
return $default;
}
$node = $node->$n;
$found = true;
}
if (!$found || $node === null || $node === '')
{
return $default;
}
return $node;
}
/**
* Returns a reference to a global Registry object, only creating it
* if it doesn't already exist.
*
* This method must be invoked as:
* <pre>$registry = Registry::getInstance($id);</pre>
*
* @param string $id An ID for the registry instance
*
* @return Registry The Registry object.
*
* @since 1.0
* @deprecated 2.0 Instantiate a new Registry instance instead
*/
public static function getInstance($id)
{
if (empty(self::$instances[$id]))
{
self::$instances[$id] = new self;
}
return self::$instances[$id];
}
/**
* Gets this object represented as an ArrayIterator.
*
* This allows the data properties to be accessed via a foreach statement.
*
* @return \ArrayIterator This object represented as an ArrayIterator.
*
* @see IteratorAggregate::getIterator()
* @since 1.3.0
*/
public function getIterator()
{
return new \ArrayIterator($this->data);
}
/**
* Load an associative array of values into the default namespace
*
* @param array $array Associative array of value to load
* @param boolean $flattened Load from a one-dimensional array
* @param string $separator The key separator
*
* @return Registry Return this object to support chaining.
*
* @since 1.0
*/
public function loadArray($array, $flattened = false, $separator = null)
{
if (!$flattened)
{
$this->bindData($this->data, $array);
return $this;
}
foreach ($array as $k => $v)
{
$this->set($k, $v, $separator);
}
return $this;
}
/**
* Load the public variables of the object into the default namespace.
*
* @param object $object The object holding the publics to load
*
* @return Registry Return this object to support chaining.
*
* @since 1.0
*/
public function loadObject($object)
{
$this->bindData($this->data, $object);
return $this;
}
/**
* Load the contents of a file into the registry
*
* @param string $file Path to file to load
* @param string $format Format of the file [optional: defaults to JSON]
* @param array $options Options used by the formatter
*
* @return Registry Return this object to support chaining.
*
* @since 1.0
*/
public function loadFile($file, $format = 'JSON', $options = array())
{
$data = file_get_contents($file);
return $this->loadString($data, $format, $options);
}
/**
* Load a string into the registry
*
* @param string $data String to load into the registry
* @param string $format Format of the string
* @param array $options Options used by the formatter
*
* @return Registry Return this object to support chaining.
*
* @since 1.0
*/
public function loadString($data, $format = 'JSON', $options = array())
{
// Load a string into the given namespace [or default namespace if not given]
$handler = AbstractRegistryFormat::getInstance($format, $options);
$obj = $handler->stringToObject($data, $options);
// If the data object has not yet been initialized, direct assign the object
if (!$this->initialized)
{
$this->data = $obj;
$this->initialized = true;
return $this;
}
$this->loadObject($obj);
return $this;
}
/**
* Merge a Registry object into this one
*
* @param Registry $source Source Registry object to merge.
* @param boolean $recursive True to support recursive merge the children values.
*
* @return Registry|false Return this object to support chaining or false if $source is not an instance of Registry.
*
* @since 1.0
*/
public function merge($source, $recursive = false)
{
if (!$source instanceof Registry)
{
return false;
}
$this->bindData($this->data, $source->toArray(), $recursive, false);
return $this;
}
/**
* Method to extract a sub-registry from path
*
* @param string $path Registry path (e.g. joomla.content.showauthor)
*
* @return Registry|null Registry object if data is present
*
* @since 1.2.0
*/
public function extract($path)
{
$data = $this->get($path);
if ($data === null)
{
return null;
}
return new Registry($data);
}
/**
* Checks whether an offset exists in the iterator.
*
* @param mixed $offset The array offset.
*
* @return boolean True if the offset exists, false otherwise.
*
* @since 1.0
*/
public function offsetExists($offset)
{
return (boolean) ($this->get($offset) !== null);
}
/**
* Gets an offset in the iterator.
*
* @param mixed $offset The array offset.
*
* @return mixed The array value if it exists, null otherwise.
*
* @since 1.0
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Sets an offset in the iterator.
*
* @param mixed $offset The array offset.
* @param mixed $value The array value.
*
* @return void
*
* @since 1.0
*/
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Unsets an offset in the iterator.
*
* @param mixed $offset The array offset.
*
* @return void
*
* @since 1.0
*/
public function offsetUnset($offset)
{
$this->remove($offset);
}
/**
* Set a registry value.
*
* @param string $path Registry Path (e.g. joomla.content.showauthor)
* @param mixed $value Value of entry
* @param string $separator The key separator
*
* @return mixed The value of the that has been set.
*
* @since 1.0
*/
public function set($path, $value, $separator = null)
{
if (empty($separator))
{
$separator = $this->separator;
}
/*
* Explode the registry path into an array and remove empty
* nodes that occur as a result of a double separator. ex: joomla..test
* Finally, re-key the array so they are sequential.
*/
$nodes = array_values(array_filter(explode($separator, $path), 'strlen'));
if (!$nodes)
{
return null;
}
// Initialize the current node to be the registry root.
$node = $this->data;
// Traverse the registry to find the correct node for the result.
for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++)
{
if (is_object($node))
{
if (!isset($node->{$nodes[$i]}) && ($i !== $n))
{
$node->{$nodes[$i]} = new \stdClass;
}
// Pass the child as pointer in case it is an object
$node = &$node->{$nodes[$i]};
continue;
}
if (is_array($node))
{
if (($i !== $n) && !isset($node[$nodes[$i]]))
{
$node[$nodes[$i]] = new \stdClass;
}
// Pass the child as pointer in case it is an array
$node = &$node[$nodes[$i]];
}
}
// Get the old value if exists so we can return it
switch (true)
{
case (is_object($node)):
$result = $node->{$nodes[$i]} = $value;
break;
case (is_array($node)):
$result = $node[$nodes[$i]] = $value;
break;
default:
$result = null;
break;
}
return $result;
}
/**
* Append value to a path in registry
*
* @param string $path Parent registry Path (e.g. joomla.content.showauthor)
* @param mixed $value Value of entry
*
* @return mixed The value of the that has been set.
*
* @since 1.4.0
*/
public function append($path, $value)
{
$result = null;
/*
* Explode the registry path into an array and remove empty
* nodes that occur as a result of a double dot. ex: joomla..test
* Finally, re-key the array so they are sequential.
*/
$nodes = array_values(array_filter(explode('.', $path), 'strlen'));
if ($nodes)
{
// Initialize the current node to be the registry root.
$node = $this->data;
// Traverse the registry to find the correct node for the result.
// TODO Create a new private method from part of code below, as it is almost equal to 'set' method
for ($i = 0, $n = count($nodes) - 1; $i <= $n; $i++)
{
if (is_object($node))
{
if (!isset($node->{$nodes[$i]}) && ($i !== $n))
{
$node->{$nodes[$i]} = new \stdClass;
}
// Pass the child as pointer in case it is an array
$node = &$node->{$nodes[$i]};
}
elseif (is_array($node))
{
if (($i !== $n) && !isset($node[$nodes[$i]]))
{
$node[$nodes[$i]] = new \stdClass;
}
// Pass the child as pointer in case it is an array
$node = &$node[$nodes[$i]];
}
}
if (!is_array($node))
// Convert the node to array to make append possible
{
$node = get_object_vars($node);
}
$node[] = $value;
$result = $value;
}
return $result;
}
/**
* Delete a registry value
*
* @param string $path Registry Path (e.g. joomla.content.showauthor)
*
* @return mixed The value of the removed node or null if not set
*
* @since 1.6.0
*/
public function remove($path)
{
// Cheap optimisation to direct remove the node if there is no separator
if (!strpos($path, $this->separator))
{
$result = (isset($this->data->$path) && $this->data->$path !== null && $this->data->$path !== '') ? $this->data->$path : null;
unset($this->data->$path);
return $result;
}
/*
* Explode the registry path into an array and remove empty
* nodes that occur as a result of a double separator. ex: joomla..test
* Finally, re-key the array so they are sequential.
*/
$nodes = array_values(array_filter(explode($this->separator, $path), 'strlen'));
if (!$nodes)
{
return null;
}
// Initialize the current node to be the registry root.
$node = $this->data;
$parent = null;
// Traverse the registry to find the correct node for the result.
for ($i = 0, $n = count($nodes) - 1; $i < $n; $i++)
{
if (is_object($node))
{
if (!isset($node->{$nodes[$i]}) && ($i !== $n))
{
continue;
}
$parent = &$node;
$node = $node->{$nodes[$i]};
continue;
}
if (is_array($node))
{
if (($i !== $n) && !isset($node[$nodes[$i]]))
{
continue;
}
$parent = &$node;
$node = $node[$nodes[$i]];
continue;
}
}
// Get the old value if exists so we can return it
switch (true)
{
case (is_object($node)):
$result = isset($node->{$nodes[$i]}) ? $node->{$nodes[$i]} : null;
unset($parent->{$nodes[$i]});
break;
case (is_array($node)):
$result = isset($node[$nodes[$i]]) ? $node[$nodes[$i]] : null;
unset($parent[$nodes[$i]]);
break;
default:
$result = null;
break;
}
return $result;
}
/**
* Transforms a namespace to an array
*
* @return array An associative array holding the namespace data
*
* @since 1.0
*/
public function toArray()
{
return (array) $this->asArray($this->data);
}
/**
* Transforms a namespace to an object
*
* @return object An an object holding the namespace data
*
* @since 1.0
*/
public function toObject()
{
return $this->data;
}
/**
* Get a namespace in a given string format
*
* @param string $format Format to return the string in
* @param mixed $options Parameters used by the formatter, see formatters for more info
*
* @return string Namespace in string format
*
* @since 1.0
*/
public function toString($format = 'JSON', $options = array())
{
// Return a namespace in a given format
$handler = AbstractRegistryFormat::getInstance($format, $options);
return $handler->objectToString($this->data, $options);
}
/**
* Method to recursively bind data to a parent object.
*
* @param object $parent The parent object on which to attach the data values.
* @param mixed $data An array or object of data to bind to the parent object.
* @param boolean $recursive True to support recursive bindData.
* @param boolean $allowNull True to allow null values.
*
* @return void
*
* @since 1.0
*/
protected function bindData($parent, $data, $recursive = true, $allowNull = true)
{
// The data object is now initialized
$this->initialized = true;
// Ensure the input data is an array.
$data = is_object($data) ? get_object_vars($data) : (array) $data;
foreach ($data as $k => $v)
{
if (!$allowNull && !(($v !== null) && ($v !== '')))
{
continue;
}
if ($recursive && ((is_array($v) && ArrayHelper::isAssociative($v)) || is_object($v)))
{
if (!isset($parent->$k))
{
$parent->$k = new \stdClass;
}
$this->bindData($parent->$k, $v);
continue;
}
$parent->$k = $v;
}
}
/**
* Method to recursively convert an object of data to an array.
*
* @param object $data An object of data to return as an array.
*
* @return array Array representation of the input object.
*
* @since 1.0
*/
protected function asArray($data)
{
$array = array();
if (is_object($data))
{
$data = get_object_vars($data);
}
foreach ($data as $k => $v)
{
if (is_object($v) || is_array($v))
{
$array[$k] = $this->asArray($v);
continue;
}
$array[$k] = $v;
}
return $array;
}
/**
* Dump to one dimension array.
*
* @param string $separator The key separator.
*
* @return string[] Dumped array.
*
* @since 1.3.0
*/
public function flatten($separator = null)
{
$array = array();
if (empty($separator))
{
$separator = $this->separator;
}
$this->toFlatten($separator, $this->data, $array);
return $array;
}
/**
* Method to recursively convert data to one dimension array.
*
* @param string $separator The key separator.
* @param array|object $data Data source of this scope.
* @param array $array The result array, it is passed by reference.
* @param string $prefix Last level key prefix.
*
* @return void
*
* @since 1.3.0
*/
protected function toFlatten($separator = null, $data = null, &$array = array(), $prefix = '')
{
$data = (array) $data;
if (empty($separator))
{
$separator = $this->separator;
}
foreach ($data as $k => $v)
{
$key = $prefix ? $prefix . $separator . $k : $k;
if (is_object($v) || is_array($v))
{
$this->toFlatten($separator, $v, $array, $key);
continue;
}
$array[$key] = $v;
}
}
}