|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tylercd100\GitDeploy; |
|
4
|
|
|
|
|
5
|
|
|
class Deploy { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A callback function to call after the deploy has finished. |
|
9
|
|
|
* |
|
10
|
|
|
* @var callback |
|
11
|
|
|
*/ |
|
12
|
|
|
public $post_deploy; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The name of the file that will be used for logging deployments. Set to |
|
16
|
|
|
* FALSE to disable logging. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $_log = 'deployments.log'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The timestamp format used for logging. |
|
24
|
|
|
* |
|
25
|
|
|
* @link http://www.php.net/manual/en/function.date.php |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_date_format = 'Y-m-d H:i:sP'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The name of the branch to pull from. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $_branch = 'master'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The name of the remote to pull from. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $_remote = 'origin'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The directory where your website and git repository are located, can be |
|
46
|
|
|
* a relative or absolute path |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $_directory; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets up defaults. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $directory Directory where your website is located |
|
56
|
|
|
* @param array $data Information about the deployment |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct($directory, $options = array()) |
|
59
|
|
|
{ |
|
60
|
|
|
// Determine the directory path |
|
61
|
|
|
$this->_directory = realpath($directory).DIRECTORY_SEPARATOR; |
|
62
|
|
|
|
|
63
|
|
|
$available_options = array('log', 'date_format', 'branch', 'remote'); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($options as $option => $value) |
|
66
|
|
|
{ |
|
67
|
|
|
if (in_array($option, $available_options)) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->{'_'.$option} = $value; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->log('Attempting deployment...'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Writes a message to the log file. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $message The message to write |
|
80
|
|
|
* @param string $type The type of log message (e.g. INFO, DEBUG, ERROR, etc.) |
|
81
|
|
|
*/ |
|
82
|
|
|
public function log($message, $type = 'INFO') |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->_log) |
|
85
|
|
|
{ |
|
86
|
|
|
// Set the name of the log file |
|
87
|
|
|
$filename = $this->_log; |
|
88
|
|
|
|
|
89
|
|
|
if ( ! file_exists($filename)) |
|
90
|
|
|
{ |
|
91
|
|
|
// Create the log file |
|
92
|
|
|
file_put_contents($filename, ''); |
|
93
|
|
|
|
|
94
|
|
|
// Allow anyone to write to log files |
|
95
|
|
|
chmod($filename, 0666); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Write the message into the log file |
|
99
|
|
|
// Format: time --- type: message |
|
100
|
|
|
file_put_contents($filename, date($this->_date_format).' --- '.$type.': '.$message.PHP_EOL, FILE_APPEND); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Executes the necessary commands to deploy the website. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function execute() |
|
108
|
|
|
{ |
|
109
|
|
|
try |
|
110
|
|
|
{ |
|
111
|
|
|
// Make sure we're in the right directory |
|
112
|
|
|
exec('cd '.$this->_directory, $output); |
|
113
|
|
|
$this->log('Changing working directory... '.implode(' ', $output)); |
|
114
|
|
|
|
|
115
|
|
|
// Discard any changes to tracked files since our last deploy |
|
116
|
|
|
exec('git reset --hard HEAD', $output); |
|
117
|
|
|
$this->log('Reseting repository... '.implode(' ', $output)); |
|
118
|
|
|
|
|
119
|
|
|
// Update the local repository |
|
120
|
|
|
exec('git pull '.$this->_remote.' '.$this->_branch, $output); |
|
121
|
|
|
$this->log('Pulling in changes... '.implode(' ', $output)); |
|
122
|
|
|
|
|
123
|
|
|
// Secure the .git directory |
|
124
|
|
|
exec('chmod -R og-rx .git'); |
|
125
|
|
|
$this->log('Securing .git directory... '); |
|
126
|
|
|
|
|
127
|
|
|
if (is_callable($this->post_deploy)) |
|
128
|
|
|
{ |
|
129
|
|
|
call_user_func($this->post_deploy, $this->_data); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->log('Deployment successful.'); |
|
133
|
|
|
} |
|
134
|
|
|
catch (Exception $e) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
$this->log($e, 'ERROR'); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.