Last active
October 17, 2015 12:19
-
-
Save davidyell/f6ee8013f06414997504 to your computer and use it in GitHub Desktop.
Example CakePHP3 event listeners for the Proffer plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example listener class which will log the full absolute pathname for a Proffer upload | |
* to the logs. | |
* | |
* Should be in `src/Event` | |
* | |
* @category Example | |
* @package LogFilenameListener.php | |
* | |
* @author David Yell <[email protected]> | |
* @when 03/03/15 | |
*/ | |
namespace App\Event; | |
use Cake\Event\Event; | |
use Cake\Event\EventListenerInterface; | |
use Cake\Log\Log; | |
use Proffer\Lib\ProfferPath; | |
class LogFilenameListener implements EventListenerInterface | |
{ | |
public function implementedEvents() | |
{ | |
// Which events to listen for, and which method to run | |
return [ | |
'Proffer.beforeThumbs' => 'log', | |
'Proffer.afterThumbs' => 'log' | |
]; | |
} | |
public function log(Event $event, ProfferPath $path) | |
{ | |
Log::write(LOG_INFO, $path->fullPath()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example listener which will change the upload folder and filename for an uploaded image | |
* | |
* Should be in `src/Event` | |
* | |
* @category Example | |
* @package UploadFilenameListener.php | |
* | |
* @author David Yell <[email protected]> | |
* @when 03/03/15 | |
* | |
*/ | |
namespace App\Event; | |
use Cake\Event\Event; | |
use Cake\Event\EventListenerInterface; | |
use Cake\Utility\Inflector; | |
use Proffer\Lib\ProfferPath; | |
class UploadFilenameListener implements EventListenerInterface | |
{ | |
public function implementedEvents() | |
{ | |
return [ | |
'Proffer.afterPath' => 'change', | |
]; | |
} | |
/** | |
* Rename a file and change it's upload folder before it's processed | |
* | |
* @param Event $event The event class with a subject of the entity | |
* @param ProfferPath $path | |
* @return ProfferPath $path | |
*/ | |
public function change(Event $event, ProfferPath $path) | |
{ | |
// Detect and select the right file extension | |
switch ($event->subject()->get('image')['type']) { | |
default: | |
case "image/jpeg": | |
$ext = '.jpg'; | |
break; | |
case "image/png": | |
$ext = '.png'; | |
break; | |
case "image/gif": | |
$ext = '.gif'; | |
break; | |
} | |
// Create a new filename using the id and the name of the entity | |
$newFilename = $event->subject()->get('id') . '_' . Inflector::slug($event->subject()->get('name')) . $ext; | |
// If a seed is set in the data already, we'll use that rather than make a new one each time we upload | |
if (empty($event->subject()->get('image_dir'))) { | |
$path->setSeed(date('Y-m-d-His')); | |
} | |
// Change the filename in both the path to be saved, and in the entity data for saving to the db | |
$path->setFilename($newFilename); | |
$event->subject()['image']['name'] = $newFilename; | |
// Must return the modified path instance, so that things are saved in the right place | |
return $path; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi David,
thank you for your work. I could get the plugin to work and I want to implement the Listener to change the file path and name but I'm getting and error. Cake cannot find the listener in my App.
Fatal error: Class '\App\Lib\Proffer\UploadFilenameListener' not found in /Library/WebServer/Workspace/cakephp3.1/anohou/vendor/davidyell/proffer/src/Model/Behavior/ProfferBehavior.php on line 79
Which directory the src/Event refers to?
App/src/Event?
I put this line in my Table Initialize method
// Add your new custom listener
$listener = new App\Event\UploadFilenameListener();
$this->eventManager()->on($listener);
Thank you