1
|
|
|
<?php |
2
|
|
|
namespace samsonphp\deploy; |
3
|
|
|
|
4
|
|
|
use samson\core\Service; |
5
|
|
|
use samsonphp\event\Event; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* SamsonPHP deployment service |
9
|
|
|
* |
10
|
|
|
* @package samsonphp\deploy |
11
|
|
|
* @author Vitaly Iegorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Deploy extends Service |
14
|
|
|
{ |
15
|
|
|
/** Идентификатор модуля */ |
16
|
|
|
protected $id = 'deploy'; |
17
|
|
|
|
18
|
|
|
/** @var Remote */ |
19
|
|
|
public $remote; |
20
|
|
|
|
21
|
|
|
/** @var array Collection of path names to be ignored */ |
22
|
|
|
public $ignorePath = array('cms'); |
23
|
|
|
|
24
|
|
|
/** Path to site document root on local server */ |
25
|
|
|
public $sourceroot = ''; |
26
|
|
|
|
27
|
|
|
/** FTP host */ |
28
|
|
|
public $host = ''; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** Path to site document root on remote server */ |
31
|
|
|
public $wwwroot = ''; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** FTP username */ |
34
|
|
|
public $username= ''; |
35
|
|
|
|
36
|
|
|
/** FTP password */ |
37
|
|
|
public $password= ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get all entries in $path |
41
|
|
|
* @param string $path Folder path for listing |
42
|
|
|
* @return array Collection of entries int folder |
43
|
|
|
*/ |
44
|
|
|
protected function directoryFiles($path) |
45
|
|
|
{ |
46
|
|
|
$result = array(); |
47
|
|
|
// Get all entries in path |
48
|
|
|
foreach (array_diff(scandir($path), array_merge($this->ignorePath, array('..', '.'))) as $entry) { |
49
|
|
|
// Build full REAL path to entry |
50
|
|
|
$result[$entry] = realpath($path . '/' . $entry); |
51
|
|
|
} |
52
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Perform synchronizing folder via FTP connection |
57
|
|
|
* @param string $path Local path for synchronizing |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
protected function synchronize($path) |
60
|
|
|
{ |
61
|
|
|
$this->remote->log('Synchronizing remote folder [##]', $path); |
62
|
|
|
|
63
|
|
|
// Check if we can read this path |
64
|
|
|
foreach ($this->directoryFiles($path) as $fileName => $fullPath) { |
65
|
|
|
// If this is a folder |
66
|
|
|
if (is_dir($fullPath)) { |
67
|
|
|
// Try to create it |
68
|
|
|
$this->remote->mkDir($fileName); |
69
|
|
|
// Go deeper in recursion |
70
|
|
|
$this->synchronize($fullPath); |
71
|
|
|
} elseif ($this->remote->isOld($fullPath)) { // Check if file has to be updated |
72
|
|
|
// Copy file to remote |
73
|
|
|
$this->remote->write($fullPath); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Go one level up |
78
|
|
|
$this->remote->cdup(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Initialize module |
83
|
|
|
* @param array $params |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function init(array $params = array()) |
87
|
|
|
{ |
88
|
|
|
// Check configuration |
89
|
|
|
if (!isset($this->sourceroot{0})) { |
90
|
|
|
return $this->error('Local project folder['.$this->sourceroot.'] is not specified'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Check configuration |
94
|
|
|
if (!isset($this->wwwroot{0})) { |
95
|
|
|
return $this->error('Remote project folder['.$this->wwwroot.'] is not specified'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return parent::init($params); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Signal module error |
103
|
|
|
* @param string $message error message |
104
|
|
|
* @return bool false |
105
|
|
|
*/ |
106
|
|
|
public function error($message) |
107
|
|
|
{ |
108
|
|
|
// Signal error |
109
|
|
|
Event::fire('error', array($this, $message)); |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Perform project deployment |
116
|
|
|
*/ |
117
|
|
|
protected function deploy() |
118
|
|
|
{ |
119
|
|
|
// If this is remote app - chdir to it |
120
|
|
|
if (__SAMSON_REMOTE_APP) { |
121
|
|
|
// Create folder |
122
|
|
|
$this->remote->mkDir(str_replace('/', '', __SAMSON_BASE__)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Выполним синхронизацию папок |
126
|
|
|
$this->synchronize($this->sourceroot); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** Controller to perform deploy routine */ |
130
|
|
|
public function __BASE() |
131
|
|
|
{ |
132
|
|
|
$this->title('Deploying project to '.$this->host); |
133
|
|
|
|
134
|
|
|
// If no remote class name is set |
135
|
|
|
if (!isset($this->remote)) { |
136
|
|
|
// Create remote connection instance |
137
|
|
|
$this->remote = new Remote($this->host, $this->username, $this->password); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Go to project remote folder |
141
|
|
|
if (empty($this->wwwroot) || $this->remote->cd($this->wwwroot)) { |
142
|
|
|
|
143
|
|
|
$this->deploy(); |
144
|
|
|
|
145
|
|
|
$this->remote->log('Project[##] has been successfully deployed to [##]', $this->sourceroot, $this->host); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|