Handle files using a transfer adapter
Overview
The Bundle offers the possibility to configure adapters for handling files beyond the file system. This enables alternative storage methods such as using an SFTP server.
This mainly affects storage of assets and JSON files for the export process.
Available adapters
Unless a different adapter is specified in the configuration, the NsfTransferAdapter is used by default!
By default, the bundle offers the following adapters:
NfsTransferAdapter
Identifier: 'nfs'
Class: Twocream\ShopwareConnectorBundle\Service\TransferAdapter\NfsTransferAdapter
Description: this is the default adapter, it handles files on the local file system
SftpTransferAdapter
Identifier: 'sftp'
Class: Twocream\ShopwareConnectorBundle\Service\TransferAdapter\SftpTransferAdapter
Description: This adapter enables file handling on an SFTP server
The connection data for the SFTP server can be configured using the following environment variables:
| Identifier | Description |
|---|---|
SW_CONNECTOR_TRANSFER_ADAPTER_SFTP_HOST | The host name of the SFTP server |
SW_CONNECTOR_TRANSFER_ADAPTER_SFTP_USERNAME | The user name used for authentication on the SFTP server |
SW_CONNECTOR_TRANSFER_ADAPTER_SFTP_PASSWORD | The password used for authentication on the SFTP server |
SW_CONNECTOR_TRANSFER_ADAPTER_SFTP_PRIVATE_KEY | The private key used for authentication on the SFTP server |
SW_CONNECTOR_TRANSFER_ADAPTER_SFTP_PASSPHRASE | The passphrase used to authenticate the private key |
SW_CONNECTOR_TRANSFER_ADAPTER_SFTP_PORT | The port to be used for connection (Default: 22) |
Create custom adapter
Adapters can be retrieved by their identifier using the following collection class: Twocream\ShopwareConnectorBundle\Service\TransferAdapter\TransferAdapterCollector
Interface
A TransferAdapter must implement the following interface: Twocream\ShopwareConnectorBundle\Service\TransferAdapter\TransferAdapterInterface.
Methods
The adapter must implement the following methods:
| Definition | Arguments | Description |
|---|---|---|
public static function getIdentifier(): string | No arguments | Returns the adapter's unique identifier (This is the value used in the configuration) |
public function exists(string $path): bool | $path: The path to be checked | Checks if the given path exists |
public function isDirectory(string $path): bool | $path: The path to be checked | Checks if the given path is a directory |
public function isFile(string $path): bool | $path: The path to be checked | Checks if the given path is a file |
public function isEmptyDirectory(string $path): bool | $path: The path to be checked | Checks if the given path is an empty directory |
public function isAbsolutePath(string $path): bool | $path: The path to be checked | Checks if the given path is absolute |
public function read(string $path): ?string | $path: The path to the file to be read | Reads the content of the given file and returns it |
public function dumpFile(string $filename, string $content): bool | $filename: The path to the file$content: The content to be written to the file | Writes the given content to the given file |
public function copy(string $source, string $target, bool $overwriteNewerFiles = false): bool | $source: The path to the file to copy$target: The path to copy the file to $overwriteNewerFiles: Determines whether to overwrite existing files | Copies the given file to the given path |
public function rename(string $origin, string $target, bool $overwrite = false): bool | $origin: The path to the file or directory to be renamed$target: The new name $overwrite: Determines whether to overwrite existing files with the same name | Renames the given file or directory to the given name |
public function remove(string $path): bool | $path: The path to the file or directory to be deleted | Deletes the given file or directory |
public function mkdir(string $dir, int $mode = 0777): bool | $dir: The directory to be created$mode: File permissions in octal format | Creates the given directory |
public function copyDirectory(string $sourceDir, string $destinationDir): bool | $sourceDir: The path to the directory to copy$destinationDir: The path to copy the directory to | Copies the given directory to the given path |
public function findFiles(string $pattern, string $path, ?int $depth = null, bool $sortByModifiedTime = false, bool $ignoreDotFiles = false): array | $pattern: The pattern to filter files by (RegEx or wildcards)$path: The path to the directory to be searched in$depth: Number of subdirectory levels to be searched$sortByModifiedTime: Determines whether to sort the files by last modified timestamp$ignoreDotFiles: Determines whether dotfiles (e.g., ., .., .env) should be ignored | Filters for files that match the given conditions (see arguments) and returns them as an array |
public function getFileModifiedTime(string $path): ?int | $path: The path to the file | Returns the last modified timestamp of the given file |
Registering the adapter
New adapters must be registered in a service configuration file. The service tag twocream.shopware_connector.transfer_adapter must be used.
services:
_defaults:
autowire: true
autoconfigure: true
public: false
twocream.shopware_connector.transfer_adapter_example:
public: true
class: App\Service\TransferAdapter\ExampleTransferAdapter
tags: [ 'twocream.shopware_connector.transfer_adapter' ]