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: Add ViewableData::setFailover() to refresh detected methods when changing failover #4870

Merged
merged 1 commit into from Jan 6, 2016

Conversation

kinglozzer
Copy link
Member

Hopefully the tests explain this better than my bug ticket did...

This is the only workaround I could think up for this issue. We don’t have a setter like ViewableData::setFailover() to hook into, and even if we added one we have no guarantee that people would use it (especially because setting $this->failover = $foo; has been an undocumented convention for a while now).

*/
protected function checkAndRedefineMethods() {
if ($this->failover !== $this->oldFailover) {
$this->oldFailover = $this->failover;
Copy link
Member Author

Choose a reason for hiding this comment

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

I did consider using spl_object_hash() for comparing the failover objects (instead of keeping two copies of them in memory), but I don’t think it’s reliable enough - the same hash can be re-used once an object is destroyed.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you force defineMethods() every time setFailover() or __construct() is called, would this remove the need for oldFailover check?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed it would, I think that’ll be the best approach :)

@tractorcow
Copy link
Contributor

Here's a small critique; Will we ever need to re-generate the extra methods when assigning a different instance of a failover of the same class? It looks like the instance is extracted from the failover in Object::__call, so maybe a get_class($failover) comparison is sufficient?

You might also need a mechanism and test to ensure you remove methods from the removed failover, since it looks like the new ones are being merged with the old.

* @inheritdoc
*/
public function hasMethod($method) {
$this->checkAndRedefineMethods();
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should be on setFailover instead, rather than duplicating it over all methods that rely on the failover?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is no setFailover() currently (see original comment) :(

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. If you have it all over the place (especially in __call()) then its going to redefine all methods every time something on the failover object changes.

@kinglozzer
Copy link
Member Author

Will we ever need to re-generate the extra methods when assigning a different instance of a failover of the same class

It’s unlikely, but given that extra methods are defined at an instance-level instead of statically it’s possible, which is why I used that approach.

Upon reflection, I’m wondering if it might be better to add a setFailover() and call defineMethods() in there. Any user-code which doesn’t use that setter may encounter the bug that this is trying to fix, but I don’t think I’ve seen this raised before so I suppose it’s an edge-case?

parent::__construct();
}

public function setFailover($failover) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the setFailover I was refering to. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes, I only added that to the test class. I think I’ll redo this PR and add it to ViewableData, we should probably have a setter anyway.

@tractorcow
Copy link
Contributor

Ok, thanks for your response. :) I'll have a look once you have an update.

@kinglozzer kinglozzer changed the title FIX: Changing ViewableData::$failover didn't refresh defined methods (fixes #4821) NEW: Add ViewableData::setFailover() to refresh detected methods when changing failover Jan 6, 2016
@kinglozzer
Copy link
Member Author

Okay, updated. This PR is looking a bit simpler now :). I’ll do a PR for cms to go with this one that actually fixes #4821

Edit: CMS PR is here - silverstripe/silverstripe-cms#1355

tractorcow pushed a commit that referenced this pull request Jan 6, 2016
NEW: Add ViewableData::setFailover() to refresh detected methods when changing failover
@tractorcow tractorcow merged commit c32017e into silverstripe:3 Jan 6, 2016
@kinglozzer kinglozzer deleted the 4821-viewabledata-failover branch January 7, 2016 09:07
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.

None yet

3 participants