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

[POC] Singleton/Static classes refactoring to dependency injection #5946

Closed
wants to merge 3 commits into from
Closed

Conversation

mnapoli
Copy link
Contributor

@mnapoli mnapoli commented Aug 7, 2014

This is a POC of the refactoring to get rid of singletons and static classes. This is the step 1 described in #4917.

  • Piwik\Db was a static class, i.e. all its methods were static.

I added Piwik\Db\Db (do you like the name? would Database be better?) and moved the code of Db into it. This new class is not static, and uses dependency injection.

Piwik\Db is now a static proxy to Piwik\Db\Db. So for example calling Db::fetchAll(...) will be forwarded to self::$db->fetchAll(...) (Db\Db).

We can now inject the non-singleton (Db\Db) into whatever (in theory). But before doing that, we'll need to move the creation of that object into a container (right now it's created in Db\getInstance()).

We can now also unit tests Db\Db without hitting any database, because we can pass a mock instance of the DbAdapter. Yay.

  • Piwik\Access was a singleton.

I added Piwik\Authorization\Authorization and now Piwik\Access just forwards to this new class.

I think it was probably a mistake to do that because it's not necessary, I just got carried away and thought the new name was more explicit (I didn't get what "Access" meant at first, authorization is more standard). But whatever no big deal, we can revert that.

So a better solution would be probably to have Access extend Authorization (if we want to keep that name and namespace) and it would just add the singleton access for BC compatibility. That would avoid useless call forwarding.

If we don't want to rename it, then we can just keep Access as it is and then when we have a container just have the getInstance() call the container instead of creating the object directly.

…t singleton

Piwik\Access is kept for BC and is now a proxy to the new class
…\Db\Db)

Piwik\Db is kept for BC and is now a static proxy to the new class
@czolnowski
Copy link
Contributor

+1 for Db\Database class
and +1 for Authorization class (more standard name than Access what for me mean like a special case of Authorization)

@mnapoli mnapoli mentioned this pull request Aug 7, 2014
@mnapoli
Copy link
Contributor Author

mnapoli commented Aug 7, 2014

I have changed Access: it now extends Authorization which make much more sense, and it avoids lots of useless code.

Edit: the tests seem to be running OK now, I think the ones that are failing are just because master is failing too.

@mattab mattab added the Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. label Sep 17, 2014
@mattab mattab added this to the Short term milestone Sep 17, 2014
@mattab mattab added c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change. labels Sep 17, 2014
@mnapoli
Copy link
Contributor Author

mnapoli commented Oct 5, 2014

Closing this as this was a POC.

@mnapoli mnapoli closed this Oct 5, 2014
@mattab mattab added the answered For when a question was asked and we referred to forum or answered it. label Oct 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it. c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. Task Indicates an issue is neither a feature nor a bug and it's purely a "technical" change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants