|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Adapter\PhpFilesystemAdapter |
|
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\Adapter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Adapter for a PHP filesystem implementation. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link https://github.com/techdivision/import |
|
30
|
|
|
* @link http://www.techdivision.com |
|
31
|
|
|
*/ |
|
32
|
|
|
class PhpFilesystemAdapter implements FilesystemAdapterInterface |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates a new directroy. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $pathname The directory path |
|
39
|
|
|
* @param integer $mode The mode is 0700 by default, which means the widest possible access |
|
40
|
|
|
* |
|
41
|
|
|
* @return boolean TRUE on success, else FALSE |
|
42
|
|
|
* @link http://php.net/mkdir |
|
43
|
|
|
*/ |
|
44
|
|
|
public function mkdir($pathname, $mode = 0700) |
|
45
|
|
|
{ |
|
46
|
|
|
return mkdir($pathname, $mode, true); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Query whether or not the passed filename exists. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $filename The filename to query |
|
53
|
|
|
* |
|
54
|
|
|
* @return boolean TRUE if the passed filename exists, else FALSE |
|
55
|
|
|
* @link http://php.net/is_file |
|
56
|
|
|
*/ |
|
57
|
|
|
public function isFile($filename) |
|
58
|
|
|
{ |
|
59
|
|
|
return is_file($filename); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Tells whether the filename is a directory. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $filename Path to the file |
|
66
|
|
|
* |
|
67
|
|
|
* @return TRUE if the filename exists and is a directory, else FALSE |
|
68
|
|
|
* @link http://php.net/is_dir |
|
69
|
|
|
*/ |
|
70
|
|
|
public function isDir($filename) |
|
71
|
|
|
{ |
|
72
|
|
|
return is_dir($filename); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Creates an empty file with the passed filename. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $filename The name of the file to create |
|
79
|
|
|
* |
|
80
|
|
|
* @return boolean TRUE if the file can be created, else FALSE |
|
81
|
|
|
*/ |
|
82
|
|
|
public function touch($filename) |
|
83
|
|
|
{ |
|
84
|
|
|
return touch($filename); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Renames a file or directory. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $oldname The old name |
|
91
|
|
|
* @param string $newname The new name |
|
92
|
|
|
* |
|
93
|
|
|
* @return boolean TRUE on success, else FALSE |
|
94
|
|
|
* @link http://php.net/rename |
|
95
|
|
|
*/ |
|
96
|
|
|
public function rename($oldname, $newname) |
|
97
|
|
|
{ |
|
98
|
|
|
return rename($oldname, $newname); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Writes the passed data to file with the passed name. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $filename The name of the file to write the data to |
|
105
|
|
|
* @param string $data The data to write to the file |
|
106
|
|
|
* |
|
107
|
|
|
* @return number The number of bytes written to the file |
|
108
|
|
|
* @link http://php.net/file_put_contents |
|
109
|
|
|
*/ |
|
110
|
|
|
public function write($filename, $data) |
|
111
|
|
|
{ |
|
112
|
|
|
return file_put_contents($filename, $data); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Delete the file with the passed name. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $filename The name of the file to be deleted |
|
119
|
|
|
* |
|
120
|
|
|
* @return boolean TRUE on success, else FALSE |
|
121
|
|
|
*/ |
|
122
|
|
|
public function delete($filename) |
|
123
|
|
|
{ |
|
124
|
|
|
return unlink($filename); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Copy's a file from source to destination. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $src The source file |
|
131
|
|
|
* @param string $dest The destination file |
|
132
|
|
|
* |
|
133
|
|
|
* @return boolean TRUE on success, else FALSE |
|
134
|
|
|
* @link http://php.net/copy |
|
135
|
|
|
*/ |
|
136
|
|
|
public function copy($src, $dest) |
|
137
|
|
|
{ |
|
138
|
|
|
return copy($src, $dest); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* List the filenames of a directory. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $directory The directory to list |
|
145
|
|
|
* @param boolean $recursive Whether to list recursively |
|
146
|
|
|
* |
|
147
|
|
|
* @return array A list of filenames |
|
148
|
|
|
*/ |
|
149
|
|
|
public function listContents($directory = '', $recursive = false) |
|
150
|
|
|
{ |
|
151
|
|
|
|
|
152
|
|
|
// parse the directory |
|
153
|
|
|
$files = glob($pattern = sprintf('%s/*', $directory), 0); |
|
154
|
|
|
|
|
155
|
|
|
// parse all subdirectories, if recursive parsing is wanted |
|
156
|
|
|
if ($recursive !== false) { |
|
157
|
|
|
foreach (glob(dirname($pattern). DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR|GLOB_NOSORT|GLOB_BRACE) as $dir) { |
|
158
|
|
|
$files = array_merge($files, $this->listContents($dir . DIRECTORY_SEPARATOR . basename($pattern), $recursive)); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// return the array with the files matching the glob pattern |
|
163
|
|
|
return $files; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.