|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Subjects\FilesystemTrait |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects; |
|
22
|
|
|
|
|
23
|
|
|
use League\Flysystem\FilesystemInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The trait implementation that provides filesystem handling functionality. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
trait FilesystemTrait |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The virtual filesystem instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \League\Flysystem\FilesystemInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $filesystem; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The root directory for the virtual filesystem. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $rootDir; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set's root directory for the virtual filesystem. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $rootDir The root directory for the virtual filesystem |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setRootDir($rootDir) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->rootDir = $rootDir; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return's the root directory for the virtual filesystem. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string The root directory for the virtual filesystem |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getRootDir() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->rootDir; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set's the virtual filesystem instance. |
|
75
|
|
|
* |
|
76
|
|
|
* @param \League\Flysystem\FilesystemInterface $filesystem The filesystem instance |
|
77
|
|
|
* |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setFilesystem(FilesystemInterface $filesystem) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->filesystem = $filesystem; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return's the virtual filesystem instance. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \League\Flysystem\FilesystemInterface The filesystem instance |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getFilesystem() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->filesystem; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* This method tries to resolve the passed path and returns it. If the path |
|
97
|
|
|
* is relative, the actual working directory will be prepended. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $path The path to be resolved |
|
100
|
|
|
* |
|
101
|
|
|
* @return string The resolved path |
|
102
|
|
|
* @throws \InvalidArgumentException Is thrown, if the path can not be resolved |
|
103
|
|
|
*/ |
|
104
|
|
|
public function resolvePath($path) |
|
105
|
|
|
{ |
|
106
|
|
|
|
|
107
|
|
|
// if we've an absolute path, return it immediately |
|
108
|
|
|
if ($this->getFilesystem()->has($path)) { |
|
109
|
|
|
return $path; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// try to prepend the actual working directory, assuming we've a relative path |
|
113
|
|
|
if ($this->getFilesystem()->has($path = getcwd() . DIRECTORY_SEPARATOR . $path)) { |
|
114
|
|
|
return $path; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// throw an exception if the passed directory doesn't exists |
|
118
|
|
|
throw new \InvalidArgumentException( |
|
119
|
|
|
sprintf('Directory %s doesn\'t exist', $path) |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Query whether or not the passed filename exists. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $filename The filename to query |
|
127
|
|
|
* |
|
128
|
|
|
* @return boolean TRUE if the passed filename exists, else FALSE |
|
129
|
|
|
*/ |
|
130
|
|
|
public function isFile($filename) |
|
131
|
|
|
{ |
|
132
|
|
|
return is_file($filename); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Creates an empty file with the passed filename. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $filename The name of the file to create |
|
139
|
|
|
* |
|
140
|
|
|
* @return boolean TRUE if the file can be created, else FALSE |
|
141
|
|
|
*/ |
|
142
|
|
|
public function touch($filename) |
|
143
|
|
|
{ |
|
144
|
|
|
return touch($filename); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Renames a file or directory. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $oldname The old name |
|
151
|
|
|
* @param string $newname The new name |
|
152
|
|
|
* |
|
153
|
|
|
* @return boolean TRUE on success, else FALSE |
|
154
|
|
|
* @link http://php.net/rename |
|
155
|
|
|
*/ |
|
156
|
|
|
public function rename($oldname, $newname) |
|
157
|
|
|
{ |
|
158
|
|
|
return rename($oldname, $newname); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Writes the passed data to file with the passed name. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $filename The name of the file to write the data to |
|
165
|
|
|
* @param string $data The data to write to the file |
|
166
|
|
|
* |
|
167
|
|
|
* @return number The number of bytes written to the file |
|
168
|
|
|
* @link http://php.net/file_put_contents |
|
169
|
|
|
*/ |
|
170
|
|
|
public function write($filename, $data) |
|
171
|
|
|
{ |
|
172
|
|
|
return file_put_contents($filename, $data); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|