Help needed about Piwik\Registry deprecation

Well, inside PHPStorm and the changelog also, Piwik\Registry usage was deprecated since 2.10.*

So you suggest using Piwik\Container\StaticContainer but I still get warnings by PHPStorm because looks like StaticContainer would be deprecated too.

This pattern looks better like a Dependency Injection design, so what’s the best implementation for Piwik 2.10.* installations and the upcoming in the future?

Hi, sorry about the delay. To answer your question directly: Piwik\Container\StaticContainer is less deprecated than the Registry :wink:

To expand, yes we want to use dependency injection everywhere because it’s a much better pattern. However using dependency injection means changing a lot of things in Piwik, so in order to move towards that goal we consider acceptable to use the StaticContainer.

StaticContainer is marked as deprecated because it might be removed in 3.0 or later, and that deprecation means “it’s better to use dependency injection if you can”. However if you can’t use dependency injection right now, then use the StaticContainer.

Given all that, can you use dependency injection? That depends. Where are you calling the StaticContainer? Is it in a controller, in an API class?

Also what methods are you calling? Only “get()”? Or also “set()”?

If your plugin code is open source please give me a link I might be able to help more. Or else paste a bit of code here.


The code we’re talking about is inside my plugin class that extends the core Plugin class.
I bet I should inject my authentication class inside the core Auth but isn’t so clear for me how to do.

     * Set login name and token for authentication request.
     * Listens to API.Request.authenticate hook.
    public function ApiRequestAuthenticate($tokenAuth)
        StaticContainer::get('Piwik\Auth')->setLogin($login = null);

     * Initializes the authentication object.
     * Listens to Request.initAuthenticationObject hook.
    public function initAuthenticationObject($activateCookieAuth = false)
        $auth = new RerAuth;
        StaticContainer::getContainer()->set('Piwik\Auth', $auth);
        Login::initAuthenticationFromCookie($auth, $activateCookieAuth);

OK if it’s in that class then using the StaticContainer is fine, your code looks fine to me.

Hello @mnapoli sorry for reviving an old topic, (imho it’s still fresh), the question is: should I better make use DI\Container; then invoke:

Container->get('\Piwik\Auth')->setLogin($login = null);
// AND: 
Container->set('\Piwik\Auth', $auth);