| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Concurrency; |
|
|
| use Piwik\Common; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Option; |
| use Piwik\Log\LoggerInterface; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| class DistributedList |
| { |
| |
| |
| |
| |
| |
| private $optionName; |
|
|
| |
| |
| |
| private $logger; |
|
|
| |
| |
| |
| public function __construct($optionName, ?LoggerInterface $logger = null) |
| { |
| $this->optionName = $optionName; |
| $this->logger = $logger ?: StaticContainer::get(LoggerInterface::class); |
| } |
|
|
| |
| |
| |
| |
| |
| public function getAll() |
| { |
| $result = $this->getListOptionValue(); |
|
|
| foreach ($result as $key => $item) { |
| |
| if (is_array($item)) { |
| $this->logger->info("Found array item in DistributedList option value '{name}': {data}", array( |
| 'name' => $this->optionName, |
| 'data' => var_export($result, true), |
| )); |
|
|
| unset($result[$key]); |
| } |
| } |
|
|
| return $result; |
| } |
|
|
| |
| |
| |
| |
| |
| public function setAll($items) |
| { |
| foreach ($items as $key => &$item) { |
| if (is_array($item)) { |
| throw new \InvalidArgumentException("Array item encountered in DistributedList::setAll() [ key = $key ]."); |
| } else { |
| $item = (string)$item; |
| } |
| } |
|
|
| Option::set($this->optionName, serialize($items)); |
| } |
|
|
| |
| |
| |
| |
| |
| public function add($item) |
| { |
| $allItems = $this->getAll(); |
| if (is_array($item)) { |
| $allItems = array_merge($allItems, $item); |
| } else { |
| $allItems[] = $item; |
| } |
|
|
| $this->setAll($allItems); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function remove($items) |
| { |
| if (!is_array($items)) { |
| $items = array($items); |
| } |
|
|
| $allItems = $this->getAll(); |
|
|
| foreach ($items as $item) { |
| $existingIndex = array_search($item, $allItems); |
| if ($existingIndex === false) { |
| return; |
| } |
|
|
| unset($allItems[$existingIndex]); |
| } |
|
|
| $this->setAll(array_values($allItems)); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function removeByIndex($indices) |
| { |
| if (!is_array($indices)) { |
| $indices = array($indices); |
| } |
|
|
| $indices = array_unique($indices); |
|
|
| $allItems = $this->getAll(); |
| foreach ($indices as $index) { |
| unset($allItems[$index]); |
| } |
|
|
| $this->setAll(array_values($allItems)); |
| } |
|
|
| protected function getListOptionValue() |
| { |
| Option::clearCachedOption($this->optionName); |
| $array = Option::get($this->optionName); |
|
|
| $result = array(); |
| if ( |
| $array |
| && ($array = Common::safe_unserialize($array)) |
| && count($array) |
| ) { |
| $result = $array; |
| } |
| return $result; |
| } |
| } |
|
|