| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- class modalConsoleHistory
- {
- /** @var xPDOCacheManager */
- public $cacheManager;
- /** @var array */
- public $items = [];
- /** @var string */
- protected $userFolder;
- /** @var integer */
- protected $limit;
- public function __construct($cacheManager, array $config)
- {
- $this->cacheManager = $cacheManager;
- $this->userFolder = $config['userFolder'];
- $this->limit = $config['limit'];
- $this->loadHistory();
- }
- public function loadHistory()
- {
- $this->items = $this->cacheManager->get('history', array(xPDO::OPT_CACHE_KEY => $this->userFolder));
- $this->items = is_null($this->items) ? [] : $this->items;
- return $this;
- }
- public function getItems()
- {
- return $this->items;
- }
- public function getItem($key, $default = '')
- {
- return isset($this->items[$key]) ? $this->items[$key] : $default;
- }
- public function addItem($key, $code)
- {
- $this->deleteItem($key);
- $this->items[$key] = $code;
- if ($this->count() > $this->limit) {
- $diff = $this->count() - $this->limit;
- $this->items = array_slice($this->items, $diff);
- }
- return $this;
- }
- public function deleteItem($key)
- {
- if ($this->hasItem($key)) {
- unset($this->items[$key]);
- }
- return $this;
- }
- public function hasItem($key)
- {
- return isset($this->items[$key]);
- }
- public function getLastItem($default = '')
- {
- end($this->items);
- return $this->getItem(key($this->items), $default);
- }
- public function getKeys()
- {
- return array_keys($this->items);
- }
- public function isEmpty()
- {
- return empty($this->items);
- }
- public function count()
- {
- return count($this->items);
- }
- public function save($force = false)
- {
- if ($this->limit > 0 || $force) {
- $options = array(
- xPDO::OPT_CACHE_KEY => $this->getUserFolder(),
- );
- $items = $this->getItems();
- return $this->getCacheManager()->set('history', $items, 0, $options);
- }
- return false;
- }
- public function clear()
- {
- $options = array(
- xPDO::OPT_CACHE_KEY => $this->getUserFolder(),
- );
- $this->items = [];
- return $this->getCacheManager()->set('history', $this->items, 0, $options);
- }
- public function getUserFolder()
- {
- return $this->userFolder;
- }
- public function getCacheManager()
- {
- return $this->cacheManager;
- }
- }
|