Skip to content

Commit 07ba2c3

Browse files
authored
use flysystem for storage (#374)
1 parent 484c32d commit 07ba2c3

File tree

11 files changed

+187
-293
lines changed

11 files changed

+187
-293
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrade Notes
22

3+
## Version 4.3.0
4+
- **[ENHANCEMENT]**: Use Flysystem for Storage [#334](https://github.com/dachcom-digital/pimcore-formbuilder/issues/334)
5+
- Remove `PIMCORE_SYSTEM_TEMP_DIRECTORY/formbuilder-cache` as it is no longer needed
6+
37
## Version 4.2.3
48
- **[BUGFIX]**: Fix field collection mapping [#370](https://github.com/dachcom-digital/pimcore-formbuilder/issues/370)
59

src/FormBuilderBundle/Event/OutputWorkflow/OutputWorkflowSignalsEvent.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,61 @@
77

88
class OutputWorkflowSignalsEvent extends Event
99
{
10+
protected string $channel;
1011
protected array $signals;
11-
protected ?\Throwable $exception;
12+
protected array $context;
1213

13-
public function __construct(array $signals, ?\Throwable $exception)
14+
public function __construct(string $channel, array $signals, array $context)
1415
{
16+
$this->channel = $channel;
1517
$this->signals = $signals;
16-
$this->exception = $exception;
18+
$this->context = $context;
19+
}
20+
21+
public function getChannel(): string
22+
{
23+
return $this->channel;
24+
}
25+
26+
public function hasContextItem(string $contextItem): bool
27+
{
28+
return array_key_exists($contextItem, $this->context);
29+
}
30+
31+
public function getContextItem(string $contextItem): mixed
32+
{
33+
if (!$this->hasContextItem($contextItem)) {
34+
return null;
35+
}
36+
37+
return $this->context[$contextItem];
1738
}
1839

1940
public function hasException(): bool
2041
{
21-
return $this->exception instanceof \Throwable;
42+
return $this->hasContextItem('exception') && $this->getContextItem('exception') instanceof \Throwable;
2243
}
2344

2445
public function hasGuardException(): bool
2546
{
26-
return $this->exception instanceof OutputWorkflow\GuardChannelException ||
27-
$this->exception instanceof OutputWorkflow\GuardOutputWorkflowException ||
28-
$this->exception instanceof OutputWorkflow\GuardStackedException;
47+
if (!$this->hasException()) {
48+
return false;
49+
}
50+
51+
$exception = $this->getContextItem('exception');
52+
53+
return $exception instanceof OutputWorkflow\GuardChannelException ||
54+
$exception instanceof OutputWorkflow\GuardOutputWorkflowException ||
55+
$exception instanceof OutputWorkflow\GuardStackedException;
2956
}
3057

3158
public function getException(): ?\Throwable
3259
{
33-
return $this->exception;
60+
if (!$this->hasException()) {
61+
return null;
62+
}
63+
64+
return $this->getContextItem('exception');
3465
}
3566

3667
/**

src/FormBuilderBundle/EventListener/Core/CleanUpListener.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,45 @@
22

33
namespace FormBuilderBundle\EventListener\Core;
44

5-
use FormBuilderBundle\Tool\FileLocator;
5+
use Carbon\Carbon;
6+
use League\Flysystem\FilesystemOperator;
7+
use League\Flysystem\StorageAttributes;
68
use Pimcore\Logger;
79
use Pimcore\Maintenance\TaskInterface;
810

911
class CleanUpListener implements TaskInterface
1012
{
11-
protected FileLocator $fileLocator;
12-
13-
public function __construct(FileLocator $fileLocator)
14-
{
15-
$this->fileLocator = $fileLocator;
13+
public function __construct(
14+
protected FilesystemOperator $formBuilderChunkStorage,
15+
protected FilesystemOperator $formBuilderFilesStorage,
16+
) {
1617
}
1718

1819
public function execute(): void
1920
{
20-
foreach ($this->fileLocator->getFolderContent($this->fileLocator->getFilesFolder()) as $file) {
21-
Logger::log('Remove form builder files folder: ' . $file);
22-
$this->fileLocator->removeDir($file->getPathname());
21+
$minimumModifiedDelta = Carbon::now()->subHour();
22+
23+
foreach ($this->formBuilderFilesStorage->listContents('/') as $file) {
24+
$this->remove($minimumModifiedDelta, $file);
25+
}
26+
27+
foreach ($this->formBuilderChunkStorage->listContents('/') as $file) {
28+
$this->remove($minimumModifiedDelta, $file);
2329
}
30+
}
2431

25-
foreach ($this->fileLocator->getFolderContent($this->fileLocator->getChunksFolder()) as $file) {
26-
Logger::log('Remove form builder chunk folder: ' . $file);
27-
$this->fileLocator->removeDir($file->getPathname());
32+
protected function remove(Carbon $minimumModifiedDelta, StorageAttributes $file): void
33+
{
34+
if (!$minimumModifiedDelta->greaterThan(Carbon::createFromTimestamp($file->lastModified()))) {
35+
return;
2836
}
2937

30-
foreach ($this->fileLocator->getFolderContent($this->fileLocator->getZipFolder()) as $file) {
31-
Logger::log('Remove form builder zip folder: ' . $file);
32-
$this->fileLocator->removeDir($file->getPathname());
38+
if ($file->isDir()) {
39+
$this->formBuilderFilesStorage->deleteDirectory($file->path());
40+
} else {
41+
$this->formBuilderFilesStorage->delete($file->path());
3342
}
43+
44+
Logger::log(sprintf('Removing outdated form builder tmp %s: %s', $file->isDir() ? 'directory' : 'file', $file->path()));
3445
}
3546
}

src/FormBuilderBundle/EventListener/Core/FunnelRouteListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ public function shutdownFunnelRequest(ResponseEvent $event): void
8585
return;
8686
}
8787

88-
$this->signalSubscribeHandler->broadcast();
88+
$isFunnelShutdownRequest = $this->funnelDataResolver->isFunnelShutdownRequest($event->getRequest());
8989

90-
if ($this->funnelDataResolver->isFunnelShutdownRequest($event->getRequest())) {
90+
$this->signalSubscribeHandler->broadcast(['funnel_shutdown' => $isFunnelShutdownRequest]);
91+
92+
if ($isFunnelShutdownRequest === true) {
9193
$this->funnelDataResolver->flushFunnelData($event->getRequest());
9294
}
9395
}

src/FormBuilderBundle/EventSubscriber/SignalSubscribeHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public function listen(string $channel, array $context = []): void
6565

6666
public function broadcast(array $context = []): void
6767
{
68-
if ($this->channel === null) {
68+
$channelName = $this->channel;
69+
70+
if ($channelName === null) {
6971
return;
7072
}
7173

@@ -74,8 +76,9 @@ public function broadcast(array $context = []): void
7476

7577
$this->eventDispatcher->dispatch(
7678
new OutputWorkflowSignalsEvent(
79+
$channelName,
7780
$this->signalStorage->getSignals(),
78-
$context['exception'] ?? null
81+
$context
7982
),
8083
FormBuilderEvents::OUTPUT_WORKFLOW_SIGNALS
8184
);

src/FormBuilderBundle/OutputWorkflow/Channel/Email/Parser/MailParser.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FormBuilderBundle\OutputWorkflow\Channel\Email\Parser;
44

55
use FormBuilderBundle\Stream\File;
6+
use League\Flysystem\FilesystemOperator;
67
use Pimcore\Mail;
78
use Pimcore\Model\Document\Email;
89
use Symfony\Component\Form\FormInterface;
@@ -13,18 +14,12 @@
1314

1415
class MailParser
1516
{
16-
protected EngineInterface $templating;
17-
protected FormValuesOutputApplierInterface $formValuesOutputApplier;
18-
protected PlaceholderParserInterface $placeholderParser;
19-
2017
public function __construct(
21-
EngineInterface $templating,
22-
FormValuesOutputApplierInterface $formValuesOutputApplier,
23-
PlaceholderParserInterface $placeholderParser
18+
protected EngineInterface $templating,
19+
protected FormValuesOutputApplierInterface $formValuesOutputApplier,
20+
protected PlaceholderParserInterface $placeholderParser,
21+
protected FilesystemOperator $formBuilderFilesStorage
2422
) {
25-
$this->templating = $templating;
26-
$this->formValuesOutputApplier = $formValuesOutputApplier;
27-
$this->placeholderParser = $placeholderParser;
2823
}
2924

3025
/**
@@ -153,7 +148,7 @@ protected function parseMailAttachment(Mail $mail, array $attachments): void
153148
/** @var File $attachmentFile */
154149
foreach ($attachments as $attachmentFile) {
155150
try {
156-
$mail->attach(file_get_contents($attachmentFile->getPath()), $attachmentFile->getName());
151+
$mail->attach($this->formBuilderFilesStorage->read($attachmentFile->getPath()), $attachmentFile->getName());
157152
} catch (\Exception $e) {
158153
// fail silently.
159154
}

src/FormBuilderBundle/Resources/config/pimcore/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ parameters:
3737
- ['onselect','onselect']
3838
form_builder_field_attributes: '%form_builder_form_attributes%'
3939

40+
flysystem:
41+
storages:
42+
form_builder.chunk.storage:
43+
adapter: 'local'
44+
options:
45+
directory: '%kernel.project_dir%/var/tmp/form-builder-chunks'
46+
form_builder.files.storage:
47+
adapter: 'local'
48+
options:
49+
directory: '%kernel.project_dir%/var/tmp/form-builder-files'
50+
4051
form_builder:
4152
area:
4253
presets: ~

src/FormBuilderBundle/Resources/config/services/system.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ services:
1010
arguments:
1111
$bundle: "@=service('kernel').getBundle('FormBuilderBundle')"
1212

13-
# tool: file locator
14-
FormBuilderBundle\Tool\FileLocator: ~
15-
1613
# tool: file locator
1714
FormBuilderBundle\Tool\FormDependencyLocator: ~
1815

src/FormBuilderBundle/Stream/AttachmentStream.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Doctrine\DBAL\Query\QueryBuilder;
66
use FormBuilderBundle\Event\OutputWorkflow\OutputWorkflowSignalEvent;
77
use FormBuilderBundle\Event\OutputWorkflow\OutputWorkflowSignalsEvent;
8-
use FormBuilderBundle\Tool\FileLocator;
8+
use FormBuilderBundle\EventSubscriber\SignalSubscribeHandler;
9+
use League\Flysystem\FilesystemOperator;
910
use Pimcore\Logger;
1011
use Pimcore\Model\Asset;
1112
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -15,13 +16,10 @@ class AttachmentStream implements AttachmentStreamInterface
1516
public const SIGNAL_CLEAN_UP = 'tmp_file_attachment_stream';
1617
protected const PACKAGE_IDENTIFIER = 'formbuilder_package_identifier';
1718

18-
protected EventDispatcherInterface $eventDispatcher;
19-
protected FileLocator $fileLocator;
20-
21-
public function __construct(EventDispatcherInterface $eventDispatcher, FileLocator $fileLocator)
22-
{
23-
$this->eventDispatcher = $eventDispatcher;
24-
$this->fileLocator = $fileLocator;
19+
public function __construct(
20+
protected EventDispatcherInterface $eventDispatcher,
21+
protected FilesystemOperator $formBuilderFilesStorage
22+
) {
2523
}
2624

2725
/**
@@ -46,7 +44,7 @@ public function createAttachmentAsset($data, $fieldName, $formName): ?Asset
4644

4745
$packageIdentifier = '';
4846
foreach ($fileStack->getFiles() as $file) {
49-
$packageIdentifier .= sprintf('%s-%s-%s-%s', filesize($file->getPath()), $file->getId(), $file->getPath(), $file->getName());
47+
$packageIdentifier .= sprintf('%s-%s-%s-%s', $this->formBuilderFilesStorage->fileSize($file->getPath()), $file->getId(), $file->getPath(), $file->getName());
5048
}
5149

5250
// create package identifier to check if we just in another channel
@@ -55,7 +53,7 @@ public function createAttachmentAsset($data, $fieldName, $formName): ?Asset
5553
$formName = \Pimcore\File::getValidFilename($formName);
5654
$zipKey = substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyz'), 0, 5);
5755
$zipFileName = sprintf('%s-%s.zip', \Pimcore\File::getValidFilename($fieldName), $zipKey);
58-
$zipPath = sprintf('%s/%s', $this->fileLocator->getZipFolder(), $zipFileName);
56+
$zipPath = sprintf('%s/%s', PIMCORE_SYSTEM_TEMP_DIRECTORY, $zipFileName);
5957

6058
$existingAssetPackage = $this->findExistingAssetPackage($packageIdentifier, $formName);
6159

@@ -68,7 +66,7 @@ public function createAttachmentAsset($data, $fieldName, $formName): ?Asset
6866
$zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
6967

7068
foreach ($fileStack->getFiles() as $file) {
71-
$zip->addFile($file->getPath(), $file->getName());
69+
$zip->addFromString($file->getName(), $this->formBuilderFilesStorage->read($file->getPath()));
7270
}
7371

7472
$zip->close();
@@ -145,7 +143,18 @@ public function createAttachmentAsset($data, $fieldName, $formName): ?Asset
145143
*/
146144
public function cleanUp(OutputWorkflowSignalsEvent $signalsEvent): void
147145
{
148-
// keep assets if guard exception occurs: user may want to retry!
146+
// keep assets:
147+
// - if broadcasting channel is initiating funnel
148+
// - if broadcasting channel is processing funnel and not done yet
149+
// - if guard exception occurs: user may want to retry!
150+
151+
if ($signalsEvent->getChannel() === SignalSubscribeHandler::CHANNEL_FUNNEL_INITIATE) {
152+
return;
153+
}
154+
155+
if ($signalsEvent->getChannel() === SignalSubscribeHandler::CHANNEL_FUNNEL_PROCESS && $signalsEvent->getContextItem('funnel_shutdown') === false) {
156+
return;
157+
}
149158

150159
if ($signalsEvent->hasGuardException() === true) {
151160
return;
@@ -166,28 +175,21 @@ public function cleanUp(OutputWorkflowSignalsEvent $signalsEvent): void
166175

167176
protected function removeAttachmentFile(File $attachmentFile): void
168177
{
169-
$targetFolder = $this->fileLocator->getFilesFolder();
170-
$target = implode(DIRECTORY_SEPARATOR, [$targetFolder, $attachmentFile->getId()]);
171-
172-
if (!is_dir($target)) {
173-
return;
178+
if ($this->formBuilderFilesStorage->directoryExists($attachmentFile->getId())) {
179+
$this->formBuilderFilesStorage->deleteDirectory($attachmentFile->getId());
174180
}
175-
176-
$this->fileLocator->removeDir($target);
177181
}
178182

179183
protected function extractFiles(array $data): FileStack
180184
{
181185
$files = new FileStack();
182186
foreach ($data as $fileData) {
183-
184187
$fileId = (string) $fileData['id'];
185-
$fileDir = sprintf('%s/%s', $this->fileLocator->getFilesFolder(), $fileId);
186-
187-
if (is_dir($fileDir)) {
188-
$dirFiles = glob($fileDir . '/*');
189-
if (count($dirFiles) === 1) {
190-
$files->addFile(new File($fileId, $fileData['fileName'], $dirFiles[0]));
188+
if ($this->formBuilderFilesStorage->directoryExists($fileId)) {
189+
$dirFiles = $this->formBuilderFilesStorage->listContents($fileId);
190+
$flyFiles = iterator_to_array($dirFiles->getIterator());
191+
if (count($flyFiles) === 1) {
192+
$files->addFile(new File($fileId, $fileData['fileName'], $flyFiles[0]->path()));
191193
}
192194
}
193195
}

0 commit comments

Comments
 (0)