|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP-Typography. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2017 Peter Putzer. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License along |
|
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20
|
|
|
* |
|
21
|
|
|
* *** |
|
22
|
|
|
* |
|
23
|
|
|
* @package mundschenk-at/php-typography |
|
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace PHP_Typography\Hyphenator; |
|
28
|
|
|
|
|
29
|
|
|
use PHP_Typography\Hyphenator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Per-language cache of Hyphenator instances. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 5.2.0 |
|
35
|
|
|
* |
|
36
|
|
|
* @author Peter Putzer <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class Cache { |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* An array of Hyphenator instances indexed by language. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $cache = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* A flag that indicated that the cache has changed since creation/deserialization. |
|
49
|
|
|
* |
|
50
|
|
|
* @var bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $changed = false; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Ignore the "changed" flag during serialization. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __sleep() { |
|
60
|
|
|
return [ |
|
61
|
|
|
'cache', |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Caches a Hyphenator instance. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $lang A language code. |
|
69
|
|
|
* @param Hyphenator $hyphenator The object to cache. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function set_hyphenator( $lang, Hyphenator $hyphenator ) { |
|
72
|
|
|
$this->cache[ $lang ] = $hyphenator; |
|
73
|
|
|
$this->changed = true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Retrieves a cached Hyphenator. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $lang A language code. |
|
80
|
|
|
* |
|
81
|
|
|
* @return Hyphenator|null |
|
82
|
|
|
*/ |
|
83
|
|
|
public function get_hyphenator( $lang ) { |
|
84
|
|
|
if ( isset( $this->cache[ $lang ] ) ) { |
|
85
|
|
|
return $this->cache[ $lang ]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Determines whether the cache (not its content) has been modified since |
|
93
|
|
|
* instance creatino (or deserialization). |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function has_changed() { |
|
98
|
|
|
return $this->changed; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|