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

Add central/singleton Factory and deprecate FactoryTrait trait #221

Merged
merged 7 commits into from
Jun 7, 2020

Conversation

mvorisek
Copy link
Member

@mvorisek mvorisek commented Jun 4, 2020

no BC break

merge after: atk4/ui#1251 (one line fix needed for ui/Template because of php7.2 limitation of traits)

By using single point factory we can offer extreme magic - user can inject own Factory and he can return any implementation he wants for any object (based on stacktrace even different) thru the whole project.

For these reasons you should never use direct instantiation:

new Class($arg1, $args2, ...)

in Atk code but always create new instance thru \atk4\core\Factory like:

Factory::factory([Class::class, $arg1, $rg2, ...])

FactoryTrait was quite a bad design pattern - there is currently no need to remove it, as it can be deprecated only for now without any negative side effect.

@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from 972d269 to 90216fc Compare June 4, 2020 21:22
@mvorisek mvorisek requested a review from DarkSide666 June 4, 2020 21:22
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch 4 times, most recently from 2d9503e to 6491150 Compare June 4, 2020 23:38
@mvorisek mvorisek changed the title Move fromSeed and related methods to DIContainerTrait trait Add single point factory Jun 4, 2020
@mvorisek mvorisek changed the title Add single point factory Add single point (potentionally customizeable) factory Jun 4, 2020
@mvorisek mvorisek changed the title Add single point (potentionally customizeable) factory Add single point (customizeable) factory Jun 4, 2020
@mvorisek mvorisek changed the title Add single point (customizeable) factory Replace FactoryTrait with singleton factory Jun 4, 2020
@mvorisek mvorisek changed the title Replace FactoryTrait with singleton factory Replace FactoryTrait with singleton Factory Jun 4, 2020
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from a2ec463 to 72c69cc Compare June 4, 2020 23:58
@mvorisek mvorisek changed the title Replace FactoryTrait with singleton Factory Deprecate FactoryTrait with singleton Factory Jun 5, 2020
@mvorisek mvorisek removed the BC-break label Jun 5, 2020
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch 2 times, most recently from e2a9803 to 20cf93f Compare June 5, 2020 11:23
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from 5bec9f4 to 9a4817b Compare June 6, 2020 10:57
@mvorisek mvorisek changed the title Deprecate FactoryTrait with singleton Factory Add central/singleton Factory and deprecate FactoryTrait trait Jun 6, 2020
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch 6 times, most recently from d23ce41 to 5da2218 Compare June 6, 2020 12:14
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from 42e0087 to 3fa4b31 Compare June 6, 2020 20:10
@mvorisek mvorisek marked this pull request as ready for review June 6, 2020 20:11
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from 3fa4b31 to 18cd07c Compare June 6, 2020 20:12
@mvorisek mvorisek added RTM and removed RTM labels Jun 6, 2020
@mvorisek mvorisek marked this pull request as draft June 6, 2020 20:13
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from fc78823 to 8bd3aef Compare June 6, 2020 20:31
@mvorisek mvorisek marked this pull request as ready for review June 6, 2020 20:32
@mvorisek mvorisek added the RTM label Jun 6, 2020
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from ffaf5c9 to 96cff5f Compare June 6, 2020 20:35
@mvorisek mvorisek force-pushed the allow_to_use_fromseed_almost_everywhere branch from 049e3e8 to 6b0478b Compare June 6, 2020 20:42
@DarkSide666
Copy link
Member

This one I definitely would like @romaninsh to review too.

Copy link
Member

@romaninsh romaninsh left a comment

Choose a reason for hiding this comment

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

Agree that this is not a BC, however updating other core classes to use new Factory trait would cause BC..

It's better if we plan it and suggest users how to update their code.

@mvorisek mvorisek merged commit 9ee73d8 into develop Jun 7, 2020
@mvorisek mvorisek deleted the allow_to_use_fromseed_almost_everywhere branch June 7, 2020 16:44
@atk4 atk4 deleted a comment from codecov bot Apr 5, 2021
src/Core.php Outdated
/**
* See \atk4\core\Factory::factory().
*/
public static function factory($seed, $defaults = [], /* remove in 2021-jun, catch passed but no longer supported prefix */...$extraArgs): object
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about renaming this create for semantic purposes?

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 am not against, but I would like to see analysis from top 10 frameworks to decise if it is worth or not.

Copy link
Member Author

Choose a reason for hiding this comment

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

You should always use ClWithDi::fromSeed, thus renaming this method does not help with daily usage...

src/Core.php Outdated
/**
* See \atk4\core\Factory::mergeSeeds().
*/
public static function mergeSeeds(...$seeds)
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about using only merge?

Copy link
Member Author

Choose a reason for hiding this comment

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

Current name is much more self explaning, I prefer to keep it.

*/
public static function fromSeed($seed = [], $defaults = [])// :static supported by PHP8+
{
if (func_num_args() > 2) { // prevent bad usage
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can reduce code here by simpy calling

$object = static::fromSeedUnsafe($seed, $defaults);

static::assertInstanceOf($object);

return $object;

Copy link
Member Author

Choose a reason for hiding this comment

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

⚠️ Can, but do not want - this way, assertInstanceOf is computed even before the object is even created, which implies stronger security! (as seed can be an user input)

Copy link
Collaborator

Choose a reason for hiding this comment

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

What I see the assertInstanceOf is called before the $object is returned only?
The rest of the code is the same.

Copy link
Member Author

Choose a reason for hiding this comment

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

⚠️ No! _fromSeedPrecheck is called BEFORE object is constructed. Be very carefull around this and test for this behaviour will be highly welcomed.

Copy link
Member Author

Choose a reason for hiding this comment

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

PR for test: #311

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