piwik/piwik — pull request #5946 by mnapoli (2014-08-07 10:37:20+00:00)
Title: [POC] Singleton/Static classes refactoring to dependency injection

This is a POC of the refactoring to get rid of singletons and static classes.

- `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.

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.