1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Conveyor package. |
5
|
|
|
* |
6
|
|
|
* (c) Jeroen Fiege <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webcreate\Conveyor\Transporter; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\Finder\Finder; |
16
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
17
|
|
|
use Webcreate\Conveyor\Event\TransporterEvent; |
18
|
|
|
use Webcreate\Conveyor\Event\TransporterEvents; |
19
|
|
|
|
20
|
|
|
class FileTransporter extends AbstractTransporter |
21
|
|
|
{ |
22
|
|
|
public function getHost() |
23
|
|
|
{ |
24
|
|
|
return 'localhost'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function exists($path) |
28
|
|
|
{ |
29
|
|
|
return file_exists($path); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function mkdir($dest) |
33
|
|
|
{ |
34
|
1 |
|
$this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_MKDIR, new TransporterEvent($this, $dest)); |
35
|
|
|
|
36
|
1 |
|
mkdir($dest, 0777, true); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function get($src, $dest = null) |
40
|
|
|
{ |
41
|
1 |
View Code Duplication |
if (is_file($src) && is_dir($dest)) { |
|
|
|
|
42
|
|
|
$dest = $dest . DIRECTORY_SEPARATOR . basename($src); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_GET, new TransporterEvent($this, array('src' => $src, 'dest' => $dest))); |
46
|
|
|
|
47
|
1 |
|
if (null === $dest) { |
48
|
|
|
return file_get_contents($src); |
49
|
|
|
} else { |
50
|
1 |
|
if (is_file($src)) { |
51
|
1 |
|
$success = copy($src, $dest); |
|
|
|
|
52
|
|
|
} else { |
53
|
|
|
$filesystem = new Filesystem(); |
54
|
|
|
$filesystem->mirror($src, $dest); |
55
|
|
|
} |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
public function put($src, $dest) |
60
|
|
|
{ |
61
|
1 |
|
if (false === file_exists($src)) { |
62
|
1 |
|
throw new \InvalidArgumentException(sprintf('Resource \'%s\' does not exist', $src)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
if (is_file($src) && is_dir($dest)) { |
|
|
|
|
66
|
|
|
$dest = $dest . DIRECTORY_SEPARATOR . basename($src); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (is_file($src)) { |
70
|
|
|
$file = new \SplFileInfo($src); |
71
|
|
|
|
72
|
|
|
$this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_PUT, new TransporterEvent($this, array('dest' => $dest, 'src' => $file->getPathname()))); |
73
|
|
|
|
74
|
|
|
$pwd = dirname($dest); |
75
|
|
|
if (!is_dir($pwd)) { |
76
|
|
|
$this->mkdir($pwd); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
copy($file->getPathname(), $dest); |
80
|
|
|
} else { |
81
|
|
|
if (!$this->exists($dest)) { |
82
|
|
|
$this->mkdir($dest); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$finder = new Finder(); |
86
|
|
|
$test = $finder->in($src)->depth('== 0'); |
87
|
|
|
|
88
|
|
|
foreach ($test as $file) { |
89
|
|
|
$this->put(rtrim($src, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file->getFilename(), rtrim($dest, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file->getFilename()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function putContent($content, $dest) |
95
|
|
|
{ |
96
|
1 |
|
$this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_PUT_CONTENT, new TransporterEvent($this, array('dest' => $dest, 'content' => $content))); |
97
|
|
|
|
98
|
1 |
|
file_put_contents($dest, $content); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Creates a symlink on the remote server |
103
|
|
|
* |
104
|
|
|
* @param string $src |
105
|
|
|
* @param string $dest |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
1 |
|
public function symlink($src, $dest) |
109
|
|
|
{ |
110
|
1 |
|
$dest = rtrim($dest, '/'); |
|
|
|
|
111
|
|
|
|
112
|
1 |
|
$this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_SYMLINK, new TransporterEvent($this, array('dest' => $dest, 'src' => $src))); |
113
|
|
|
|
114
|
1 |
|
$filesystem = new Filesystem(); |
115
|
1 |
|
$filesystem->symlink($src, $dest); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks for symlink on the remote server |
120
|
|
|
* |
121
|
|
|
* @param $dest |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isSymlink($dest) |
125
|
|
|
{ |
126
|
|
|
return is_link($dest); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Copies a file/directory on the remote host |
131
|
|
|
* |
132
|
|
|
* @param string $src |
133
|
|
|
* @param string $dest |
134
|
|
|
* @param bool $recursive |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function copy($src, $dest, $recursive = true) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_COPY, new TransporterEvent($this, array('dest' => $dest, 'src' => $src))); |
140
|
|
|
|
141
|
|
|
$filesystem = new Filesystem(); |
142
|
|
|
|
143
|
|
|
if ($recursive) { |
144
|
|
|
$filesystem->mirror($src, $dest); |
145
|
|
|
} else { |
146
|
|
|
$filesystem->copy($src, $dest); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Removes a file/directory on the remote host |
152
|
|
|
* |
153
|
|
|
* @param string $path |
154
|
|
|
* @param bool $recursive |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
1 |
View Code Duplication |
public function remove($path, $recursive = true) |
|
|
|
|
158
|
|
|
{ |
159
|
1 |
|
$this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_REMOVE, new TransporterEvent($this, array('path' => $path))); |
160
|
|
|
|
161
|
1 |
|
$filesystem = new Filesystem(); |
162
|
1 |
|
$filesystem->remove($path); |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Lists files and directories |
167
|
|
|
* |
168
|
|
|
* @param string $path |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
|
|
public function ls($path) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$finder = new Finder(); |
174
|
|
|
$test = $finder->in($path)->depth('== 0'); |
175
|
|
|
|
176
|
|
|
$retval = array(); |
177
|
|
|
|
178
|
|
|
/** @var $file SplFileInfo */ |
179
|
|
|
foreach ($test as $file) { |
180
|
|
|
$retval[$file->getFilename()] = array( |
181
|
|
|
'type' => $file->isDir() ? 'directory' : 'file', |
182
|
|
|
'mtime' => new \DateTime('@' . $file->getMTime()), |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $retval; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.