Comments (9)
Agreed LazyValue::memoize()
it is.
RE
I mean: besides the current point, do you see any other valid use case?
Only other use case I can think of would be a more complicate construction than creating a single entity. However, the $factory
is a callable, which should allow you to be as complicated as you like. You could even use a service with dependency injection and an __invoke()
method defined.
So I don't think it's necessary to allow extension at this point. It can always be added later if a use-case arises.
from foundry.
What about using the term memoize? A bit computer science-y but perfectly describes the intent.
from foundry.
what about using a named constructor?
$owner = LazyValue::singleValue(fn () => UserFactory::createOne()); // or maybe a better name 😅
from foundry.
Perfect! Thanks for the idea @nikophil! I'll get a PR up soon.
from foundry.
Interesting use-case and solution! I'm all for a PR.
Add a flag to the existing lazy value object or new object?
from foundry.
A new object means we either have to make LazyValue
non-final, or copy paste it's internals.
But a bool flag on the LazyValue
is not very readable IMO.
$owner = new LazyValue(fn () => UserFactory::createOne(), true);
I'd probably lean towards making LazyValue
non-final. This would then allow users to extend it themselves if they have other use-cases they'd like to handle.
However, I'm unsure if it was made final for a specific reason originally?
from foundry.
If we are trying to allow other projects to implement their own, what about using an empty interface and replace $value instaceof self
checks with $value instanceof LazyValueInterface
. Would look something like the following:
interface LazyValueInterface { }
final class LazyValue implements LazyValueInterface
{
...
public function __invoke(): mixed
{
$value = ($this->factory)();
if ($value instanceof LazyValueInterface) {
return ($value)();
}
if (\is_array($value)) {
return self::normalizeArray($value);
}
return $value;
}
/**
* @internal
*/
public static function normalizeArray(array $value): array
{
\array_walk_recursive($value, static function(mixed &$v): void {
if ($v instanceof LazyValueInterface) {
$v = $v();
}
});
return $value;
}
}
Then projects can implement something invokable for their own needs
final class MyLazyValue implements LazyValueInterface
{
public function __invoke(): mixed
{
}
}
from foundry.
indeed, if we decide to allow users to define custom lazy values, we'd introduce an interface, but I'm really wondering if this would actually be used?
I mean: besides the current point, do you see any other valid use case?
from foundry.
I like it, it express well what it does, and it will be documented anyway
from foundry.
Related Issues (20)
- Question: Access pool from it's own story::build() HOT 1
- Possibility of not flushing at all HOT 11
- Add Symfony 7 to test matrix
- ResetDatabase deprecation warnings HOT 4
- 2.x checklist HOT 2
- Publishing of Messenger events after Global State setup HOT 2
- Wrong example for memoize in documentation HOT 1
- Reset incremental ids sequences
- How to make sure Entity association is valid from both sides? HOT 9
- [Foundry 2] Allow objects creation in data provider HOT 2
- support "in-memory" repositories HOT 2
- Problem with dama and reset database
- Add Faker `optional()` by default for nullable fields HOT 6
- Should object be scheduled for insert before afterInstantiate call ? HOT 3
- Error with DAMA + reset mode migrate + global story HOT 11
- [Feature] Add a event before/after reset database (and schema creation) HOT 7
- [Question] [Help needed] How to filter data returned by `random` based on type? HOT 1
- Proxy objetc TypeError HOT 2
- Possibility to "merge" properties in user land HOT 1
- Metadata in doc-comments is deprecated HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foundry.