1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
namespace Eccube\Doctrine\EventSubscriber; |
26
|
|
|
|
27
|
|
|
use Doctrine\Common\EventSubscriber; |
28
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
29
|
|
|
use Doctrine\ORM\Events; |
30
|
|
|
use Eccube\Application; |
31
|
|
|
|
32
|
|
|
class ClearCacheEventSubscriber implements EventSubscriber |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array 対象のエンティティクラス名(FQDN) |
36
|
|
|
*/ |
37
|
|
|
private $classes; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Application |
41
|
|
|
*/ |
42
|
|
|
private $app; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Application $app |
46
|
|
|
*/ |
47
|
1189 |
|
public function __construct(Application $app) |
48
|
|
|
{ |
49
|
1189 |
|
$this->app = $app; |
50
|
|
|
|
51
|
1189 |
|
$this->classes = array( |
52
|
|
|
'Eccube\Entity\BaseInfo', |
53
|
|
|
'Eccube\Entity\Category', |
54
|
|
|
'Eccube\Entity\PageLayout', |
55
|
|
|
'Eccube\Entity\Block', |
56
|
|
|
'Eccube\Entity\BlockPosition', |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
1189 |
|
public function getSubscribedEvents() |
61
|
|
|
{ |
62
|
1189 |
|
$options = $this->app['config']['doctrine_cache']; |
63
|
1189 |
|
$clearCache = $options['result_cache']['clear_cache']; |
64
|
|
|
|
65
|
1189 |
|
if ($clearCache) { |
66
|
|
|
return array( |
67
|
1189 |
|
Events::postPersist, |
68
|
|
|
Events::postRemove, |
69
|
|
|
Events::postUpdate, |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
// キャッシュの削除オプションが無効の場合は、イベントを定義せず空の配列を返す. |
73
|
|
|
return array(); |
74
|
|
|
} |
75
|
|
|
|
76
|
760 |
|
public function postPersist(LifecycleEventArgs $args) |
77
|
|
|
{ |
78
|
760 |
|
$this->app['monolog']->debug('clear result cache: postPersist'); |
79
|
760 |
|
$this->clearCache($args); |
80
|
|
|
} |
81
|
|
|
|
82
|
137 |
|
public function postRemove(LifecycleEventArgs $args) |
83
|
|
|
{ |
84
|
137 |
|
$this->app['monolog']->debug('clear result cache: postRemove'); |
85
|
137 |
|
$this->clearCache($args); |
86
|
|
|
} |
87
|
|
|
|
88
|
514 |
|
public function postUpdate(LifecycleEventArgs $args) |
89
|
|
|
{ |
90
|
514 |
|
$this->app['monolog']->debug('clear result cache: postUpdate'); |
91
|
514 |
|
$this->clearCache($args); |
92
|
|
|
} |
93
|
|
|
|
94
|
782 |
|
protected function clearCache(LifecycleEventArgs $args) |
95
|
|
|
{ |
96
|
782 |
|
$entity = $args->getObject(); |
97
|
782 |
|
$classes = $this->classes; |
98
|
782 |
|
foreach ($classes as $class) { |
99
|
782 |
|
if ($entity instanceof $class) { |
100
|
145 |
|
$this->app['monolog']->debug('clear result cache: '.$class); |
101
|
145 |
|
$cache = $args->getObjectManager()->getConfiguration()->getResultCacheImpl(); |
|
|
|
|
102
|
145 |
|
$cache->deleteAll(); |
103
|
782 |
|
break; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: