Skip to content

Command execution events + subrequests support + volume_id setting #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2015
Merged

Command execution events + subrequests support + volume_id setting #128

merged 1 commit into from
Jun 4, 2015

Conversation

nicolasmure
Copy link

Hi,

This PR provides events on elFinder command execution (pre and post).
It also allows to make subrequests to relaunch an elFinder command.
A new setting has been added : volume_id, necessary to keep the volume identifier when making subrequests on the same volume than the main request.

Related to # 19 on the lib side.

@nicolasmure
Copy link
Author

I've made this PR as I talk about in #126 (multiple thumbnails). In my opinion it looks better to do like this than in #126 .
This is how I handle multiple thumbnails with new events and LiipImagineBundle :

First I've set the cors_support option to true and set the volume_id to 1 in my main instance's root to make subrequests possible.
Then, I've made events listeners :

namespace AppBundle\EventListener\ElFinder;

use FM\ElfinderBundle\Event\ElFinderEvents;

abstract class ExecutionListener
{
    /**
     * Performs a subrequest to elFinderBundle to return informations.
     * @param  ElFinderEvents $event
     * @return array
     */
    protected function getInfos(ElFinderEvents $event)
    {
        $queryParameters = $event->getRequest()->query->all();
        $queryParameters['cmd'] = 'info';

        if (isset($queryParameters['target']) && !isset($queryParameters['targets'])) {
            $queryParameters['targets'] = array($queryParameters['target']);
            unset($queryParameters['target']);
        }

        $jsonResponse = $event->subRequest(array(
            'instance' => $event->getInstance(),
            'homeFolder' => $event->getHomeFolder()
        ), $queryParameters);

        $filesInfos = json_decode($jsonResponse->getContent());
        return $filesInfos->files;
    }
}
namespace AppBundle\EventListener\ElFinder;

use Liip\ImagineBundle\Controller\ImagineController;
use FM\ElfinderBundle\Event\ElFinderPostExecutionEvent;
use Symfony\Component\HttpFoundation\Request;

class PostExecutionListener extends ExecutionListener
{
    /**
     * LiipImagineBundle Controller
     * @var ImagineController
     */
    private $imagineController;


    /**
     * Constructor.
     * @param ImagineController $imagineController
     */
    public function __construct(ImagineController $imagineController)
    {
        $this->imagineController = $imagineController;
    }

    /**
     * Manages thumbnails creation.
     * @param  ElFinderPostExecutionEvent $event
     */
    public function onPostExecute(ElFinderPostExecutionEvent $event)
    {
        if (!$event->hasErrors() && $event->getCommand() == 'tmb') {
            $this->createThumbnails($event);
        }
    }

    /**
     * Creates picture's thumbnails with LiipImagineBundle.
     * @param ElFinderPostExecutionEvent $event
     */
    private function createThumbnails(ElFinderPostExecutionEvent $event)
    {
        $imagesInfos = $this->getInfos($event);
        $request = new Request();

        foreach ($imagesInfos as $imageInfo) {
            $path = '/'.$imageInfo->path;

            $this->imagineController->filterAction($request, $path, 'tmb_64');
            $this->imagineController->filterAction($request, $path, 'tmb_128');
        }
    }
}
namespace AppBundle\EventListener\ElFinder;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use FM\ElfinderBundle\Event\ElFinderPreExecutionEvent;

class PreExecutionListener extends ExecutionListener
{
    /**
     * LiipImagineBundle CacheManager
     * @var CacheManager
     */
    private $cacheManager;


    /**
     * Constructor.
     * @param CacheManager $cacheManager
     */
    public function __construct(CacheManager $cacheManager)
    {
        $this->cacheManager = $cacheManager;
    }

    /**
     * Manages thumbnails delete.
     * @param  ElFinderPreExecutionEvent $event
     */
    public function onPreExecute(ElFinderPreExecutionEvent $event)
    {
        $command = $event->getCommand();
        if ($command == 'rm' || $command == 'rename' || $command == 'resize') {
            $this->removeThumbnails($event);
        }
    }

    /**
     * Removes picture's thumbnails created with LiipImagineBundle.
     * @param ElFinderPreExecutionEvent $event
     */
    private function removeThumbnails(ElFinderPreExecutionEvent $event)
    {
        $imagesInfos = $this->getInfos($event);

        foreach ($imagesInfos as $imageInfo) {
            $path = '/'.$imageInfo->path;

            $this->cacheManager->remove($path);
        }
    }
}


return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this became incompatible with 2.3?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, compatible with ElFinderPHP pull request # 19

@nicolasmure
Copy link
Author

Hi @helios-ag ,

Do you have some news about it?
I think this PR could be used in favor of #126

@helios-ag
Copy link
Owner

Hi @nicolasmure, well it looks better of course. Anyway i support 2.3 in a separate branch. I could accept this if you can rebase PR. I will hold accepting PRs if you wish.

@nicolasmure
Copy link
Author

OK, I was just wondering if you were interested by this or not.
I've rebased on master, but I can still work from my fork using composer's vcs if you don't want to merge it now.
By the way, I think PR #126 could be closed.

Thank you 😉

helios-ag added a commit that referenced this pull request Jun 4, 2015
Command execution events + subrequests support + volume_id setting
@helios-ag helios-ag merged commit ff339d2 into helios-ag:master Jun 4, 2015
@nicolasmure
Copy link
Author

thank you @helios-ag 😉

please do not forget that this PR is related to PR # 19 on the lib side

@nicolasmure nicolasmure mentioned this pull request Jun 4, 2015
@helios-ag
Copy link
Owner

👍

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling b2bfad3 on nicolasmure:event-dispatcher into * on helios-ag:master*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants