OneupFlysystemBundle dynamically change/inject filesystems in Symfony

Technology:
Symfony 5
OneupFlysystemBundle

I don’t know how to dynamically change/inject file systems using Depency Ingection.
I need solution where $firstFilesystem in Service isn’t hardcoded but dependent on variable sended from controller

Thank you in advance for your help

This is my simplified project:

#   config/packages/oneup_filesystem.yaml
oneup_flysystem:
    adapters:
        first_adapter:
            local:
                directory: '%kernel.project_dir%/resources/storage_test_one'
        second_adapter:
            local:
                directory: '%kernel.project_dir%/resources/storage_test_second'
        third_adapter:
            local:
                directory: '%kernel.project_dir%/resources/storage_test_third'
    filesystems:
        first_filesystem:
            adapter: first_adapter
            alias: LeagueFlysystemFilesystem
        second_filesystem:
            adapter: second_adapter
            alias: LeagueFlysystemFilesystem
        third_filesystem:
            adapter: third_adapter
            alias: LeagueFlysystemFilesystem

namespace AppController;

use AppServiceStorageStorageConnector;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyBundleFrameworkBundleControllerAbstractController;

/**
 * @Route("/", name="home")
 */
class HomeTest extends AbstractController
{

    public $Storage;

    /**
     * @Route("/test/{storage}", name="test")
     */
    public function test($storage, StorageConnector $StorageConnector)
    {

        $this->Storage = $StorageConnector;

        return new JsonResponse([
            'status' => ['test'],
            'content' => $this->Storage->test_content(),
            ]);
    }
}

<?php
#   src/Service/Storage/StorageConnector.php

namespace AppServiceStorage;

use LeagueFlysystemFilesystemInterface;

class StorageConnector
{

    public $storageName;
    public $storageSystem;
    public $storageSyncReport = [];
    public $containerBuilder;

    public function __construct(FilesystemInterface $firstFilesystem)
    {
        $this->storageSystem = $firstFilesystem;
    }


    public function test_content()
    {
        return $this->storageSystem->read('test.md');
    }
}

everything is working, json reponse is:

{"status":["test"],"content":"this is content of file test.md in resources/storage_test"}

but can’t change filesystem

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *