Skip to content
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

Merged
merged 12 commits into from
Nov 14, 2023

Conversation

Toflar
Copy link
Member

@Toflar Toflar commented Jul 11, 2023

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.

@Toflar Toflar requested a review from m-vo July 11, 2023 10:43
@fritzmg
Copy link
Contributor

fritzmg commented Jul 11, 2023

Shouldn't we apply this to all content elements and front end modules then?

@Toflar
Copy link
Member Author

Toflar commented Jul 11, 2023

What? No - only what qualifies as a use case.

m-vo
m-vo previously approved these changes Jul 11, 2023
Copy link
Member

@m-vo m-vo left a 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:

  1. 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?)

  2. Also move the class to abstract class AbstractDownloadsController and add an empty default implementation final DownloadsController extends AbstractDownloadsController, to make it clear that the abstract is intended to be reused.

  3. 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. ☺️

@Toflar
Copy link
Member Author

Toflar commented Jul 11, 2023

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 ?

@leofeyer leofeyer added this to the 5.2 milestone Jul 12, 2023
@leofeyer
Copy link
Member

leofeyer commented Jul 12, 2023

extracting it into an abstract

Yes. 👍

(I‘d also be fine with a helper service as suggested by @m-vo above.)

@Toflar Toflar modified the milestones: 5.2, 5.3 Jul 12, 2023
@Toflar
Copy link
Member Author

Toflar commented Jul 12, 2023

I'll work on it for 5.3 then :)

Copy link
Member

@leofeyer leofeyer left a 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.

@Toflar Toflar force-pushed the feature/more-flexible-downloads-controller branch from f795860 to 0925982 Compare November 2, 2023 15:15
@Toflar Toflar changed the base branch from 5.2 to 5.x November 2, 2023 15:15
@Toflar Toflar marked this pull request as draft November 2, 2023 15:16
@Toflar Toflar force-pushed the feature/more-flexible-downloads-controller branch 4 times, most recently from 8613958 to 46e45b5 Compare November 2, 2023 16:03
Copy link
Member Author

@Toflar Toflar left a 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.

@Toflar Toflar marked this pull request as ready for review November 2, 2023 16:15
@Toflar Toflar requested review from leofeyer and m-vo November 2, 2023 16:15
Copy link
Member

@m-vo m-vo left a 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 todos can be solved in the future without anyone needing to update their implementations.

@Toflar
Copy link
Member Author

Toflar commented Nov 6, 2023

$this->handleDownload() is in the abstract class for exactly that reason?

@m-vo
Copy link
Member

m-vo commented Nov 6, 2023

$this->handleDownload() is in the abstract class for exactly that reason?

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 getResponse() and remove it later on when we switch to a route that handles it.

Can we do something like this?

public function __invoke(…): Response {
    $this->handleDownload();
    parent::__invoke(…);
}

@Toflar Toflar force-pushed the feature/more-flexible-downloads-controller branch from 46e45b5 to eda5acb Compare November 7, 2023 11:46
@Toflar
Copy link
Member Author

Toflar commented Nov 7, 2023

Ah, now I understand. Yes of course, that makes sense! Done.

@Toflar Toflar requested a review from m-vo November 7, 2023 12:16
m-vo
m-vo previously approved these changes Nov 11, 2023
Copy link
Member

@m-vo m-vo left a 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. 👍

@leofeyer leofeyer dismissed their stale review November 12, 2023 20:26

No longer valid

leofeyer
leofeyer previously approved these changes Nov 14, 2023
@leofeyer leofeyer enabled auto-merge (squash) November 14, 2023 10:52
@leofeyer
Copy link
Member

Thank you @Toflar.

@leofeyer leofeyer merged commit ff6a463 into contao:5.x Nov 14, 2023
16 checks passed
@Toflar Toflar deleted the feature/more-flexible-downloads-controller branch November 14, 2023 10:56
@leofeyer leofeyer changed the title Make DownloadsController more flexible for own sources Make the downloads cntroller more flexible for own sources Jan 18, 2024
@leofeyer leofeyer changed the title Make the downloads cntroller more flexible for own sources Make the downloads controller more flexible for own sources Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants