This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
/
Filesystem.php
208 lines (183 loc) · 4.55 KB
/
Filesystem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
* This file is part of Rocketeer
*
* (c) Maxime Fabre <ehtnam6@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Rocketeer\Traits\BashModules;
/**
* Files and folders handling
*
* @author Maxime Fabre <ehtnam6@gmail.com>
*/
class Filesystem extends Core
{
////////////////////////////////////////////////////////////////////
/////////////////////////////// COMMON /////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Symlinks two folders
*
* @param string $folder The folder in shared/
* @param string $symlink The folder that will symlink to it
*
* @return string
*/
public function symlink($folder, $symlink)
{
if (!$this->fileExists($folder)) {
if (!$this->fileExists($symlink)) {
return false;
}
$this->move($symlink, $folder);
}
// Remove existing symlink
$this->removeFolder($symlink);
return $this->run(sprintf('ln -s %s %s', $folder, $symlink));
}
/**
* Move a file
*
* @param string $origin
* @param string $destination
*
* @return string
*/
public function move($origin, $destination)
{
return $this->fromTo('mv', $origin, $destination);
}
/**
* Copy a file
*
* @param string $origin
* @param string $destination
*
* @return string
*/
public function copy($origin, $destination)
{
return $this->fromTo('cp', $origin, $destination);
}
/**
* Get the contents of a directory
*
* @param string $directory
*
* @return array
*/
public function listContents($directory)
{
return $this->run('ls '.$directory, true, true);
}
/**
* Check if a file exists
*
* @param string $file Path to the file
*
* @return boolean
*/
public function fileExists($file)
{
$exists = $this->runRaw('[ -e ' .$file. ' ] && echo "true"');
return trim($exists) == 'true';
}
/**
* Execute permissions actions on a file with the provided callback
*
* @param string $folder
*
* @return string
*/
public function setPermissions($folder)
{
$commands = array();
// Get path to folder
$folder = $this->releasesManager->getCurrentReleasePath($folder);
$this->command->comment('Setting permissions for '.$folder);
// Get permissions options
$callback = $this->rocketeer->getOption('remote.permissions.callback');
$commands = (array) $callback($this, $folder);
// Cancel if setting of permissions is not configured
if (empty($commands)) {
return true;
}
return $this->runForCurrentRelease($commands);
}
////////////////////////////////////////////////////////////////////
//////////////////////////////// FILES /////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Get the contents of a file
*
* @param string $file
*
* @return string
*/
public function getFile($file)
{
return $this->remote->getString($file);
}
/**
* Write to a file
*
* @param string $file
* @param string $contents
*
* @return void
*/
public function putFile($file, $contents)
{
$this->remote->putString($file, $contents);
}
////////////////////////////////////////////////////////////////////
/////////////////////////////// FOLDERS ////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Create a folder in the application's folder
*
* @param string $folder The folder to create
* @param boolean $recursive
*
* @return string The task
*/
public function createFolder($folder = null, $recursive = false)
{
$recursive = $recursive ? '-p ' : null;
return $this->run('mkdir '.$recursive.$this->rocketeer->getFolder($folder));
}
/**
* Remove a folder in the application's folder
*
* @param string $folder The folder to remove
*
* @return string The task
*/
public function removeFolder($folder = null)
{
return $this->run('rm -rf '.$this->rocketeer->getFolder($folder));
}
////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPERS ////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Execute a "from/to" style command
*
* @param string $command
* @param string $from
* @param string $to
*
* @return string
*/
protected function fromTo($command, $from, $to)
{
$folder = dirname($to);
if (!$this->fileExists($folder)) {
$this->createFolder($folder, true);
}
return $this->run(sprintf('%s %s %s', $command, $from, $to));
}
}