Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: 🐛 use isser instead of constructor to apply autorefresh setting to Factory#998

Merged
nikophil merged 1 commit into
zenstruck:2.xfrom
ttskch:fix/factory-template
Oct 8, 2025
Merged

fix: 🐛 use isser instead of constructor to apply autorefresh setting to Factory#998
nikophil merged 1 commit into
zenstruck:2.xfrom
ttskch:fix/factory-template

Conversation

@ttskch
Copy link
Copy Markdown
Contributor

@ttskch ttskch commented Oct 8, 2025

Although PersistentObjectFactory has a constructor, the template of Factory that inherit from it is missing parent::__construct() call. This PR adds that. Thanks.

@ttskch ttskch changed the title fix: 🐛 add parent::__construct() call to Factory template fix: add parent::__construct() call to Factory template Oct 8, 2025
Copy link
Copy Markdown
Member

@nikophil nikophil left a comment

Choose a reason for hiding this comment

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

Hi!

thanks for this! This means that maybe a lot of factories in the wild won't benefit from auto refresh 🤔

Maybe we should modify PersistentObjectFactory in a way that it could handle the case where $autorefreshEnabled has never been initialized?

Please, adapt the tests: you'll need to add parent::__construct() where it is needed in https://github.com/zenstruck/foundry/tree/2.x/tests/Fixture/Maker/expected

@ttskch
Copy link
Copy Markdown
Contributor Author

ttskch commented Oct 8, 2025

@nikophil I fixed test fixtures! Please review again.

Maybe we should modify PersistentObjectFactory in a way that it could handle the case where $autorefreshEnabled has never been initialized?

I think since PersistentObjectFactory::$autorefreshEnabled is initialized to false, there should be no problem even if we do not modify it.

However, if we would like to take maximum consideration into account users who are unintentionally missing out on the benefits of autorefresh due to this bug, we may be able to take additional measures, such as the following:

  • Change the initial value of PersistentObjectFactory::$autorefreshEnabled to null.
  • Just before using PersistentObjectFactory::$autorefreshEnabled, if a condition such as Configuration::autoRefreshWithLazyObjectsIsEnabled() && $factoryUsed->autorefreshEnabled is met, emit an E_WARNING level error (or some similar warning) and just treat $factoryUsed->autorefreshEnabled as false.

@nikophil
Copy link
Copy Markdown
Member

nikophil commented Oct 8, 2025

I fixed test fixtures! Please review again.

cool thanks

I think since PersistentObjectFactory::$autorefreshEnabled is initialized to false, there should be no problem even if we do not modify it.

not sure... think of this situation:

# config/packages/zenstruck_foundry.yaml
zenstruck_foundry:
    enable_auto_refresh_with_lazy_objects: true
class SomeFactory extends PersistentObjectFactory
{
    public function __construct(
        // ... some dependencies ...
    ){
        // no call to parent
    }
}

the user explicitly asks for auto refresh to be enabled. But PersistentObjectFactory's constructor won't be called, then it will be disabled.

I think we should do something like that:

// PersistentObjectFactory

private bool|null $autorefreshEnabled;

private function isAutorefreshEnabled(): bool
{
    return $this->autorefreshEnabled ??= Configuration::autoRefreshWithLazyObjectsIsEnabled();
}

and use isAutorefreshEnabled() anytime we want to access the variable

WDYT?

@ttskch
Copy link
Copy Markdown
Contributor Author

ttskch commented Oct 8, 2025

@nikophil

I think we should do something like that:
WDYT?

In conclusion, I completely agree.

I think there are two possible approaches for existing users whose automatic updates are disabled due to this bug.

  1. When you upgrade to the next version, the automatic update settings will be correctly reflected, and automatic updates that were previously (unintentionally) disabled will be enabled.
  2. To minimize the impact to users, even when upgrading to the next version, simply issue a warning to encourage the parent constructor to be called, and do not enable automatic updates.

Option 2 is the most cautious approach, but it could be considered a bit excessive.

Personally, I think option 1 is fine. And in that case, I think the example you provided is a very safe and good approach.

In any case, I don't think there's any need to address this issue in this PR.

Why not merge this PR for now and address any additional issues in a separate PR?

@nikophil
Copy link
Copy Markdown
Member

nikophil commented Oct 8, 2025

Actually, I do think if we implement the approach I'm suggesting, we do not need to impact the template : we should remove the constructor from PersistentObjectFactory and initialize the prop when needed

using the constructor was a mistake in the first place. Sorry for the work which needs to be undone

@ttskch
Copy link
Copy Markdown
Contributor Author

ttskch commented Oct 8, 2025

Ah, I understand. You're absolutely right. I'll update the PR later!

@nikophil
Copy link
Copy Markdown
Member

nikophil commented Oct 8, 2025

thank you!

…to Factory

The template of Factory that inherit from `PersistentObjectFactory` has  constructor but is missing parent::__construct() call. Therefore, we will modify `PersistentObjectFactory` so that the settings are applied implicitly via isser instead of the constructor.
@ttskch ttskch force-pushed the fix/factory-template branch from 691e832 to e432cf1 Compare October 8, 2025 08:38
Copy link
Copy Markdown
Member

@nikophil nikophil left a comment

Choose a reason for hiding this comment

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

👍

@nikophil
Copy link
Copy Markdown
Member

nikophil commented Oct 8, 2025

thanks @ttskch

@nikophil nikophil merged commit 9489e83 into zenstruck:2.x Oct 8, 2025
73 checks passed
@ttskch ttskch deleted the fix/factory-template branch October 8, 2025 09:18
@nikophil
Copy link
Copy Markdown
Member

nikophil commented Oct 8, 2025

released https://github.com/zenstruck/foundry/releases/tag/v2.7.4

@ttskch ttskch changed the title fix: add parent::__construct() call to Factory template fix: 🐛 use isser instead of constructor to apply autorefresh setting to Factory Oct 8, 2025
ttskch added a commit to ttskch/api-platform-book-app that referenced this pull request Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants