|
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\Resource; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* FileResource represents a resource stored on the filesystem. |
|
18
|
|
|
* |
|
19
|
|
|
* The resource can be a file or a directory. |
|
20
|
|
|
*/ |
|
21
|
|
|
class FileResource implements ResourceInterface |
|
22
|
|
|
{ |
|
23
|
|
|
protected $resource; |
|
24
|
|
|
protected $configuration; |
|
25
|
|
|
protected $alias; |
|
26
|
|
|
protected $restore; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $resource |
|
32
|
|
|
* @param ConfigurationInterface $configuration |
|
33
|
|
|
* @param string $alias |
|
34
|
|
|
* @param bool $restore |
|
35
|
|
|
*/ |
|
36
|
49 |
|
public function __construct($resource, ConfigurationInterface $configuration = null, $alias = null, $restore = false) |
|
37
|
|
|
{ |
|
38
|
49 |
|
$this->resource = $resource; |
|
39
|
49 |
|
$this->configuration = $configuration; |
|
40
|
49 |
|
$this->alias = $alias; |
|
41
|
49 |
|
$this->restore = $restore; |
|
42
|
49 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Creates a FileResource. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $resource |
|
48
|
|
|
* @param ConfigurationInterface $configuration |
|
49
|
|
|
* @param string $alias |
|
50
|
|
|
* @param bool $restore |
|
51
|
|
|
* |
|
52
|
|
|
* @return FileResource |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public static function create($resource, ConfigurationInterface $configuration = null, $alias = null, $restore = false) |
|
55
|
|
|
{ |
|
56
|
1 |
|
return new static($resource, $configuration, $alias, $restore); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Gets configuration. |
|
61
|
|
|
* |
|
62
|
|
|
* @return ConfigurationInterface |
|
63
|
|
|
*/ |
|
64
|
44 |
|
public function getConfiguration() |
|
65
|
|
|
{ |
|
66
|
44 |
|
return $this->configuration; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets configuration. |
|
71
|
|
|
* |
|
72
|
|
|
* @param ConfigurationInterface $configuration |
|
73
|
|
|
* |
|
74
|
|
|
* @return FileResource |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function setConfiguration(ConfigurationInterface $configuration) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->configuration = $configuration; |
|
79
|
|
|
|
|
80
|
1 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the resource tied to this Resource. |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed The resource |
|
87
|
|
|
*/ |
|
88
|
49 |
|
public function getResource() |
|
89
|
|
|
{ |
|
90
|
49 |
|
return $this->resource; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Sets the resource. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $resource |
|
97
|
|
|
* |
|
98
|
|
|
* @return FileResource |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function setResource($resource) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->resource = $resource; |
|
103
|
|
|
|
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Gets an alias (as ConfigCache service name suffix). |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
18 |
|
public function getAlias() |
|
113
|
|
|
{ |
|
114
|
18 |
|
return $this->alias; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Sets an alias (as ConfigCache service name suffix). |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $alias |
|
121
|
|
|
* |
|
122
|
|
|
* @return FileResource |
|
123
|
|
|
* |
|
124
|
|
|
* @throws \InvalidArgumentException |
|
125
|
|
|
*/ |
|
126
|
6 |
|
public function setAlias($alias) |
|
127
|
|
|
{ |
|
128
|
6 |
|
if (!is_string($alias)) { |
|
129
|
2 |
|
throw new \InvalidArgumentException("Alias[{$alias}] must be string."); |
|
130
|
|
|
} |
|
131
|
4 |
|
$this->alias = $alias; |
|
132
|
|
|
|
|
133
|
4 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Whether FileResource has an alias or not. |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
32 |
|
public function hasAlias() |
|
142
|
|
|
{ |
|
143
|
32 |
|
return !is_null($this->alias) && $this->alias !== ''; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Whether this resources is restorable or not. |
|
148
|
|
|
* |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
16 |
|
public function isRestorable() |
|
152
|
|
|
{ |
|
153
|
16 |
|
return $this->restore ? true : false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Sets restorable. |
|
158
|
|
|
* |
|
159
|
|
|
* @param bool $restore |
|
160
|
|
|
* |
|
161
|
|
|
* @return FileResource |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function setRestorable($restore) |
|
164
|
|
|
{ |
|
165
|
1 |
|
$this->restore = $restore; |
|
166
|
|
|
|
|
167
|
1 |
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
26 |
|
public function exists() |
|
174
|
|
|
{ |
|
175
|
26 |
|
return file_exists($this->resource); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|