1
|
|
|
<?php namespace FreedomCore\TrinityCore\Support; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Filesystem |
5
|
|
|
* @package FreedomCore\TrinityCore\Support |
6
|
|
|
*/ |
7
|
|
|
class Filesystem |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Path to the public folder |
12
|
|
|
* @var null|string |
13
|
|
|
*/ |
14
|
|
|
protected $publicFolder = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Path to the storage folder |
18
|
|
|
* @var null|string |
19
|
|
|
*/ |
20
|
|
|
protected $storageFolder = null; |
21
|
|
|
|
22
|
|
|
protected $dataFilesStorage = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Filesystem constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->initializeVariables(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get public folder path |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getPublicFolder() : string |
37
|
|
|
{ |
38
|
|
|
return $this->publicFolder; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get storage folder path |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getStorageFolder() : string |
46
|
|
|
{ |
47
|
|
|
return $this->storageFolder; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get data store folder path |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getDataStorage() : string |
55
|
|
|
{ |
56
|
|
|
return $this->dataFilesStorage; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get Structures folder path |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getStructuresFolder() : string |
64
|
|
|
{ |
65
|
|
|
return __DIR__ . DIRECTORY_SEPARATOR . 'DB2Reader' . DIRECTORY_SEPARATOR . 'Structures' . DIRECTORY_SEPARATOR; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set public folder path |
70
|
|
|
* @param string $path |
71
|
|
|
*/ |
72
|
|
|
public function setPublicFolder(string $path) |
73
|
|
|
{ |
74
|
|
|
$this->publicFolder = $path; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set storage folder path |
79
|
|
|
* @param string $path |
80
|
|
|
*/ |
81
|
|
|
public function setStorageFolder(string $path) |
82
|
|
|
{ |
83
|
|
|
$this->storageFolder = $path; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set data store folder path |
88
|
|
|
* @param string $path |
89
|
|
|
*/ |
90
|
|
|
public function setDataStorage(string $path) |
91
|
|
|
{ |
92
|
|
|
$this->dataFilesStorage = $path; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if file exists |
97
|
|
|
* @param string $path |
98
|
|
|
* @param string $fileName |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public static function fileExists(string $path, string $fileName = '') : bool |
102
|
|
|
{ |
103
|
|
|
$fullPath = (strlen($fileName) > 1) ? $path . DIRECTORY_SEPARATOR . $fileName : $path; |
104
|
|
|
return file_exists($fullPath); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get folders in the folder |
109
|
|
|
* @param string $path |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public static function foldersInFolder(string $path) : array |
113
|
|
|
{ |
114
|
|
|
return array_map(function (string $value) { |
115
|
|
|
return str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $value); |
116
|
|
|
}, glob($path . '/*', GLOB_ONLYDIR)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get files in the folder |
121
|
|
|
* @param string $path |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public static function filesInFolder(string $path) : array |
125
|
|
|
{ |
126
|
|
|
return array_map(function (string $value) { |
127
|
|
|
return str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $value); |
128
|
|
|
}, glob($path . '/*', GLOB_NOSORT)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Initialize required variables. |
133
|
|
|
*/ |
134
|
|
|
private function initializeVariables() |
135
|
|
|
{ |
136
|
|
|
$this->publicFolder = getcwd(); |
137
|
|
|
$this->storageFolder = function_exists('storage_path') ? |
138
|
|
|
storage_path() : |
139
|
|
|
(strstr($this->publicFolder, 'public') ? |
140
|
|
|
str_replace('public', 'storage', $this->publicFolder) : |
141
|
|
|
$this->publicFolder . DIRECTORY_SEPARATOR . 'storage' |
142
|
|
|
); |
143
|
|
|
$this->dataFilesStorage = $this->storageFolder . DIRECTORY_SEPARATOR . 'data'; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|