1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixMigrations\Storages; |
4
|
|
|
|
5
|
|
|
use Arrilot\BitrixMigrations\Helpers; |
6
|
|
|
use Arrilot\BitrixMigrations\Interfaces\FileStorageInterface; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
class FileStorage implements FileStorageInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get all of the migration files in a given path. |
13
|
|
|
* |
14
|
|
|
* @param string $path |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
|
|
public function getMigrationFiles($path) |
19
|
|
|
{ |
20
|
|
|
$files = Helpers::rGlob($path.'/*_*.php'); |
21
|
|
|
|
22
|
|
|
if ($files === false) { |
23
|
|
|
return []; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$files = array_map(function ($file) { |
27
|
|
|
return str_replace('.php', '', basename($file)); |
28
|
|
|
|
29
|
|
|
}, $files); |
30
|
|
|
|
31
|
|
|
sort($files); |
32
|
|
|
|
33
|
|
|
return $files; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Require a file. |
38
|
|
|
* |
39
|
|
|
* @param $path |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function requireFile($path) |
44
|
|
|
{ |
45
|
|
|
require_once $path; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a directory if it does not exist. |
50
|
|
|
* |
51
|
|
|
* @param $dir |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function createDirIfItDoesNotExist($dir) |
56
|
|
|
{ |
57
|
|
|
if (!file_exists($dir)) { |
58
|
|
|
mkdir($dir, 0755, true); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the content of a file. |
64
|
|
|
* |
65
|
|
|
* @param string $path |
66
|
|
|
* |
67
|
|
|
* @throws Exception |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getContent($path) |
72
|
|
|
{ |
73
|
|
|
if (!file_exists($path)) { |
74
|
|
|
throw new Exception("File does not exist at path {$path}"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return file_get_contents($path); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Write the contents of a file. |
82
|
|
|
* |
83
|
|
|
* @param string $path |
84
|
|
|
* @param string $contents |
85
|
|
|
* @param bool $lock |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function putContent($path, $contents, $lock = false) |
90
|
|
|
{ |
91
|
|
|
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if file exists. |
96
|
|
|
* |
97
|
|
|
* @param string $path |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function exists($path) |
102
|
|
|
{ |
103
|
|
|
return file_exists($path); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Delete file. |
108
|
|
|
* |
109
|
|
|
* @param string $path |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function delete($path) |
114
|
|
|
{ |
115
|
|
|
return $this->exists($path) ? unlink($path) : false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Move file. |
120
|
|
|
* |
121
|
|
|
* @param string $path_from |
122
|
|
|
* @param string $path_to |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function move($path_from, $path_to) |
127
|
|
|
{ |
128
|
|
|
return $this->exists($path_from) ? rename($path_from, $path_to) : false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|