1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Reader\XLSX\Manager\SharedStringsCaching; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Reader\Exception\SharedStringNotFoundException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class InMemoryStrategy |
9
|
|
|
* |
10
|
|
|
* This class implements the in-memory caching strategy for shared strings. |
11
|
|
|
* This strategy is used when the number of unique strings is low, compared to the memory available. |
12
|
|
|
* |
13
|
|
|
* @package Box\Spout\Reader\XLSX\Manager\SharedStringsCaching |
14
|
|
|
*/ |
15
|
|
|
class InMemoryStrategy implements CachingStrategyInterface |
16
|
|
|
{ |
17
|
|
|
/** @var \SplFixedArray Array used to cache the shared strings */ |
18
|
|
|
protected $inMemoryCache; |
19
|
|
|
|
20
|
|
|
/** @var bool Whether the cache has been closed */ |
21
|
|
|
protected $isCacheClosed; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param int $sharedStringsUniqueCount Number of unique shared strings |
25
|
|
|
*/ |
26
|
33 |
|
public function __construct($sharedStringsUniqueCount) |
27
|
|
|
{ |
28
|
33 |
|
$this->inMemoryCache = new \SplFixedArray($sharedStringsUniqueCount); |
29
|
33 |
|
$this->isCacheClosed = false; |
30
|
33 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Adds the given string to the cache. |
34
|
|
|
* |
35
|
|
|
* @param string $sharedString The string to be added to the cache |
36
|
|
|
* @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
22 |
|
public function addStringForIndex($sharedString, $sharedStringIndex) |
40
|
|
|
{ |
41
|
22 |
|
if (!$this->isCacheClosed) { |
42
|
22 |
|
$this->inMemoryCache->offsetSet($sharedStringIndex, $sharedString); |
43
|
|
|
} |
44
|
22 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Closes the cache after the last shared string was added. |
48
|
|
|
* This prevents any additional string from being added to the cache. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
31 |
|
public function closeCache() |
53
|
|
|
{ |
54
|
31 |
|
$this->isCacheClosed = true; |
55
|
31 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the string located at the given index from the cache. |
59
|
|
|
* |
60
|
|
|
* @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file |
61
|
|
|
* @return string The shared string at the given index |
62
|
|
|
* @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index |
63
|
|
|
*/ |
64
|
22 |
|
public function getStringAtIndex($sharedStringIndex) |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
22 |
|
return $this->inMemoryCache->offsetGet($sharedStringIndex); |
68
|
1 |
|
} catch (\RuntimeException $e) { |
69
|
1 |
|
throw new SharedStringNotFoundException("Shared string not found for index: $sharedStringIndex"); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Destroys the cache, freeing memory and removing any created artifacts |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
31 |
|
public function clearCache() |
79
|
|
|
{ |
80
|
31 |
|
unset($this->inMemoryCache); |
81
|
31 |
|
$this->isCacheClosed = false; |
82
|
31 |
|
} |
83
|
|
|
} |
84
|
|
|
|