如何在 cakephp4 中使用 Admad cakephp-glide 删除 Glide 缓存?
how to delete Glide cache using Admad cakephp-glide in cakephp4?
我在我的 Cakephp4 应用程序中使用插件 cakephp-glide。
我想在删除图片后立即删除图片的 Glide 缓存。
当图像文件被删除时,我的插件 FileManager
触发事件 'afterDelete' :
// 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);
}
我想在我的应用程序 bootstrap 中捕获事件以删除图像缓存:
//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 ???
}
);
但是我不知道如何获取 Glide Server 实例?
我在插件中找不到获取它的方法。
如果有人能帮助我...
根据 ADmad 的建议,解决方案是将 Glide 服务器配置存储在 Configure
中,然后在 ADmad/Glide 插件配置的路由和我的事件监听器中使用此配置来创建一个新的服务器实例。
//in bootstrap.php
//'ADmad/Glide.server' will be used in routes to configure the plugin and in my event listener below to create a new server instance.
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')); // Use the Glide server config above
$file = $event->getData('file');
$server->deleteCache(Configure::read('ADmad/Glide.server.base_url') . $file->url);
}
);
}
);
我在我的 Cakephp4 应用程序中使用插件 cakephp-glide。
我想在删除图片后立即删除图片的 Glide 缓存。
当图像文件被删除时,我的插件 FileManager
触发事件 'afterDelete' :
// 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);
}
我想在我的应用程序 bootstrap 中捕获事件以删除图像缓存:
//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 ???
}
);
但是我不知道如何获取 Glide Server 实例?
我在插件中找不到获取它的方法。 如果有人能帮助我...
根据 ADmad 的建议,解决方案是将 Glide 服务器配置存储在 Configure
中,然后在 ADmad/Glide 插件配置的路由和我的事件监听器中使用此配置来创建一个新的服务器实例。
//in bootstrap.php
//'ADmad/Glide.server' will be used in routes to configure the plugin and in my event listener below to create a new server instance.
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')); // Use the Glide server config above
$file = $event->getData('file');
$server->deleteCache(Configure::read('ADmad/Glide.server.base_url') . $file->url);
}
);
}
);