1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 zepi |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
* THE SOFTWARE. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The FileObjectBackend saves and loads an object from a file |
29
|
|
|
* |
30
|
|
|
* @package Zepi\Turbo\Backend |
31
|
|
|
* @author Matthias Zobrist <[email protected]> |
32
|
|
|
* @copyright Copyright (c) 2015 zepi |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
namespace Zepi\Turbo\Backend; |
36
|
|
|
|
37
|
|
|
use \Zepi\Turbo\Exception; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The FileObjectBackend saves and loads an object from a file |
41
|
|
|
* |
42
|
|
|
* @author Matthias Zobrist <[email protected]> |
43
|
|
|
* @copyright Copyright (c) 2015 zepi |
44
|
|
|
*/ |
45
|
|
|
class FileObjectBackend extends ObjectBackendAbstract |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @access protected |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $path; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructs the object |
55
|
|
|
* |
56
|
|
|
* @access public |
57
|
|
|
* @param string $path |
58
|
|
|
*/ |
59
|
46 |
|
public function __construct($path) |
60
|
|
|
{ |
61
|
46 |
|
$this->path = $path; |
62
|
46 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Saves the object in the data source |
66
|
|
|
* |
67
|
|
|
* @access protected |
68
|
|
|
* @param string $serializedObject |
69
|
|
|
* @return integer |
70
|
|
|
* |
71
|
|
|
* @throws Zepi\Turbo\Exception The file "$path" isn't writable! |
72
|
|
|
*/ |
73
|
7 |
|
protected function saveSerializedObject($serializedObject) |
74
|
|
|
{ |
75
|
7 |
|
if (file_exists($this->path) && !is_writable($this->path)) { |
76
|
1 |
|
throw new Exception('The file "' . $this->path . '" isn\'t writable!'); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
return file_put_contents($this->path, $serializedObject); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Loads the object from the data source |
84
|
|
|
* |
85
|
|
|
* @access protected |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
19 |
|
protected function loadSerializedObject() |
89
|
|
|
{ |
90
|
19 |
|
if (!file_exists($this->path)) { |
91
|
17 |
|
return ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return file_get_contents($this->path); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|