FMELfinderBundle: Modify the path dynamically

时间:2019-03-19 14:52:14

标签: symfony elfinder

I want to implement a function that allow me to dynamically modify the path under any part of the project just by calling the service: $this->get('my_elfinder_configurator')->setPath('...'); or by passing the path as an option to my ElFinderType::class I just started by creating my ConfigurationReader class to override the existent one and register it.

    my_elfinder_configurator:
        class:        TweetFieldTypeBundle\Service\ConfigurationReader
        arguments: ["%fm_elfinder%", "@request_stack", "@service_container"]

Any Idea how to create a setPath function that do the trick if is it possible !!

<?php declare(strict_types=1);

namespace TweetFieldTypeBundle\Service;

use FM\ElfinderBundle\Configuration\ElFinderConfigurationReader;
use FM\ElfinderBundle\Model\ElFinderConfigurationProviderInterface;
use FM\ElfinderBundle\Security\ElfinderSecurityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use League\Flysystem\AdapterInterface;
use Symfony\Component\HttpFoundation\Request;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Adapter\Ftp;


class ConfigurationReader extends ElFinderConfigurationReader
{
    /**
     * @var array $options
     */
    protected $options = array();

    /**
     * @var array $parameters
     */
    protected $parameters;

    /**
     * @var RequestStack
     */
    protected $requestStack;

    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @param $parameters
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
     * @param ContainerInterface $container
     */
    public function __construct($parameters, RequestStack $requestStack, ContainerInterface $container)
    {
        parent::__construct($parameters, $requestStack, $container);
    }

    /**
     * @param $instance
     *
     * @return array
     *
     * @throws \Exception
     */
    public function getConfiguration($instance)
    {
        $request                = $this->requestStack->getCurrentRequest();
        $efParameters           = $this->parameters;
        $parameters             = $efParameters['instances'][$instance];
        $options                = array();
        $options['corsSupport'] = $parameters['cors_support'];
        $options['debug']       = $parameters['connector']['debug'];
        $options['bind']        = $parameters['connector']['binds'];
        $options['plugins']     = $parameters['connector']['plugins'];
        $options['roots']       = array();

        foreach ($parameters['connector']['roots'] as $parameter) {
            $path              = $parameter['path'];
            $homeFolder        = $request->attributes->get('homeFolder');
            $pathAndHomeFolder = $homeFolder ? sprintf('%s/%s', $path, $homeFolder) : $path;

            if ($parameter['flysystem']['enabled']) {
                if ($parameter['flysystem']['filesystem']) {
                    $serviceName = $parameter['flysystem']['filesystem'];
                    $filesystem  = $this->getFlysystemFilesystem($serviceName);
                } else {
                    $adapter     = $parameter['flysystem']['type']; // ftp ex.
                    $serviceName = $parameter['flysystem']['adapter_service'];
                    $opt         = $parameter['flysystem']['options'];
                    $filesystem  = $this->configureFlysystem($opt, $adapter, $serviceName);
                }
            }
            $driver = $this->container->has($parameter['driver']) ? $this->container->get($parameter['driver']) : null;

            $driverOptions = array(
                'driver'            => $parameter['driver'],
                'service'           => $driver,
                'glideURL'          => $parameter['glide_url'],
                'glideKey'          => $parameter['glide_key'],
                'plugin'            => $parameter['plugins'],
                'path'              => $pathAndHomeFolder,
                'startPath'         => $parameter['start_path'],
                'encoding'          => $parameter['encoding'],
                'URL'               => $this->getURL($parameter, $request, $homeFolder, $path),
                'alias'             => $parameter['alias'],
                'mimeDetect'        => $parameter['mime_detect'],
                'mimefile'          => $parameter['mimefile'],
                'imgLib'            => $parameter['img_lib'],
                'tmbPath'           => $parameter['tmb_path'],
                'tmbPathMode'       => $parameter['tmb_path_mode'],
                'tmbURL'            => $parameter['tmb_url'],
                'tmbSize'           => $parameter['tmb_size'],
                'tmbCrop'           => $parameter['tmb_crop'],
                'tmbBgColor'        => $parameter['tmb_bg_color'],
                'copyOverwrite'     => $parameter['copy_overwrite'],
                'copyJoin'          => $parameter['copy_join'],
                'copyFrom'          => $parameter['copy_from'],
                'copyTo'            => $parameter['copy_to'],
                'uploadOverwrite'   => $parameter['upload_overwrite'],
                'uploadAllow'       => $parameter['upload_allow'],
                'uploadDeny'        => $parameter['upload_deny'],
                'uploadMaxSize'     => $parameter['upload_max_size'],
                'defaults'          => $parameter['defaults'],
                'attributes'        => $parameter['attributes'],
                'acceptedName'      => $parameter['accepted_name'],
                'disabled'          => $parameter['disabled_commands'],
                'treeDeep'          => $parameter['tree_deep'],
                'checkSubfolders'   => $parameter['check_subfolders'],
                'separator'         => $parameter['separator'],
                'timeFormat'        => $parameter['time_format'],
                'archiveMimes'      => $parameter['archive_mimes'],
                'archivers'         => $parameter['archivers'],
                'fileMode'          => $parameter['fileMode'],
            );

            if ($parameter['volume_id'] > 0) {
                $driverOptions['id'] = $parameter['volume_id'];
            }

            if (!$parameter['show_hidden']) {
                $driverOptions['accessControl'] = array($this, 'access');
            }

            if ($parameter['security_voter']) {
                /** @var ElfinderSecurityInterface $voter */
                $voter = $this->container->get($parameter['security_voter']);

                $driverOptions['disabled'] = $this->parseSecurityConfiguration($voter);
            }

            if ('Flysystem' == $parameter['driver']) {
                $driverOptions['filesystem'] = $filesystem;
            }
            $options['roots'][] = array_merge($driverOptions, $this->configureDriver($parameter));
        }

        return $options;
    }

    /**
     * @param $parameter
     * @param $request
     * @param $homeFolder
     * @param $path
     *
     * @return string
     */
    private function getURL($parameter, Request $request, $homeFolder, $path)
    {
        if (isset($parameter['url']) && $parameter['url']) {
            if (0 === strpos($parameter['url'], 'http')) {
                return $parameter['url'];
            }

            $path = $parameter['url'].'/'.$homeFolder;
        } else {
            $path = $path.'/'.$homeFolder;
        }
...
}

This is where I'm trying to modify my path:

    public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
    {
     $this->get('my_elfinder_configurator')->setPath('...'); //here
        $fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier);
        $fieldForm
            ->add(
                $formConfig->getFormFactory()
                    ->createBuilder()
                    ->create(
                        'value',
                        ElFinderType::class
                    )
                    ->getForm()
            );
    }

Any help would be appreciated and voted.

0 个答案:

没有答案