artgris / FileManagerBundle

FileManager is a simple Multilingual File Manager Bundle for Symfony

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Directory Tree Renderer

mogilvie opened this issue · comments

Hi,
I'd like to use the bundle for a multi-user application. I'd like to keep the actual director folder name as something immutable, such as a user UUID number, but display the directory folder as the user name .

There are several means of accomplishing this, and I would be happy to do a pull request with changes.
I'm wondering if you have a preferred method?

Regards,
Mark

Hi @mogilvie , Do you have a method in mind? I just want to keep my bundle simple and not be dependent on a database.

Add an additional array item for the directory list called 'fileSystemText, as opposed to 'text', which would be used for display in the directory tree tree. If fileSystemText was empty or unset then 'text' would be used.

Add an event dispatcher that allows the main application manage its own DB or logic to set the fileSystemName based on the Finder object.

An event dispatcher is a good idea and very flexible 🤔 👍

The way we solved this is indeed with a listener;

<?php
/**
 * Class FileManagerSubscriber
 */
class FileManagerSubscriber implements EventSubscriberInterface
{
    /**
     * @param GenericEvent $event
     *
     * @return void
     */
    public function onDirectoriesScanned(GenericEvent $event): void
    {
        /** @var Finder $finder */
        $finder = $event->getArgument('finder');

        // @todo Create this method to return the paths you want to present to your user as an array
        $allowedPaths = $this->getAllowedPathsArray();

        $finder->filter(function (\SplFileInfo $file) use ($allowedPaths) {
            if (in_array($file->getRealPath(), $allowedPaths, false)) {
                return true;
            }

            return false;
        });
    }

    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents(): array
    {
        return [
            FileManagerEvents::POST_DIRECTORY_FILTER_CONFIGURATION => 'onDirectoriesScanned',
        ];
    }
}

Thankyou @Martin1982 , that's a great suggestion to use the FileManagerEvent.