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

Dependency injection in widgets, menus, settings and tasks #7408

Merged
merged 5 commits into from Mar 16, 2015

Conversation

mnapoli
Copy link
Contributor

@mnapoli mnapoli commented Mar 10, 2015

Allows to use dependency injection in widgets, menus, settings and tasks.

This PR contains an example in the Piwik\Plugins\CoreHome\Widgets class.

@mnapoli mnapoli added the Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc. label Mar 10, 2015
@mnapoli mnapoli added this to the Piwik 2.12.0 milestone Mar 10, 2015
@@ -30,16 +30,6 @@
*/
class Menu
{
protected $module = '';
Copy link
Member

Choose a reason for hiding this comment

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

I think @tsteur wrote the Menu class, so I'm not sure if this was his intent or not, but I think the idea for using a variable instead of getModule() directly was so derived classes could override the value. @tsteur can you confirm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A "find usage" didn't yield any usage outside of this class but maybe @tsteur has more info

Copy link
Member

Choose a reason for hiding this comment

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

It is there for performance. We will do it only once instead of many times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given it is used only in the URL generator methods I think it's safe to think that there will be no performance regression?

Copy link
Member

Choose a reason for hiding this comment

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

It is called quite often and would be nice to have it just as fast as possible. Especially when it is so easy to achieve. It won't be a huge regression for sure

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 think you are confusing "called in total" and "called in each plugin". It is called just a few times in each plugin, and since that method return the plugin name that wouldn't cache the 50 calls, only 1-2-3 calls which is why I'm saying it's useless.

The problem is that we want to leave the constructor free to use for subclasses to use dependency injection. If those classes need to call the parent constructor this is error prone as we can expect beginner plugin developers to miss that (and the errors will be hard to debug). Even for experienced developers this is just a pain for no gain.

Copy link
Member

Choose a reason for hiding this comment

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

It is > 50 times called in total. Whereas now > 100 times in total as called twice in that method. So would save 50 times. Again, it isn't a huge improvement but it was faster this way and plugins could overwrite the value if needed. Also it was nicer to use. If it is not good for DI then I reckon it can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, the total number of calls doesn't matter since the function returns the plugin name. Called once in 50 plugins is not the same as called 50 times in 1 plugin.

Called once in 50 plugins means the cache would not save anything because it would cache a new value every time.

Copy link
Member

Choose a reason for hiding this comment

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

See urlForAction(). It calls $this->getModule() twice in that function. Meaning if it is cached it would be only done once per plugin. When this function urlForAction() is called once per plugin, we save one call to getModule(). If that method is called 2 times per plugin, we save 3 called to getModule() etc. If 50 plugins call urlForAction() once, it saves 50 called in the end since getModule() would be executed 100 times.

Anyway, it is not so important as said if it is not good for DI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In that case the problem is with urlForAction() where $this->getModule() would simply be put in a variable, there is no need for a cache for getModule(). A quick look in blackfire shows that get_class() doesn't even appear in the profiling, it's no performance concern at all.

@diosmosis
Copy link
Member

👍

@mnapoli
Copy link
Contributor Author

mnapoli commented Mar 16, 2015

I believe this is ready to be merged, I've replaced the 2 calls to getModule() by one in a7a8c71

mattab pushed a commit that referenced this pull request Mar 16, 2015
Dependency injection in widgets, menus, settings and tasks
@mattab mattab merged commit 20e002f into master Mar 16, 2015
@mnapoli mnapoli deleted the di-in-widgets-menus-tasks branch March 16, 2015 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement For new feature suggestions that enhance Matomo's capabilities or add a new report, new API etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants