|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Application Utils |
|
4
|
|
|
* @subpackage FileHelper |
|
5
|
|
|
* @see \AppUtils\FileHelper\SerializedFile |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace AppUtils\FileHelper; |
|
11
|
|
|
|
|
12
|
|
|
use AppUtils\FileHelper; |
|
13
|
|
|
use AppUtils\FileHelper_Exception; |
|
14
|
|
|
use SplFileInfo; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Handles files containing data serialized with the |
|
18
|
|
|
* PHP function {@see serialize()}. |
|
19
|
|
|
* |
|
20
|
|
|
* Create an instance with {@see SerializedFile::factory()}. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Application Utils |
|
23
|
|
|
* @subpackage FileHelper |
|
24
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class SerializedFile extends FileInfo |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @param string|PathInfoInterface|SplFileInfo $path |
|
30
|
|
|
* @return SerializedFile |
|
31
|
|
|
* @throws FileHelper_Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function factory($path) : SerializedFile |
|
34
|
|
|
{ |
|
35
|
|
|
if($path instanceof self) { |
|
36
|
|
|
return $path; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$instance = self::createInstance($path); |
|
40
|
|
|
|
|
41
|
|
|
if($instance instanceof self) { |
|
42
|
|
|
return $instance; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
throw new FileHelper_Exception( |
|
46
|
|
|
'Invalid class.' |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Opens a serialized file and returns the unserialized data. |
|
52
|
|
|
* Only supports serialized arrays - classes are not allowed. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws FileHelper_Exception |
|
55
|
|
|
* @return array<int|string,mixed> |
|
56
|
|
|
* @see FileHelper::parseSerializedFile() |
|
57
|
|
|
* |
|
58
|
|
|
* @see FileHelper::ERROR_FILE_DOES_NOT_EXIST |
|
59
|
|
|
* @see FileHelper::ERROR_SERIALIZED_FILE_CANNOT_BE_READ |
|
60
|
|
|
* @see FileHelper::ERROR_SERIALIZED_FILE_UNSERIALZE_FAILED |
|
61
|
|
|
*/ |
|
62
|
|
|
public function parse() : array |
|
63
|
|
|
{ |
|
64
|
|
|
$contents = $this |
|
65
|
|
|
->requireExists() |
|
66
|
|
|
->requireReadable() |
|
67
|
|
|
->getContents(); |
|
68
|
|
|
|
|
69
|
|
|
$result = @unserialize( |
|
70
|
|
|
$contents, |
|
71
|
|
|
array( |
|
72
|
|
|
'allowed_classes' => false |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
if($result !== false) { |
|
77
|
|
|
return $result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
throw new FileHelper_Exception( |
|
81
|
|
|
'Cannot unserialize the file contents.', |
|
82
|
|
|
sprintf( |
|
83
|
|
|
'Tried unserializing the data from file at [%s].', |
|
84
|
|
|
$this->getPath() |
|
85
|
|
|
), |
|
86
|
|
|
FileHelper::ERROR_SERIALIZED_FILE_UNSERIALZE_FAILED |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Saves the data serialized to the file. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array<mixed> $data |
|
94
|
|
|
* @return $this |
|
95
|
|
|
* @throws FileHelper_Exception |
|
96
|
|
|
*/ |
|
97
|
|
|
public function putData(array $data) : self |
|
98
|
|
|
{ |
|
99
|
|
|
$serialized = @serialize($data); |
|
100
|
|
|
|
|
101
|
|
|
return $this->putContents($serialized); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|