Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Latest commit

 

History

History
48 lines (35 loc) · 1.22 KB

UploadAndDeleteImageListener.md

File metadata and controls

48 lines (35 loc) · 1.22 KB

Customizing behavior of file creation/deletion using event listener

You can hook Proffer's image creation/deletion as below.

Create src/Event/UploadAndDeleteImageListener.php

<?php

namespace App\Event;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\Log\Log;
use Proffer\Lib\ProfferPath;

class UploadAndDeleteImageListener implements EventListenerInterface {

    public function implementedEvents()
    {
        return [
            'Proffer.afterCreateImage' => 'createImage',
            'Proffer.beforeDeleteImage' => 'deleteImage',
        ];
    }

    public function createImage(Event $event, ProfferPath $path, $imagePath)
    {
        Log::write('debug', 'hook event of createImage path: ' . $imagePath);

        // copy file to external service (e.g. Amazon S3)
        // delete locale file
    }

    public function deleteImage(Event $event, ProfferPath $path)
    {
        Log::write('debug', 'hook event of deleteImage folder: ' . $path->getFolder());

        // delete file from external service (e.g. Amazon S3)
    }
}

Register listener to EventManager in config/bootstrap.php

Cake\Event\EventManager::instance()->on(new \App\Event\UploadAndDeleteImageListener());