|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the LanguageHandler class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\Persistence\Cache; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\Handler as ContentLanguageHandlerInterface; |
|
12
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language; |
|
13
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\Language\Handler |
|
17
|
|
|
*/ |
|
18
|
|
|
class ContentLanguageHandler extends AbstractInMemoryHandler implements ContentLanguageHandlerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var callable */ |
|
21
|
|
|
private $getTags; |
|
22
|
|
|
|
|
23
|
|
|
/** @var callable */ |
|
24
|
|
|
private $getKeys; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Set callback functions for use in cache retrival. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function init(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->getTags = static function (Language $language) { return ['language-' . $language->id]; }; |
|
32
|
|
|
$this->getKeys = static function (Language $language) { |
|
33
|
|
|
return [ |
|
34
|
|
|
'ez-language-' . $language->id, |
|
35
|
|
|
'ez-language-code-' . $language->languageCode, |
|
36
|
|
|
]; |
|
37
|
|
|
}; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function create(CreateStruct $struct) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->logger->logCall(__METHOD__, array('struct' => $struct)); |
|
46
|
|
|
$this->deleteCache(['ez-language-list']); |
|
47
|
|
|
|
|
48
|
|
|
return $this->persistenceHandler->contentLanguageHandler()->create($struct); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function update(Language $struct) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->logger->logCall(__METHOD__, array('struct' => $struct)); |
|
57
|
|
|
$return = $this->persistenceHandler->contentLanguageHandler()->update($struct); |
|
58
|
|
|
|
|
59
|
|
|
$this->deleteCache([ |
|
60
|
|
|
'ez-language-list', |
|
61
|
|
|
'ez-language-' . $struct->id, |
|
62
|
|
|
'ez-language-code-' . $struct->languageCode, |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
return $return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function load($id) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->getCacheValue( |
|
74
|
|
|
$id, |
|
75
|
|
|
'ez-language-', |
|
76
|
|
|
function ($id) { |
|
77
|
|
|
return $this->persistenceHandler->contentLanguageHandler()->load($id); |
|
78
|
|
|
}, |
|
79
|
|
|
$this->getTags, |
|
80
|
|
|
$this->getKeys |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
public function loadList(array $ids): iterable |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->getMultipleCacheValues( |
|
|
|
|
|
|
90
|
|
|
$ids, |
|
91
|
|
|
'ez-language-', |
|
92
|
|
|
function (array $ids) { |
|
93
|
|
|
return $this->persistenceHandler->contentLanguageHandler()->loadList($ids); |
|
94
|
|
|
}, |
|
95
|
|
|
$this->getTags, |
|
96
|
|
|
$this->getKeys |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
public function loadByLanguageCode($languageCode) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->getCacheValue( |
|
106
|
|
|
$languageCode, |
|
107
|
|
|
'ez-language-code-', |
|
108
|
|
|
function ($languageCode) { |
|
109
|
|
|
return $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode); |
|
110
|
|
|
}, |
|
111
|
|
|
$this->getTags, |
|
112
|
|
|
$this->getKeys |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function loadListByLanguageCodes(array $languageCodes): iterable |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->getMultipleCacheValues( |
|
|
|
|
|
|
122
|
|
|
$languageCodes, |
|
123
|
|
|
'ez-language-code-', |
|
124
|
|
|
function (array $languageCodes) { |
|
125
|
|
|
return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($languageCodes); |
|
126
|
|
|
}, |
|
127
|
|
|
$this->getTags, |
|
128
|
|
|
$this->getKeys |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function loadAll() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->getListCacheValue( |
|
138
|
|
|
'ez-language-list', |
|
139
|
|
|
function () { |
|
140
|
|
|
return $this->persistenceHandler->contentLanguageHandler()->loadAll(); |
|
141
|
|
|
}, |
|
142
|
|
|
$this->getTags, |
|
143
|
|
|
$this->getKeys |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
View Code Duplication |
public function delete($id) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->logger->logCall(__METHOD__, array('language' => $id)); |
|
153
|
|
|
$return = $this->persistenceHandler->contentLanguageHandler()->delete($id); |
|
154
|
|
|
|
|
155
|
|
|
// As we don't have locale we clear cache by tag invalidation |
|
156
|
|
|
$this->invalidateCache(['language-' . $id]); |
|
157
|
|
|
|
|
158
|
|
|
return $return; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.