1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the SVN-Buddy library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Repository\Connector; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use ConsoleHelpers\ConsoleKit\Config\ConfigEditor; |
15
|
|
|
use ConsoleHelpers\ConsoleKit\ConsoleIO; |
16
|
|
|
use ConsoleHelpers\SVNBuddy\Cache\CacheManager; |
17
|
|
|
use ConsoleHelpers\SVNBuddy\Process\IProcessFactory; |
18
|
|
|
|
19
|
|
|
class CommandFactory |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Reference to configuration. |
24
|
|
|
* |
25
|
|
|
* @var ConfigEditor |
26
|
|
|
*/ |
27
|
|
|
private $_configEditor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Process factory. |
31
|
|
|
* |
32
|
|
|
* @var IProcessFactory |
33
|
|
|
*/ |
34
|
|
|
private $_processFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Console IO. |
38
|
|
|
* |
39
|
|
|
* @var ConsoleIO |
40
|
|
|
*/ |
41
|
|
|
private $_io; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Cache manager. |
45
|
|
|
* |
46
|
|
|
* @var CacheManager |
47
|
|
|
*/ |
48
|
|
|
private $_cacheManager; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Path to an svn command. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $_svnCommand = 'svn'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates command factory. |
59
|
|
|
* |
60
|
|
|
* @param ConfigEditor $config_editor ConfigEditor. |
61
|
|
|
* @param IProcessFactory $process_factory Process factory. |
62
|
|
|
* @param ConsoleIO $io Console IO. |
63
|
|
|
* @param CacheManager $cache_manager Cache manager. |
64
|
|
|
*/ |
65
|
19 |
|
public function __construct( |
66
|
|
|
ConfigEditor $config_editor, |
67
|
|
|
IProcessFactory $process_factory, |
68
|
|
|
ConsoleIO $io, |
69
|
|
|
CacheManager $cache_manager |
70
|
|
|
) { |
71
|
19 |
|
$this->_configEditor = $config_editor; |
72
|
19 |
|
$this->_processFactory = $process_factory; |
73
|
19 |
|
$this->_io = $io; |
74
|
19 |
|
$this->_cacheManager = $cache_manager; |
75
|
|
|
|
76
|
19 |
|
$this->prepareSvnCommand(); |
77
|
19 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Prepares static part of svn command to be used across the script. |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
19 |
|
protected function prepareSvnCommand() |
85
|
|
|
{ |
86
|
19 |
|
$username = $this->_configEditor->get('repository-connector.username'); |
87
|
19 |
|
$password = $this->_configEditor->get('repository-connector.password'); |
88
|
|
|
|
89
|
19 |
|
$this->_svnCommand .= ' --non-interactive'; |
90
|
|
|
|
91
|
19 |
|
if ( $username ) { |
92
|
2 |
|
$this->_svnCommand .= ' --username ' . $username; |
93
|
|
|
} |
94
|
|
|
|
95
|
19 |
|
if ( $password ) { |
96
|
2 |
|
$this->_svnCommand .= ' --password ' . $password; |
97
|
|
|
} |
98
|
19 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Builds a command. |
102
|
|
|
* |
103
|
|
|
* @param string $sub_command Sub command. |
104
|
|
|
* @param string|null $param_string Parameter string. |
105
|
|
|
* |
106
|
|
|
* @return Command |
107
|
|
|
*/ |
108
|
10 |
|
public function getCommand($sub_command, $param_string = null) |
109
|
|
|
{ |
110
|
10 |
|
$command_line = $this->buildCommand($sub_command, $param_string); |
111
|
|
|
|
112
|
9 |
|
return new Command( |
113
|
9 |
|
$command_line, |
114
|
9 |
|
$this->_io, |
115
|
9 |
|
$this->_cacheManager, |
116
|
9 |
|
$this->_processFactory |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Builds command from given arguments. |
122
|
|
|
* |
123
|
|
|
* @param string $sub_command Command. |
124
|
|
|
* @param string $param_string Parameter string. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
* @throws \InvalidArgumentException When command contains spaces. |
128
|
|
|
*/ |
129
|
10 |
|
protected function buildCommand($sub_command, $param_string = null) |
130
|
|
|
{ |
131
|
10 |
|
if ( strpos($sub_command, ' ') !== false ) { |
132
|
1 |
|
throw new \InvalidArgumentException('The "' . $sub_command . '" sub-command contains spaces.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
9 |
|
$command_line = $this->_svnCommand; |
136
|
|
|
|
137
|
9 |
|
if ( !empty($sub_command) ) { |
138
|
4 |
|
$command_line .= ' ' . $sub_command; |
139
|
|
|
} |
140
|
|
|
|
141
|
9 |
|
if ( !empty($param_string) ) { |
142
|
8 |
|
$command_line .= ' ' . $param_string; |
143
|
|
|
} |
144
|
|
|
|
145
|
9 |
|
$command_line = preg_replace_callback( |
146
|
9 |
|
'/\{([^\}]*)\}/', |
147
|
|
|
function (array $matches) { |
148
|
2 |
|
return escapeshellarg($matches[1]); |
149
|
9 |
|
}, |
150
|
9 |
|
$command_line |
151
|
|
|
); |
152
|
|
|
|
153
|
9 |
|
return $command_line; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|