Ticket #15: FtpDeploy.php

File FtpDeploy.php, 3.9 KB (added by Jorrit Schippers <jorrit@…>, 2 years ago)

FtpDeploy task

Line 
1<?php
2// warning: we get problems with PEAR bug http://pear.php.net/bugs/bug.php?id=7270
3
4// TODO : documentation
5
6require_once 'phing/Task.php';
7require_once 'PEAR.php';
8
9class FtpDeploy extends Task {
10        public $Taskname = 'ftpdeploy';
11       
12        private $host = null;
13        private $port = 0;
14        private $username = null;
15        private $password = null;
16        private $dir = null;
17        private $filesets;
18        private $completeDirMap;
19        private $mode = null;
20        private $clearFirst = false;
21       
22        public function __construct() {
23                $this->filesets = array();
24                $this->completeDirMap = array();
25        }
26       
27        public function setHost($host) {
28                $this->host = $host;
29        }
30       
31        public function setPort($port) {
32                $this->port = (int) $port;
33        }
34       
35        public function setUsername($username) {
36                $this->username = $username;
37        }
38       
39        public function setPassword($password) {
40                $this->password = $password;
41        }
42       
43        public function setDir($dir) {
44                $this->dir = $dir;
45        }
46       
47        public function setMode($mode) {
48                switch(strtolower($mode)) {
49                        case 'ascii':
50                                $this->mode = FTP_ASCII;
51                                break;
52                        case 'binary':
53                        case 'bin':
54                                $this->mode = FTP_BINARY;
55                                break;
56                }
57        }
58       
59        public function setClearFirst($clearFirst) {
60                $this->clearFirst = (bool) $clearFirst;
61        }
62       
63        function createFileSet() {
64                $num = array_push($this->filesets, new FileSet());
65                return $this->filesets[$num-1];
66        }
67       
68        /**
69         * The init method: check if Net_FTP is available
70         */
71        public function init() {
72                $paths = explode(PATH_SEPARATOR, get_include_path());
73                foreach($paths as $path) {
74                        if(file_exists($path.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR.'FTP.php')) {
75                                return true;
76                        }
77                }
78                throw new BuildException('The FTP Deploy task requires the Net_FTP PEAR package.');
79        }
80       
81        /**
82         * The main entry point method.
83         */
84        public function main() {
85                $project = $this->getProject();
86               
87                require_once 'Net/FTP.php';
88                $ftp = new Net_FTP($this->host, $this->port);
89                $ret = $ftp->connect();
90                if(PEAR::isError($ret))
91                        throw new BuildException('Could not connect to FTP server '.$this->post.' on port '.$this->port.': '.$ret->getMessage());
92                $ret = $ftp->login($this->username, $this->password);
93                if(PEAR::isError($ret))
94                        throw new BuildException('Could not login to FTP server '.$this->post.' on port '.$this->port.' with username '.$this->username.': '.$ret->getMessage());
95               
96                if($this->clearFirst) {
97                        // TODO change to a loop through all files and directories within current directory
98                        $this->log('Clearing directory '.$this->dir, Project::MSG_INFO);
99                        $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir.'/';
100                        $ftp->rm($dir, true);
101                        $ftp->mkdir($dir);
102                }
103               
104                $ret = $ftp->cd($this->dir);
105                if(PEAR::isError($ret))
106                        throw new BuildException('Could not change to directory '.$this->dir.': '.$ret->getMessage());
107               
108                $fs = FileSystem::getFileSystem();
109                $convert = $fs->getSeparator() == '\\';
110               
111                foreach($this->filesets as $fs) {
112                        $ds = $fs->getDirectoryScanner($project);
113                        $fromDir  = $fs->getDir($project);
114                        $srcFiles = $ds->getIncludedFiles();
115                        $srcDirs  = $ds->getIncludedDirectories();
116                        foreach($srcDirs as $dirname) {
117                                if($convert)
118                                        $dirname = str_replace('\\', '/', $dirname);
119                                $this->log('Will create directory '.$dirname, Project::MSG_VERBOSE);
120                                $ret = $ftp->mkdir($dirname, true);
121                                if(PEAR::isError($ret))
122                                        throw new BuildException('Could not create directory '.$dirname.': '.$ret->getMessage());
123                        }
124                        foreach($srcFiles as $filename) {
125                                $file = new PhingFile($fromDir->getAbsolutePath(), $filename);
126                                if($convert)
127                                        $filename = str_replace('\\', '/', $filename);
128                                $this->log('Will copy '.$file->getCanonicalPath().' to '.$filename, Project::MSG_VERBOSE);
129                                $ret = $ftp->put($file->getCanonicalPath(), $filename, true, $this->mode);
130                                if(PEAR::isError($ret))
131                                        throw new BuildException('Could not deploy file '.$filename.': '.$ret->getMessage());
132                        }
133                }
134               
135                $ftp->disconnect();
136        }
137}
138?>