-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Rename #222
base: 3.0.x
Are you sure you want to change the base?
Refactor Rename #222
Conversation
I'm not entirely sure because I haven't had a decent look at it myself, but:
My reasoning here is that we shouldn't need to worry too much about compat with input filter and form, because they are next in line for major release, and, if you want to deal with multiple uploads, then, from input filters perspective, we should be looking at an 'ArrayInput' wrapping n 'FileInput's rather than all this type juggling. Options:
Further reference:
|
The $filter = new \Laminas\Filter\File\Rename([
[
'source' => 'fileA.txt'
'target' => '/dest1/newfileA.txt',
'overwrite' => true,
],
[
'source' => 'fileB.txt'
'target' => '/dest2/newfileB.txt',
'randomize' => true,
],
]); But I would follow your idea and the
I also think that is the right way to go:
If several values are to be filtered, a decorator can still be created for this purpose. |
Thanks both. Started moving things in that direction. Just a few more questions;
|
OK, so branching configuration based on matching the input to an option set has merit. Perhaps this can be made more explicit with a i.e. [
[
'match' => '*.txt',
'target' => '/text/files',
],
[
'match' => '*.pdf',
'target' => '/pdf/files',
],
]; We should be receiving a file path (Or PSR Upload, or PHP Upload). All of those 3 evaluate to a single file path input, so the filter's job should be, (assuming input matches an options set):
In which case, I think that the options to configure that behaviour should be:
I think that From there, The above behaviour makes more sense to me and I can see the filter being useful when processing an HTTP upload, or some other input such as, say, a list of file paths on a local disk. So, options proposal: /**
* @psalm-type OptionsSet = array{
* match?: non-empty-string, // default '*'
* target?: non-empty-string, // default '*'
* renameTo?: non-empty-string, // default '*'
* overwrite?: bool, // default false
* randomize?: bool, // default false
* }
* @psalm-type Options = OptionsSet|list<OptionsSet>
*/
When match, target and renameTo are all *, then the filter (by default) does absolutely nothing. This is all just opinion, so feel free to point out glaring stupidity in my suggestions. We have an opportunity to break BC and make it useful and predictable.
I think that if the file cannot be successfully moved/renamed, that's an exceptional condition. It means the developer probably hasn't got a writable directory in the right place, so it's more of a configuration error that an exception reacting to user input. If we've been given theoretically filterable input and the filter fails, then it's an exception. For input that cannot be filtered based solely on the input, then return the un-filtered value. |
@gsteel That all makes sense thanks. I've put in a single input version, to check the behavior is as expected and ask if there's any further test scenarios anyone can think that need covering. (Ignore the static analysis errors in RenameTest, I just haven't got to them yet). I've renamed "target" to "target_directory" just to be a bit more explicit. In terms of passing in an array for configuration for multiple matches, is this something we could do by just adding multiple Rename filters to a field (possibly in a Chain) rather than having an array of arrays for the config? (Happy to add config array handling, but worth checking first) |
I was thinking that the more complex config would be useful for "If it's a word doc, put in /docs, if it's a pdf put in /pdf-files", but you're right that multiple rename filters chained after each other could achieve the same outcome and would also simplify handling configuration internally. I'm on the fence but leaning towards a single set of options (And multiple filters if required) What do you think @froschdesign? |
Would it then have to be ensured within the chain that different "matches" are defined so that all filters in the chain do not process all files? Or is this simply a misconfiguration? I think one thing we must not overlook is that this filter is not intended for files that come from an upload. |
True it does have the issue that Chains won't stop at the first match. A potential alternative is to have the Rename filter for single cases, and add a RenameList wrapper that can iterate through multiple Rename filters until it finds a match? |
Are there any updates? I also have the PR with the RenameUpload Filter which should handle the Rename stuff for uploaded files. I would like to use the same behavior like here (and with the same naming of the options) if this is done. |
Sorry @marcelthole been a busy few weeks. In the absence of further discussion, I'll update this PR to support an array of Option arrays being passed in, as well as just an individual options array |
Signed-off-by: ramchale <[email protected]>
Signed-off-by: ramchale <[email protected]>
Signed-off-by: ramchale <[email protected]>
Signed-off-by: ramchale <[email protected]>
@gsteel @froschdesign a few Psalm issues I need to fix, but otherwise this is functionally what I think it should be doing. Any feedback before I update the docs to reflect the changes in behaviour? |
@gsteel |
Very good point and this must also be implemented. 👍🏻 |
If you don't mind having seperate(ish) filters for single and multiple files something like this could work |
I haven't forgotten this PR - sorry, very busy! |
Description
Refactoring for #177
I had a couple of questions around the intent for v3;
target, etc
, or an array of arrays oftarget, etc
. Which of these do we want to support going forward?