|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer\Initializer; |
|
9
|
|
|
|
|
10
|
|
|
use Deployer\Initializer\Exception\IOException; |
|
11
|
|
|
use Deployer\Initializer\Exception\TemplateNotFoundException; |
|
12
|
|
|
use Deployer\Initializer\Template\TemplateInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Initializer system |
|
16
|
|
|
* |
|
17
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Initializer |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array|TemplateInterface[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $templates; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Add template to initializer |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @param TemplateInterface $template |
|
31
|
|
|
* |
|
32
|
|
|
* @return Initializer |
|
33
|
|
|
*/ |
|
34
|
6 |
|
public function addTemplate($name, TemplateInterface $template) |
|
35
|
|
|
{ |
|
36
|
6 |
|
$this->templates[$name] = $template; |
|
37
|
|
|
|
|
38
|
6 |
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get template names |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getTemplateNames() |
|
47
|
|
|
{ |
|
48
|
|
|
return array_keys($this->templates); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Initialize deployer in project |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $template |
|
55
|
|
|
* @param string $directory |
|
56
|
|
|
* @param string $file |
|
57
|
|
|
* |
|
58
|
|
|
* @return string The configuration file path |
|
59
|
|
|
* |
|
60
|
|
|
* @throws TemplateNotFoundException |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function initialize($template, $directory, $file = 'deploy.php') |
|
63
|
|
|
{ |
|
64
|
6 |
|
if (!isset($this->templates[$template])) { |
|
65
|
1 |
|
throw TemplateNotFoundException::create($template, array_keys($this->templates)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
$this->checkDirectoryBeforeInitialize($directory); |
|
69
|
2 |
|
$this->checkFileBeforeInitialize($directory, $file); |
|
70
|
|
|
|
|
71
|
1 |
|
$filePath = $directory . '/' . $file; |
|
72
|
|
|
|
|
73
|
1 |
|
$this->templates[$template]->initialize($filePath); |
|
74
|
|
|
|
|
75
|
1 |
|
return $filePath; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Check the directory before initialize |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $directory |
|
82
|
|
|
* |
|
83
|
|
|
* @throws IOException |
|
84
|
|
|
*/ |
|
85
|
5 |
|
private function checkDirectoryBeforeInitialize($directory) |
|
86
|
|
|
{ |
|
87
|
5 |
|
if (!file_exists($directory)) { |
|
88
|
1 |
|
set_error_handler(function ($errCode, $errStr) use ($directory) { |
|
89
|
1 |
|
$parts = explode(':', $errStr, 2); |
|
90
|
1 |
|
$errorMessage = isset($parts[1]) ? trim($parts[1]) : 'Undefined'; |
|
91
|
|
|
|
|
92
|
1 |
|
throw new IOException(sprintf( |
|
93
|
1 |
|
'Could not create directory "%s". %s', |
|
94
|
1 |
|
$directory, |
|
95
|
|
|
$errorMessage |
|
96
|
1 |
|
), $errCode); |
|
97
|
1 |
|
}); |
|
98
|
|
|
|
|
99
|
1 |
|
mkdir($directory, 0775); |
|
100
|
|
|
|
|
101
|
|
|
restore_error_handler(); |
|
102
|
4 |
|
} elseif (!is_dir($directory)) { |
|
103
|
1 |
|
throw new IOException(sprintf( |
|
104
|
1 |
|
'Can not create directory. The path "%s" already exist.', |
|
105
|
|
|
$directory |
|
106
|
1 |
|
)); |
|
107
|
3 |
|
} elseif (!is_writable($directory)) { |
|
108
|
1 |
|
throw new IOException(sprintf( |
|
109
|
1 |
|
'The directory "%s" is not writable.', |
|
110
|
|
|
$directory |
|
111
|
1 |
|
)); |
|
112
|
|
|
} |
|
113
|
2 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Check the file before initialize |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $directory |
|
119
|
|
|
* @param string $file |
|
120
|
|
|
* |
|
121
|
|
|
* @throws IOException |
|
122
|
|
|
*/ |
|
123
|
2 |
|
private function checkFileBeforeInitialize($directory, $file) |
|
124
|
|
|
{ |
|
125
|
2 |
|
$filePath = $directory . '/' . $file; |
|
126
|
|
|
|
|
127
|
2 |
|
if (file_exists($filePath)) { |
|
128
|
1 |
|
throw new IOException(sprintf( |
|
129
|
1 |
|
'Can not initialize deployer. The file "%s" already exist.', |
|
130
|
|
|
$filePath |
|
131
|
1 |
|
)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
touch($filePath); |
|
135
|
1 |
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|