1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ConfigCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 Yahoo Japan Corporation |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace YahooJapan\ConfigCacheBundle\ConfigCache; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\PhpFileCache; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* RestorablePhpFileCache restores by re-creating the caches to the temporary directory or cache directory. |
18
|
|
|
*/ |
19
|
|
|
class RestorablePhpFileCache extends PhpFileCache |
20
|
|
|
{ |
21
|
|
|
const TAG_RESTORABLE_CACHE = 'config_cache.restorable'; |
22
|
|
|
|
23
|
|
|
protected $builder; |
24
|
|
|
protected $restoringDirectory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Saves the cache to the temporary directory. |
28
|
|
|
* |
29
|
|
|
* @param string $id |
30
|
|
|
*/ |
31
|
7 |
View Code Duplication |
public function saveToTemp($id) |
|
|
|
|
32
|
|
|
{ |
33
|
7 |
|
if ($this->contains($id)) { |
34
|
6 |
|
$data = $this->fetch($id); |
35
|
6 |
|
$this->prepareTemporaryDirectory(); |
36
|
6 |
|
$this->save($id, $data); |
37
|
6 |
|
$this->restoreDirectory(); |
38
|
6 |
|
} |
39
|
7 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Restores the cache to the cache directory. |
43
|
|
|
* |
44
|
|
|
* @param string $id |
45
|
|
|
*/ |
46
|
4 |
View Code Duplication |
public function restore($id) |
|
|
|
|
47
|
|
|
{ |
48
|
4 |
|
$this->prepareTemporaryDirectory(); |
49
|
4 |
|
if ($this->contains($id)) { |
50
|
3 |
|
$data = $this->fetch($id); |
51
|
3 |
|
$this->restoreDirectory(); |
52
|
3 |
|
$this->save($id, $data); |
53
|
3 |
|
} else { |
54
|
1 |
|
$this->restoreDirectory(); |
55
|
|
|
} |
56
|
4 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Sets a SaveAreaBuilder. |
60
|
|
|
* |
61
|
|
|
* @param SaveAreaBuilder $builder |
62
|
|
|
* |
63
|
|
|
* @return RestorablePhpFileCache |
64
|
|
|
*/ |
65
|
13 |
|
public function setBuilder(SaveAreaBuilder $builder) |
66
|
|
|
{ |
67
|
13 |
|
$this->builder = $builder; |
68
|
|
|
|
69
|
13 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets a directory. |
74
|
|
|
* |
75
|
|
|
* @param string $directory |
76
|
|
|
* |
77
|
|
|
* @return RestorablePhpFileCache |
78
|
|
|
*/ |
79
|
11 |
|
protected function setDirectory($directory) |
80
|
|
|
{ |
81
|
11 |
|
$this->directory = realpath($directory); |
82
|
|
|
|
83
|
11 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets an original restoring directory. |
88
|
|
|
* |
89
|
|
|
* @param string $directory |
90
|
|
|
* |
91
|
|
|
* @return RestorablePhpFileCache |
92
|
|
|
*/ |
93
|
10 |
|
protected function setRestoringDirectory($directory) |
94
|
|
|
{ |
95
|
10 |
|
$this->restoringDirectory = $directory; |
96
|
|
|
|
97
|
10 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Prepares temporary directory to re-create cache. |
102
|
|
|
* |
103
|
|
|
* @return RestorablePhpFileCache |
104
|
|
|
*/ |
105
|
8 |
|
protected function prepareTemporaryDirectory() |
106
|
|
|
{ |
107
|
8 |
|
$currentDirectory = $this->getDirectory(); |
108
|
8 |
|
$this->setRestoringDirectory($currentDirectory); |
109
|
|
|
|
110
|
|
|
// mkdir before setDirectory() |
111
|
8 |
|
$temporaryDirectory = $this->builder->build($currentDirectory); |
112
|
|
|
|
113
|
8 |
|
return $this->setDirectory($temporaryDirectory); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Restores an original directory. |
118
|
|
|
* |
119
|
|
|
* @return RestorablePhpFileCache |
120
|
|
|
*/ |
121
|
8 |
|
protected function restoreDirectory() |
122
|
|
|
{ |
123
|
8 |
|
return $this->setDirectory($this->restoringDirectory); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.