Filesystem - Flysystem

Overview

Plugins often need the ability to read and write files. Thanks to the Flysystem that Shopware uses, this can be managed very easily. It does not matter whether the files are stored on the local file system or at a cloud provider. The read and write access remains the same. If you want to learn more about the configuration of the file system in Shopware, have a look at the filesystem guide. There you will learn how to outsource the file system to the Amazon cloud, for example. In a plugin we don't have to worry about the configuration and can use the advantages of the Flysystem directly.

Prerequisites

This guide is built upon both the Plugin base guide as well as the Add custom service guide.

Flysystem overview

The Flysystem enables your plugin to read and write files through a common interface. There are several default namespaces/directories that are available, for example:
  • One for general private files of the shop: invoices, delivery notes
  • One for general public files: product pictures, media files
  • One for theme files
  • One for sitemap files
  • One for bundle assets files
However, every plugin/bundle gets an own namespace that should be used for private or public plugin files. These are automatically generated during the plugin installation. The namespace is prefixed with the Snake case plugin name followed by filesystem . private or plugin. For our example plugin this would be
  • swag_example_plugin.filesystem.public for public plugin files
  • swag_example_plugin.filesystem.private for private plugin files

Use filesystem in a service

To make use of the filesystem we register a new service, which helps to read and write files to the filesystem.
<plugin root>/src/Service/ExampleFilesystemService.php
1
Copied!
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Service;
4
5
use League\Flysystem\FilesystemInterface;
6
7
class ExampleFilesystemService
8
{
9
/**
10
* @var FilesystemInterface
11
*/
12
private FilesystemInterface $fileSystemPublic;
13
/**
14
* @var FilesystemInterface
15
*/
16
private FilesystemInterface $fileSystemPrivate;
17
18
/**
19
* ExampleFilesystemService constructor.
20
* @param FilesystemInterface $fileSystemPublic
21
* @param FilesystemInterface $fileSystemPrivate
22
*/
23
public function __construct(FilesystemInterface $fileSystemPublic, FilesystemInterface $fileSystemPrivate)
24
{
25
$this->fileSystemPublic = $fileSystemPublic;
26
$this->fileSystemPrivate = $fileSystemPrivate;
27
}
28
29
public function readPrivateFile(string $filename) {
30
return $this->fileSystemPrivate->read($filename);
31
}
32
33
public function writePrivateFile(string $filename, string $content) {
34
$this->fileSystemPrivate->write($filename, $content);
35
}
36
37
public function listPublicFiles(): array {
38
return $this->fileSystemPublic->listContents();
39
}
40
}
Copied!
This service makes use of the private und public filesystem of the plugin. As you already know, this php class has to be registered as a service in the dependency injection container. This is also the place where we define which filesystem will be handed over to the constructor. To make use of the plugin private and public files, the service definition could look like this:
<plugin root>/src/Resources/config/services.xml
1
Copied!
1
<?xml version="1.0" ?>
2
<container xmlns="http://symfony.com/schema/dic/services"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5
6
<services>
7
<service id="Swag\BasicExample\Service\ExampleFilesystemService">
8
<argument type="service" id="swag_basic_example.filesystem.public"/>
9
<argument type="service" id="swag_basic_example.filesystem.private"/>
10
<!--
11
There are also predefined file system services
12
<argument type="service" id="shopware.filesystem.private"/>
13
<argument type="service" id="shopware.filesystem.public"/>
14
-->
15
</service>
16
</services>
17
</container>
Copied!
Now, this service can be used to read or write files to the private plugin filesystem or to list all files in the public plugin filesystem. You should visit the Flysystem API documentation for more information.
Last modified 1d ago