|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * This file is part of Phpfastcache. |
| 5 | + * |
| 6 | + * @license MIT License (MIT) |
| 7 | + * |
| 8 | + * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. |
| 9 | + * |
| 10 | + * @author Georges.L (Geolim4) <contact@geolim4.com> |
| 11 | + * @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace Phpfastcache\Drivers\Solr; |
| 17 | + |
| 18 | +use Phpfastcache\Config\ConfigurationOption; |
| 19 | +use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
| 20 | +use Phpfastcache\Core\Pool\TaggableCacheItemPoolInterface; |
| 21 | +use Phpfastcache\EventManager; |
| 22 | +use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
| 23 | +use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
| 24 | +use Psr\EventDispatcher\EventDispatcherInterface; |
| 25 | + |
| 26 | +class Config extends ConfigurationOption |
| 27 | +{ |
| 28 | + protected const DEFAULT_MAPPING_SCHEMA = [ |
| 29 | + ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX . '_s', |
| 30 | + ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX . '_s', |
| 31 | + ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX . '_ss', |
| 32 | + ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX . '_ss', |
| 33 | + ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX => ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX . '_ss', |
| 34 | + TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX . '_ss', |
| 35 | + ]; |
| 36 | + |
| 37 | + protected string $host = '127.0.0.1'; |
| 38 | + |
| 39 | + protected int $port = 8983; |
| 40 | + |
| 41 | + protected string $coreName = ''; |
| 42 | + |
| 43 | + protected string $scheme = 'http'; |
| 44 | + |
| 45 | + protected string $path = '/'; // Override of ConfigurationOption |
| 46 | + |
| 47 | + protected array $mappingSchema = self::DEFAULT_MAPPING_SCHEMA; |
| 48 | + |
| 49 | + private EventDispatcherInterface $eventDispatcher; |
| 50 | + |
| 51 | + public function __construct(array $parameters = []) |
| 52 | + { |
| 53 | + $this->eventDispatcher = $this->getDefaultEventDispatcher(); |
| 54 | + parent::__construct($parameters); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return string |
| 59 | + */ |
| 60 | + public function getHost(): string |
| 61 | + { |
| 62 | + return $this->host; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param string $host |
| 67 | + * @return Config |
| 68 | + * @throws PhpfastcacheLogicException |
| 69 | + */ |
| 70 | + public function setHost(string $host): Config |
| 71 | + { |
| 72 | + $this->enforceLockedProperty(__FUNCTION__); |
| 73 | + $this->host = $host; |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return int |
| 79 | + */ |
| 80 | + public function getPort(): int |
| 81 | + { |
| 82 | + return $this->port; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param int $port |
| 87 | + * @return Config |
| 88 | + * @throws PhpfastcacheLogicException |
| 89 | + */ |
| 90 | + public function setPort(int $port): Config |
| 91 | + { |
| 92 | + $this->enforceLockedProperty(__FUNCTION__); |
| 93 | + $this->port = $port; |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return string |
| 99 | + */ |
| 100 | + public function getCoreName(): string |
| 101 | + { |
| 102 | + return $this->coreName; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param string $coreName |
| 107 | + * @return Config |
| 108 | + * @throws PhpfastcacheLogicException |
| 109 | + */ |
| 110 | + public function setCoreName(string $coreName): Config |
| 111 | + { |
| 112 | + $this->enforceLockedProperty(__FUNCTION__); |
| 113 | + $this->coreName = $coreName; |
| 114 | + return $this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return string |
| 119 | + */ |
| 120 | + public function getScheme(): string |
| 121 | + { |
| 122 | + return $this->scheme; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param string $scheme |
| 127 | + * @return Config |
| 128 | + * @throws PhpfastcacheLogicException |
| 129 | + */ |
| 130 | + public function setScheme(string $scheme): Config |
| 131 | + { |
| 132 | + $this->enforceLockedProperty(__FUNCTION__); |
| 133 | + $this->scheme = $scheme; |
| 134 | + return $this; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @return EventDispatcherInterface |
| 139 | + */ |
| 140 | + public function getEventDispatcher(): EventDispatcherInterface |
| 141 | + { |
| 142 | + return $this->eventDispatcher; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @return EventDispatcherInterface |
| 147 | + */ |
| 148 | + protected function getDefaultEventDispatcher(): EventDispatcherInterface |
| 149 | + { |
| 150 | + return new class implements EventDispatcherInterface { |
| 151 | + public function dispatch(object $event): object |
| 152 | + { |
| 153 | + return $event; |
| 154 | + } |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @param EventDispatcherInterface $eventDispatcher |
| 160 | + * @return Config |
| 161 | + * @throws PhpfastcacheLogicException |
| 162 | + */ |
| 163 | + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): Config |
| 164 | + { |
| 165 | + $this->enforceLockedProperty(__FUNCTION__); |
| 166 | + $this->eventDispatcher = $eventDispatcher; |
| 167 | + return $this; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @return string[] |
| 172 | + */ |
| 173 | + public function getMappingSchema(): array |
| 174 | + { |
| 175 | + return $this->mappingSchema; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @param string[] $mappingSchema |
| 180 | + * @return Config |
| 181 | + * @throws PhpfastcacheLogicException |
| 182 | + * @throws PhpfastcacheInvalidArgumentException |
| 183 | + */ |
| 184 | + public function setMappingSchema(array $mappingSchema): Config |
| 185 | + { |
| 186 | + $this->enforceLockedProperty(__FUNCTION__); |
| 187 | + |
| 188 | + $diff = array_diff(array_keys(self::DEFAULT_MAPPING_SCHEMA), array_keys($mappingSchema)); |
| 189 | + |
| 190 | + if ($diff) { |
| 191 | + throw new PhpfastcacheInvalidArgumentException('Missing keys for the solr mapping schema: ' . \implode(', ', $diff)); |
| 192 | + } |
| 193 | + |
| 194 | + $this->mappingSchema = $mappingSchema; |
| 195 | + return $this; |
| 196 | + } |
| 197 | +} |
0 commit comments