1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204:
<?php
namespace Deliverist\Syncer;
use CzProject\Events\Events;
use CzProject\Logger\ILogger;
class FtpSyncer implements ISyncer
{
/** @var Events */
private $events;
/** @var string */
private $host;
/** @var string|NULL */
private $user;
/** @var string|NULL */
private $password;
/** @var string|NULL */
private $path;
/** @var int|NULL */
private $port;
/** @var bool */
private $passiveMode = TRUE;
/** @var bool */
private $ssl = TRUE;
/** @var array|NULL */
private $ignore;
/** @var array|NULL */
private $purge;
/** @var string|NULL */
private $deploymentFile;
/**
* @param string
* @param string|NULL
* @param string|NULL
* @param string|NULL
*/
public function __construct($host, $user = NULL, $password = NULL, $path = NULL)
{
$this->events = Utils\EventsFactory::create();
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->path = $path;
}
/**
* @param int|NULL
* @return self
*/
public function setPort($port)
{
$this->port = $port;
return $this;
}
/**
* @return self
*/
public function disablePassiveMode()
{
$this->passiveMode = FALSE;
return $this;
}
/**
* {@inheritDoc}
*/
public function disableSsl()
{
$this->ssl = FALSE;
return $this;
}
/**
* {@inheritDoc}
*/
public function getLabel()
{
return $this->getUrl(TRUE);
}
/**
* {@inheritDoc}
*/
public function onAfterUpload($handler)
{
$this->events->addHandler(Utils\EventsFactory::AFTER_SYNC, $handler);
return $this;
}
/**
* {@inheritDoc}
*/
public function setIgnore(array $ignore)
{
$this->ignore = $ignore;
return $this;
}
/**
* {@inheritDoc}
*/
public function setPurge(array $purge)
{
$this->purge = $purge;
return $this;
}
/**
* {@inheritDoc}
*/
public function setDeploymentFile($deploymentFile)
{
$this->deploymentFile = $deploymentFile;
return $this;
}
/**
* {@inheritDoc}
*/
public function sync($dir, ILogger $logger)
{
$server = new \Deployment\FtpServer($this->getUrl(), $this->passiveMode);
$deployer = Bridges\FtpDeployerFactory::create($dir, $logger, $server);
if ($this->deploymentFile !== NULL) {
$deployer->deploymentFile = $this->deploymentFile;
}
if ($this->ignore !== NULL) {
$deployer->ignoreMasks = $this->ignore;
}
if ($this->purge !== NULL) {
$deployer->toPurge = $this->purge;
}
$deployer->runAfter[] = function () {
$this->events->fireEvent(Utils\EventsFactory::AFTER_SYNC);
};
$deployer->deploy();
}
/**
* @return string
*/
private function getUrl($anonymized = FALSE)
{
$url = $this->ssl ? 'ftps' : 'ftp';
$url .= '://';
if (isset($this->user)) {
$url .= $anonymized ? '***' : rawurlencode($this->user);
}
if (isset($this->password)) {
$url .= ':' . ($anonymized ? '***' : rawurlencode($this->password));
}
if (isset($this->user) || isset($this->password)) {
$url .= '@';
}
$url .= $this->host;
if (isset($this->port)) {
$url .= ':' . $this->port;
}
$url .= '/';
if ($this->path) {
$url .= ltrim($this->path, '/');
}
return $url;
}
}