-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Make the downloads controller more flexible for own sources #6206
Make the downloads controller more flexible for own sources #6206
Conversation
Shouldn't we apply this to all content elements and front end modules then? |
What? No - only what qualifies as a use case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think it's a good idea to treat the controllers as if they were final classes, i.e. that they are not meant to be reused. But I can understand the use case here.
Some other options, we should at least consider to communicate this idea:
-
Extract the functionality in a helper class/service, like the already existing
FileDownloadHelper
, so that the controller itself gets 'thinner' and does not need to be reused. (Is this feasible in this scenario?) -
Also move the class to
abstract class AbstractDownloadsController
and add an empty default implementationfinal DownloadsController extends AbstractDownloadsController
, to make it clear that the abstract is intended to be reused. -
Alternatively mark all other controllers with
@final
and make them final in Contao 6.
Wdyt? I'm also fine with the simple changes as they are in this PR.
I actually think the logic is controller-specific so it's good to reside where it does. But I like the idea of extracting it into an abstract. Wdyt, @contao/developers ? |
Yes. 👍 (I‘d also be fine with a helper service as suggested by @m-vo above.) |
I'll work on it for 5.3 then :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is still based on the 5.2
branch, therefore I request changes so we don‘t accidentally merge it before it has been rebased.
f795860
to
0925982
Compare
8613958
to
46e45b5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ready for review now. I have added a few review comments myself to explain my design thoughts.
I have tested the changes in the demo and everything still works.
core-bundle/src/Controller/ContentElement/AbstractDownloadContentElementController.php
Show resolved
Hide resolved
core-bundle/src/Controller/ContentElement/AbstractDownloadContentElementController.php
Show resolved
Hide resolved
core-bundle/src/Controller/ContentElement/AbstractDownloadContentElementController.php
Show resolved
Hide resolved
core-bundle/src/Controller/ContentElement/AbstractDownloadContentElementController.php
Show resolved
Hide resolved
core-bundle/src/Controller/ContentElement/AbstractDownloadContentElementController.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very promising to me!
I was wondering if we could also move $this->handleDownload()
into the abstract class? We could do this by overwriting __invoke()
to handle the download and then call parent::__invoke()
. Basically, so that all open todo
s can be solved in the future without anyone needing to update their implementations.
|
Yes, but I would also put the call to it in the abstract class as well. Because now you still need to add it to your implementation of Can we do something like this? public function __invoke(…): Response {
$this->handleDownload();
parent::__invoke(…);
} |
46e45b5
to
eda5acb
Compare
Ah, now I understand. Yes of course, that makes sense! Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how much cleaner the downloads controller is looking now. Probably even helps people to better understand the logic. And all the workarounds are moved out of the class as well. 👍
Thank you @Toflar. |
With the introduction of the new
DownloadsController
, I could actually turn my own 600 lines of code in my custom downloads content element into just 20. Everything works the same: I need previews, download handling, sorting etc. The only difference is: I have a different source folder in the settings and additional$filesystemItems->filter()
calls.This PR introduces an
AbstractDownloadContentElementController
which greatly enhances reusability.