ADmad / cakephp-glide

CakePHP plugin for using Glide image manipulation library
MIT License
34 stars 11 forks source link

How to get the server instance to delete Glide cache ? #34

Closed infoliv closed 2 years ago

infoliv commented 2 years ago

Thank you for this plugin !

However I'm looking for a way to delete Glide cache...

My plugin FileManager triggers an event 'afterDelete' when the image file is deleted :

// in FileManager.FichiersTable
public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options)
{
    $fichier = new File(WWW_ROOT . $entity->url);
    $fichier->delete();

    $afterDeleteEvent = new Event('FileManager.Fichiers.afterDelete', $this, [
        'file' => $entity
    ]);
    $this->getEventManager()->dispatch($afterDeleteEvent);
}

and I want to catch the event in bootstrap of my application to delete the cache of the image :

//in bootstrap.php
EventManager::instance()
    ->on(
        'FileManager.Fichiers.afterDelete',
        function (Event $event) {
            // Delete cache of $event->getData('file')
            $filename = $event->getData('file')->filename;
            $server->deleteCache($filename); // How to get the instance $server ???
        }
    );

But I don't know how to get the Glide Server instance ?

Would it be possible to had a way to get server instance or a functionality to delete Glide cache ?

ADmad commented 2 years ago

You can just store the Glide server config that you use for the middleware in Configure and then read the same config in your event listener and create a new server instance yourself.

infoliv commented 2 years ago

Now I can get the server instance this way, thanks ! (But the cacheDelete() is not working with me... it returns true but the directory is not deleted...)

ADmad commented 2 years ago

Well you'll have to figure it out yourself why the deletion not working :).

impronta48 commented 6 months ago

Now I can get the server instance this way, thanks ! (But the cacheDelete() is not working with me... it returns true but the directory is not deleted...)

Did you manage to solve? Can you show an example?

infoliv commented 6 months ago

@impronta48 In bootstrap :

Configure::write('ADmad/Glide.server', [
            // Path or League\Flysystem adapter instance to read images from.
            // http://glide.thephpleague.com/1.0/config/source-and-cache/
            'source' => WWW_ROOT,

            // Path or League\Flysystem adapter instance to write cached images to.
            'cache' => WWW_ROOT . 'files' . DS . 'cache',

            // URL part to be omitted from source path. Defaults to "/images/"
            // http://glide.thephpleague.com/1.0/config/source-and-cache/#set-a-base-url
            'base_url' => '/images/',

            // Response class for serving images. If unset (default) an instance of
            // \ADmad\Glide\Response\PsrResponseFactory() will be used.
            // http://glide.thephpleague.com/1.0/config/responses/
            'response' => null,
        ]);

EventManager::instance()
    ->on(
        'FileManager.Fichiers.afterDelete',
        function (Event $event) {
            $server = \League\Glide\ServerFactory::create(Configure::read('ADmad/Glide.server'));
            $file = $event->getData('file');
            $server->deleteCache(Configure::read('ADmad/Glide.server.base_url') . $file->url);

        }
    );