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

New extender for adding variables to HtmlDocument payload #1608

Merged
merged 2 commits into from
Oct 21, 2018

Conversation

franzliedke
Copy link
Contributor

@franzliedke franzliedke commented Oct 20, 2018

Fixes #1602

Changes proposed in this pull request:
Add a document() method to the Frontend extender as proposed in #1602.

Reviewers should focus on:
The method accepts a closure as well as a class name, to allow for more complex logic to be encapsulated in a class.

Confirmed

  • Backend changes: tests are green (run php vendor/bin/phpunit).

Required changes:

  • Fix database columns in flarum/statistics
  • Adopt extender in flarum/statistics

@dsevillamartin
Copy link
Member

dsevillamartin commented Oct 20, 2018

@franzliedke I think that @tobscure had in mind (and mentioned a few times) is that Flarum flattens the extend.php array, so you can use the extenders in multiple files and require them (however that's done) in extend.php

image

How would it work with the Listener, anyway? I don't see a specific method being called or anything... though that may be because of my nonexistent Laravel knowledge.

@franzliedke
Copy link
Contributor Author

@datitisev You mean having a separate file with a rather long extender callback instead of my proposed solution with an extra class?

Not sure whether that works here, as we're just calling one more method on the Frontend extender, which is very short otherwise.

@franzliedke
Copy link
Contributor Author

How would it work with the Listener, anyway? I don't see a specific method being called or anything... though that may be because of my nonexistent Laravel knowledge.

The class would have an __invoke method, which means it can be treated like a closure or any other callable.

src/Extend/Frontend.php Outdated Show resolved Hide resolved
$callback = $container->make($this->documentCallback);
}

$view->addCallback($callback);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to add a new collection of "callbacks" to the HtmlDocumentFactory? That's what "content" is for. We could do this instead:

$content = new class implements ContentInterface {
    public $callback;
    public function populate(HtmlDocument $view, Request $request)
    {
        $this->callback($view, $request);
    }
});

$content->callback = $callback;

$view->add($content);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point. What about getting rid of ContentInterface and making those classes invokable, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can keep the interface, and the existing listener class would implement that interface instead of being invokable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your second suggestion, as per my comment in the other thread :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done this now. I changed ContentInterface to use __invoke instead of populate, so that we can just drop in closures instead (we weren't type-hinting anyway).

@franzliedke franzliedke force-pushed the fl/1602-extend-frontend-document branch from 1a9b9c7 to d14afb0 Compare October 21, 2018 18:42
@franzliedke franzliedke force-pushed the fl/1602-extend-frontend-document branch from d14afb0 to 4ed1d0a Compare October 21, 2018 18:45
@franzliedke
Copy link
Contributor Author

All done from my side.

@franzliedke
Copy link
Contributor Author

flarum/statistics can now get by without implementing the ContentInterface (it just defines __invoke, without any implements ContentInterface). This would mean we can consider ContentInterface private API for now, and I kind of like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants