CompiledData.php
3 years ago
Cookie.php
4 years ago
Option.php
9 years ago
Permissions.php
1 year ago
Preferences.php
4 years ago
RecentItems.php
2 years ago
Serializable.php
3 years ago
Session.php
4 years ago
Settings.php
1 year ago
Transient.php
7 years ago
Upload.php
2 years ago
CompiledData.php
94 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Static, read-only caching of data held in serialized files. |
| 4 | * Used for pre-built arrays of information such as plural forms. |
| 5 | */ |
| 6 | class Loco_data_CompiledData implements ArrayAccess, Countable, IteratorAggregate { |
| 7 | |
| 8 | /** |
| 9 | * @var array |
| 10 | */ |
| 11 | private static $reg = []; |
| 12 | |
| 13 | /** |
| 14 | * @var string |
| 15 | */ |
| 16 | private $name; |
| 17 | |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | private $data; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @param string $name |
| 26 | * @return self |
| 27 | */ |
| 28 | public static function get( $name ){ |
| 29 | if( ! isset(self::$reg[$name]) ){ |
| 30 | self::$reg[$name] = new Loco_data_CompiledData($name); |
| 31 | } |
| 32 | return self::$reg[$name]; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Remove all cached data from memory |
| 38 | * @return void |
| 39 | */ |
| 40 | public static function flush(){ |
| 41 | self::$reg = []; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | private function __construct( $name ){ |
| 46 | $path = 'lib/data/'.$name.'.php'; |
| 47 | $this->data = loco_include( $path ); |
| 48 | $this->name = $name; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | public function destroy(){ |
| 53 | unset( self::$reg[$this->name], $this->data ); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | #[ReturnTypeWillChange] |
| 58 | public function offsetGet( $k ){ |
| 59 | return isset($this->data[$k]) ? $this->data[$k] : null; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | #[ReturnTypeWillChange] |
| 64 | public function offsetExists( $k ){ |
| 65 | return isset($this->data[$k]); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | #[ReturnTypeWillChange] |
| 70 | public function offsetUnset( $k ){ |
| 71 | throw new RuntimeException('Read only'); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | #[ReturnTypeWillChange] |
| 76 | public function offsetSet( $k, $v ){ |
| 77 | throw new RuntimeException('Read only'); |
| 78 | } |
| 79 | |
| 80 | #[ReturnTypeWillChange] |
| 81 | public function count(){ |
| 82 | return count($this->data); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Implements IteratorAggregate::getIterator |
| 87 | * @return ArrayIterator |
| 88 | */ |
| 89 | #[ReturnTypeWillChange] |
| 90 | public function getIterator(){ |
| 91 | return new ArrayIterator( $this->data ); |
| 92 | } |
| 93 | |
| 94 | } |