Came up with a solution (kind of) - posting it here for everyone's future
reference.
Injecting the service locator, triggering events or adding listeners did
cross my mind, but some of my forms are created programmatically, others via
annotations, so the solution needs to work in both scenarios.
My workaround was to create a new filter, RenameUploadWithCallback, which
accepts a 'callback' option to rename the file, in place of the 'target'
option. If the callback is not specified, it falls back to the target option
as like its parent class, Zend\Filter\File\RenameUpload.
The callback must take in 2 arguments ($uploadData, $targetDir) and return a
string. If the callback is a string, a class name is assumed and its
__invoke() will be called. __invoke() must then take in the same 2
arguments.
// Programmatically - using example in previous post
$fileInput->getFilterChain()->attachByName(
'ZnZend\Filter\File\RenameUploadWithCallback', array(
'callback' => function ($uploadData, $targetDir) { return '/path/to/save';
}, ) );// Form annotations /** *
@Annotation\Type("Zend\Form\Element\File") * @Annotation\Filter({ *
"name":"ZnZend\Filter\File\RenameUploadWithCallback", *
"options":{"callback":"MyProject\Callback\RenameUploadCallback"} * })
*/ public $file;
The source code is at
https://github.com/zionsg/ZnZend/blob/master/src/ZnZend/Filter/File/RenameUploadWithCallback.php
<https://github.com/zionsg/ZnZend/blob/master/src/ZnZend/Filter/File/RenameUploadWithCallback.php>
.This is what I've come up with for now. If anyone has an alternate
suggestion, do post back to this thread - thanks!
-----
Zion Ng
Singapore
--
View this message in context:
http://zend-framework-community.634137.n4.nabble.com/How-to-inject-dependencies-into-filters-via-config-tp4660772p4660872.html
Sent from the Zend Framework mailing list archive at Nabble.com.