FtpConnectionTrait uses \oihana\logging\LoggerTrait
Provides the connection lifecycle of the {@see \oihana\ftp\FtpClient}: configuration, connect/login with retry, passive mode, secure transport and clean teardown.
All transport calls go through an injected FtpDriverInterface, so the whole lifecycle is testable without a live server.
Tags
Table of Contents
Properties
- $maxRetries : int
- The maximum number of attempts for a transient (connection) failure.
- $connected : bool
- Whether the client is currently connected.
- $credentials : FtpCredentials
- The login credentials.
- $driver : FtpDriverInterface
- The transport driver.
- $host : string
- The remote host.
- $passive : bool
- Whether passive mode is enabled.
- $port : int
- The control-channel port.
- $root : string
- The remote base directory entered right after login (empty to stay in the default directory).
- $secure : bool
- Whether the transport is secured (FTPS).
- $security : string
- The transport security mode.
- $timeout : int
- The connection timeout, in seconds.
- $transferMode : string
- The default transfer mode applied to file operations.
Methods
- __construct() : mixed
- Creates a new FTP client.
- __destruct() : mixed
- Closes the connection on destruction.
- connect() : static
- Opens the connection and authenticates, retrying transient failures with exponential backoff. A no-op when already connected.
- disconnect() : static
- Closes the connection if it is open.
- getCredentials() : FtpCredentials
- Returns the configured login credentials.
- getHost() : string
- Returns the configured remote host.
- getMaxRetries() : int
- Returns the configured maximum number of connection attempts.
- getPort() : int
- Returns the configured control-channel port.
- getRoot() : string
- Returns the configured remote root directory.
- getTimeout() : int
- Returns the configured connection timeout.
- getTransferMode() : string
- Returns the default transfer mode applied to file operations.
- isConnected() : bool
- Indicates whether a connection is currently open.
- isPassive() : bool
- Indicates whether passive mode is enabled.
- isSecure() : bool
- Indicates whether the transport is secured (FTPS).
- waitBeforeRetry() : void
- Pauses between two connection attempts. Isolated so tests can override it without changing the retry logic.
- applyRoot() : void
- Changes into the configured root directory, if any, right after connecting.
- ensureConnected() : void
- Asserts that the client holds an open connection.
- establish() : void
- Performs a single connect + authenticate sequence.
- initializeConfig() : void
- Reads the configuration array into the connection state.
Properties
$maxRetries
The maximum number of attempts for a transient (connection) failure.
public
int
$maxRetries
= 3
$connected
Whether the client is currently connected.
private
bool
$connected
= false
$credentials
The login credentials.
private
FtpCredentials
$credentials
$driver
The transport driver.
private
FtpDriverInterface
$driver
$host
The remote host.
private
string
$host
= ''
$passive
Whether passive mode is enabled.
private
bool
$passive
= true
$port
The control-channel port.
private
int
$port
= 21
$root
The remote base directory entered right after login (empty to stay in the default directory).
private
string
$root
= ''
$secure
Whether the transport is secured (FTPS).
private
bool
$secure
= false
$security
The transport security mode.
private
string
$security
= \oihana\ftp\enums\FtpSecurity::NONE
$timeout
The connection timeout, in seconds.
private
int
$timeout
= 90
$transferMode
The default transfer mode applied to file operations.
private
string
$transferMode
= \oihana\ftp\enums\FtpTransferMode::BINARY
Methods
__construct()
Creates a new FTP client.
public
__construct([array<string|int, mixed>|FtpOptions $init = [] ][, FtpDriverInterface|null $driver = null ][, ContainerInterface|null $container = null ]) : mixed
Parameters
- $init : array<string|int, mixed>|FtpOptions = []
-
Configuration keyed by Ftp constants (or an FtpOptions instance):
host,port,username,password,security,passive,timeout,maxRetries, plus any logger options. - $driver : FtpDriverInterface|null = null
-
The transport driver. Defaults to a NativeFtpDriver.
- $container : ContainerInterface|null = null
-
Optional PSR-11 container used to resolve a logger service.
Tags
__destruct()
Closes the connection on destruction.
public
__destruct() : mixed
connect()
Opens the connection and authenticates, retrying transient failures with exponential backoff. A no-op when already connected.
public
connect() : static
Tags
Return values
static —This instance, for chaining.
disconnect()
Closes the connection if it is open.
public
disconnect() : static
Return values
static —This instance, for chaining.
getCredentials()
Returns the configured login credentials.
public
getCredentials() : FtpCredentials
Return values
FtpCredentials —The credentials holder.
getHost()
Returns the configured remote host.
public
getHost() : string
Return values
string —The host name or IP address.
getMaxRetries()
Returns the configured maximum number of connection attempts.
public
getMaxRetries() : int
Return values
int —The retry ceiling.
getPort()
Returns the configured control-channel port.
public
getPort() : int
Return values
int —The port number.
getRoot()
Returns the configured remote root directory.
public
getRoot() : string
Return values
string —The root directory, or an empty string when none is set.
getTimeout()
Returns the configured connection timeout.
public
getTimeout() : int
Return values
int —The timeout, in seconds.
getTransferMode()
Returns the default transfer mode applied to file operations.
public
getTransferMode() : string
Return values
string —One of the FtpTransferMode constants.
isConnected()
Indicates whether a connection is currently open.
public
isConnected() : bool
Return values
bool —True when the client is connected.
isPassive()
Indicates whether passive mode is enabled.
public
isPassive() : bool
Return values
bool —True when passive mode is used.
isSecure()
Indicates whether the transport is secured (FTPS).
public
isSecure() : bool
Return values
bool —True when TLS is used.
waitBeforeRetry()
Pauses between two connection attempts. Isolated so tests can override it without changing the retry logic.
protected
waitBeforeRetry(int $seconds) : void
Parameters
- $seconds : int
-
The number of seconds to wait.
Tags
applyRoot()
Changes into the configured root directory, if any, right after connecting.
private
applyRoot() : void
Tags
ensureConnected()
Asserts that the client holds an open connection.
private
ensureConnected() : void
Shared by the file and directory operations.
Tags
establish()
Performs a single connect + authenticate sequence.
private
establish() : void
Tags
initializeConfig()
Reads the configuration array into the connection state.
private
initializeConfig(array<string|int, mixed> $init) : void
Parameters
- $init : array<string|int, mixed>
-
The configuration keyed by Ftp constants.