1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Base class for Subversion tasks
22
 *
23
 * @author Michiel Rook <mrook@php.net>
24
 * @author Andrew Eddie <andrew.eddie@jamboworks.com>
25
 *
26
 * @package phing.tasks.ext.svn
27
 *
28
 * @see   VersionControl_SVN
29
 * @since 2.2.0
30
 */
31
abstract class SvnBaseTask extends Task
32
{
33
    /**
34
     * @var string
35
     */
36
    private $workingCopy = "";
37

38
    /**
39
     * @var string
40
     */
41
    private $repositoryUrl = "";
42

43
    /**
44
     * @var string
45
     */
46
    private $svnPath = "/usr/bin/svn";
47

48
    protected $svn = null;
49

50
    private $mode = "";
51

52
    private $svnArgs = [];
53

54
    private $svnSwitches = [];
55

56
    protected $configOption = null;
57

58
    private $toDir = "";
59

60
    protected $fetchMode;
61

62
    protected $oldVersion = false;
63

64
    /**
65
     * Initialize Task.
66
     * This method includes any necessary SVN libraries and triggers
67
     * appropriate error if they cannot be found.  This is not done in header
68
     * because we may want this class to be loaded w/o triggering an error.
69
     */
70 0
    public function init()
71
    {
72 0
        include_once 'VersionControl/SVN.php';
73 0
        $this->fetchMode = VERSIONCONTROL_SVN_FETCHMODE_ASSOC;
74 0
        if (!class_exists('VersionControl_SVN')) {
75 0
            throw new Exception("The SVN tasks depend on the pear/versioncontrol_svn package being installed.");
76
        }
77
    }
78

79
    /**
80
     * Sets the path to the workingcopy
81
     *
82
     * @param $workingCopy
83
     */
84 0
    public function setWorkingCopy($workingCopy)
85
    {
86 0
        $this->workingCopy = $workingCopy;
87
    }
88

89
    /**
90
     * Returns the path to the workingcopy
91
     */
92 0
    public function getWorkingCopy()
93
    {
94 0
        return $this->workingCopy;
95
    }
96

97
    /**
98
     * Sets the path/URI to the repository
99
     *
100
     * @param $repositoryUrl
101
     */
102 0
    public function setRepositoryUrl($repositoryUrl)
103
    {
104 0
        $this->repositoryUrl = $repositoryUrl;
105
    }
106

107
    /**
108
     * Returns the path/URI to the repository
109
     */
110 0
    public function getRepositoryUrl()
111
    {
112 0
        return $this->repositoryUrl;
113
    }
114

115
    /**
116
     * Sets the path to the SVN executable
117
     *
118
     * @param $svnPath
119
     */
120 0
    public function setSvnPath($svnPath)
121
    {
122 0
        $this->svnPath = $svnPath;
123
    }
124

125
    /**
126
     * Returns the path to the SVN executable
127
     */
128 0
    public function getSvnPath()
129
    {
130 0
        return $this->svnPath;
131
    }
132

133
    //
134
    // Args
135
    //
136

137
    /**
138
     * Sets the path to export/checkout to
139
     *
140
     * @param $toDir
141
     */
142 0
    public function setToDir($toDir)
143
    {
144 0
        $this->toDir = $toDir;
145
    }
146

147
    /**
148
     * Returns the path to export/checkout to
149
     */
150 0
    public function getToDir()
151
    {
152 0
        return $this->toDir;
153
    }
154

155
    //
156
    // Switches
157
    //
158

159
    /**
160
     * Sets the force switch
161
     *
162
     * @param $value
163
     */
164 0
    public function setForce($value)
165
    {
166 0
        $this->svnSwitches['force'] = $value;
167
    }
168

169
    /**
170
     * Returns the force switch
171
     */
172 0
    public function getForce()
173
    {
174 0
        return isset($this->svnSwitches['force']) ? $this->svnSwitches['force'] : '';
175
    }
176

177
    /**
178
     * Sets the username of the user to export
179
     *
180
     * @param $value
181
     */
182 0
    public function setUsername($value)
183
    {
184 0
        $this->svnSwitches['username'] = $value;
185
    }
186

187
    /**
188
     * Returns the username
189
     */
190 0
    public function getUsername()
191
    {
192 0
        return isset($this->svnSwitches['username']) ? $this->svnSwitches['username'] : '';
193
    }
194

195
    /**
196
     * Sets the password of the user to export
197
     *
198
     * @param $value
199
     */
200 0
    public function setPassword($value)
201
    {
202 0
        $this->svnSwitches['password'] = $value;
203
    }
204

205
    /**
206
     * Returns the password
207
     */
208 0
    public function getPassword()
209
    {
210 0
        return isset($this->svnSwitches['password']) ? $this->svnSwitches['password'] : '';
211
    }
212

213
    /**
214
     * Sets the no-auth-cache switch
215
     *
216
     * @param $value
217
     */
218 0
    public function setNoCache($value)
219
    {
220 0
        $this->svnSwitches['no-auth-cache'] = $value;
221
    }
222

223
    /**
224
     * Returns the no-auth-cache switch
225
     */
226 0
    public function getNoCache()
227
    {
228 0
        return isset($this->svnSwitches['no-auth-cache']) ? $this->svnSwitches['no-auth-cache'] : '';
229
    }
230

231
    /**
232
     * Sets the depth switch
233
     *
234
     * @param $value
235
     */
236 0
    public function setDepth($value)
237
    {
238 0
        $this->svnSwitches['depth'] = $value;
239
    }
240

241
    /**
242
     * Returns the depth switch
243
     */
244 0
    public function getDepth()
245
    {
246 0
        return isset($this->svnSwitches['depth']) ? $this->svnSwitches['depth'] : '';
247
    }
248

249
    /**
250
     * Sets the ignore-externals switch
251
     *
252
     * @param $value
253
     */
254 0
    public function setIgnoreExternals($value)
255
    {
256 0
        $this->svnSwitches['ignore-externals'] = $value;
257
    }
258

259
    /**
260
     * Returns the ignore-externals switch
261
     */
262 0
    public function getIgnoreExternals()
263
    {
264 0
        return isset($this->svnSwitches['ignore-externals']) ? $this->svnSwitches['ignore-externals'] : '';
265
    }
266

267
    /**
268
     * Sets the trust-server-cert switch
269
     *
270
     * @param $value
271
     */
272 0
    public function setTrustServerCert($value)
273
    {
274 0
        $this->svnSwitches['trust-server-cert'] = $value;
275
    }
276

277
    /**
278
     * Returns the trust-server-cert switch
279
     */
280 0
    public function getTrustServerCert()
281
    {
282 0
        return isset($this->svnSwitches['trust-server-cert']) ? $this->svnSwitches['trust-server-cert'] : '';
283
    }
284

285
    /**
286
     * Sets the config-option switch
287
     *
288
     * @param $value
289
     */
290 0
    public function setConfigOption($value)
291
    {
292 0
        $this->configOption = $value;
293
    }
294

295
    /**
296
     * Returns the config-option switch
297
     */
298 0
    public function getConfigOption()
299
    {
300 0
        return $this->configOption;
301
    }
302

303
    /**
304
     * Creates a VersionControl_SVN class based on $mode
305
     *
306
     * @param  string The SVN mode to use (info, export, checkout, ...)
307
     * @throws BuildException
308
     */
309 0
    protected function setup($mode)
310
    {
311 0
        $this->mode = $mode;
312

313
        // Set up runtime options. Will be passed to all
314
        // subclasses.
315 0
        $options = ['fetchmode' => $this->fetchMode];
316

317 0
        if ($this->oldVersion) {
318 0
            $options['svn_path'] = $this->getSvnPath();
319
        } else {
320 0
            $options['binaryPath'] = $this->getSvnPath();
321
        }
322

323 0
        if ($this->configOption) {
324 0
            $options['configOption'] = $this->configOption;
325
        }
326

327
        // Pass array of subcommands we need to factory
328 0
        $this->svn = VersionControl_SVN::factory($mode, $options);
329

330 0
        if (get_parent_class($this->svn) !== 'VersionControl_SVN_Command') {
331 0
            $this->oldVersion = true;
332 0
            $this->svn->use_escapeshellcmd = false;
333
        }
334

335 0
        if (!empty($this->repositoryUrl)) {
336 0
            $this->svnArgs = [$this->repositoryUrl];
337
        } else {
338 0
            if (!empty($this->workingCopy)) {
339 0
                if (is_dir($this->workingCopy)) {
340 0
                    $this->svnArgs = [$this->workingCopy];
341
                } else {
342 0
                    if ($mode == 'info') {
343 0
                        if (is_file($this->workingCopy)) {
344 0
                            $this->svnArgs = [$this->workingCopy];
345
                        } else {
346 0
                            throw new BuildException("'" . $this->workingCopy . "' is not a directory nor a file");
347
                        }
348
                    } else {
349 0
                        throw new BuildException("'" . $this->workingCopy . "' is not a directory");
350
                    }
351
                }
352
            }
353
        }
354
    }
355

356
    /**
357
     * Executes the constructed VersionControl_SVN instance
358
     *
359
     * @param    array $args
360
     * @param    array $switches
361
     * @throws   BuildException
362
     * @internal param Additional $array arguments to pass to SVN.
363
     * @internal param Switches $array to pass to SVN.
364
     * @return   string Output generated by SVN.
365
     */
366 0
    protected function run($args = [], $switches = [])
367
    {
368 0
        $tempArgs = array_merge($this->svnArgs, $args);
369 0
        $tempSwitches = array_merge($this->svnSwitches, $switches);
370

371 0
        if ($this->oldVersion) {
372 0
            $svnstack = \PEAR_ErrorStack::singleton('VersionControl_SVN');
373

374 0
            if ($output = $this->svn->run($tempArgs, $tempSwitches)) {
375 0
                return $output;
376
            }
377

378 0
            if (count($errs = $svnstack->getErrors())) {
379 0
                $err = current($errs);
380 0
                $errorMessage = $err['message'];
381

382 0
                if (isset($err['params']['errstr'])) {
383 0
                    $errorMessage = $err['params']['errstr'];
384
                }
385

386 0
                throw new BuildException("Failed to run the 'svn " . $this->mode . "' command: " . $errorMessage);
387
            }
388
        } else {
389
            try {
390 0
                return $this->svn->run($tempArgs, $tempSwitches);
391 0
            } catch (Exception $e) {
392 0
                throw new BuildException("Failed to run the 'svn " . $this->mode . "' command: " . $e->getMessage());
393
            }
394
        }
395
    }
396
}

Read our documentation on viewing source code .

Loading