ISGArchiveDelegate.php
3 years ago
SGBGArchive.php
3 years ago
SGBGArchiveCdr.php
3 years ago
SGBGArchiveHelper.php
3 years ago
SGBGCache.php
3 years ago
SGBGCacheableFile.php
3 years ago
SGBGDirectoryTreeFile.php
3 years ago
SGBGFile.php
3 years ago
SGBGFileHelper.php
3 years ago
SGBGJsonFile.php
3 years ago
SGBGLog.php
3 years ago
SGBGReloader.php
3 years ago
SGBGStateFile.php
3 years ago
SGBGTask.php
3 years ago
SGBGCacheableFile.php
65 lines
| 1 | <?php |
| 2 | /* |
| 3 | @ class CacheableFile |
| 4 | @ version 1.0.0 |
| 5 | @ updated 23/01/2021 |
| 6 | */ |
| 7 | |
| 8 | require_once(__DIR__.'/SGBGFile.php'); |
| 9 | require_once(__DIR__.'/SGBGCache.php'); |
| 10 | |
| 11 | class SGBGCacheableFile extends SGBGFile |
| 12 | { |
| 13 | private $cache = null; |
| 14 | |
| 15 | /** |
| 16 | * |
| 17 | * Constructor. |
| 18 | * |
| 19 | * @param string $path absolute file path |
| 20 | * @return null |
| 21 | */ |
| 22 | public function __construct($path) |
| 23 | { |
| 24 | parent::__construct($path); |
| 25 | |
| 26 | $this->cache = new SGBGCache(array($this, 'writeToFile')); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * Get Cache object. |
| 32 | * |
| 33 | * @return Cache instance of Cache class |
| 34 | */ |
| 35 | public function getCache() |
| 36 | { |
| 37 | return $this->cache; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * Callback called when Cache flushes. |
| 43 | * |
| 44 | * @param string $data data being flushed |
| 45 | * @return null |
| 46 | */ |
| 47 | public function writeToFile($data) |
| 48 | { |
| 49 | parent::write($data); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * |
| 54 | * Override parent write function to cache instead of directly writing to file. |
| 55 | * |
| 56 | * @param string $data data to write to file |
| 57 | * @return int the number of bytes cached or written to file |
| 58 | */ |
| 59 | public function write($data) |
| 60 | { |
| 61 | $this->getCache()->cache($data); |
| 62 | return strlen($data); |
| 63 | } |
| 64 | } |
| 65 |