Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

[DI] Use alias will create shared object twice #327

Open
asika32764 opened this issue Jan 2, 2014 · 2 comments
Open

[DI] Use alias will create shared object twice #327

asika32764 opened this issue Jan 2, 2014 · 2 comments

Comments

@asika32764
Copy link
Contributor

If we use buildSharedObject() to build an object named \Foo\Bar , the instance will be:

instance => Array
(
    [\Foo\Bar] => Object()
)

But if we add alias foo.bar to this key, when we get by alias $container->get('foo.bar');, container will create two object and store it in instance:

instance => Array
(
    [\Foo\Bar] => Object()
    [foo.bar] => Object()
)

It because the buildObject use this closure, it will create new instance everytime called:

$callback = function () use ($reflection, $newInstanceArgs) {
    return $reflection->newInstanceArgs($newInstanceArgs);
};

I'm not sure what is the good way to fix it, so I just report this issue.

See Container::get(): https://github.com/joomla/joomla-framework/blob/staging/src/Joomla/DI/Container.php#L337

public function get($key, $forceNew = false)
{
    // Here we use alias to find raw data
    $raw = $this->getRaw($key);

    // ignore...

    if ($raw['shared'])
    {
        // This key is 'foo.bar', but instance key is \Foo\Bar
        if (!isset($this->instances[$key]) || $forceNew)
        {
            $this->instances[$key] = $raw['callback']($this);
        }

        return $this->instances[$key];
    }

    return call_user_func($raw['callback'], $this);
}
@asika32764
Copy link
Contributor Author

Same problem when child get instance from parent.

@asika32764
Copy link
Contributor Author

Just a sample to fix this:

public function get($key, $forceNew = false)
{
    $raw = $this->getRaw($key);

    $key = $this->resolveAlias($key);

    if (is_null($raw))
    {
        throw new \InvalidArgumentException(sprintf('Key %s has not been registered with the container.', $key));
    }

    if ($raw['shared'])
    {
        if (!isset($this->instances[$key]) || $forceNew)
        {
            $this->instances[$key] = $raw['callback']($this);
        }

        return $this->instances[$key];
    }

    return call_user_func($raw['callback'], $this);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant