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

References access #392

Closed
spolischook opened this issue Jun 10, 2016 · 9 comments
Closed

References access #392

spolischook opened this issue Jun 10, 2016 · 9 comments

Comments

@spolischook
Copy link

Is there any way to add new reference to alice? Now I have to set array References with setReferences in Loader but can't add just one reference in runtime without reload all of references. Maybe you can open access to $objects property in Loader?

@theofidry
Copy link
Member

I don't really want to open $objects in alice core, there is already a getter/setter which is more than enough. If you need to inject a reference only for 1 load but not others, you can already do it (even though a bit cumbersome).

For more extended and custom usages, you can always create your own loaded inheriting from alice one, $object is protected so you have access to it.

Note that in v3 the loader will be stateless so any external reference will need to be injected in the loading method.

@theofidry
Copy link
Member

That said I don't know what your use case is, if you feel like I missed something feel free to give me more details :)

@spolischook
Copy link
Author

Ok so what I see for now it's:

$this->objects         = new Collection;

in constructor of Nelmio\Alice\Fixtures\Loader and I have only ability to replace all references by setReferences method.
It would be great to have ability add reference by method in Loader, e.g.:

/**
     * @param string $key
     * @param object $object
     */
    public function addReference($key, $object)
    {
        $this->objects->set($key, $object);
    }

@theofidry
Copy link
Member

But you don't "add", because in that case $objects is not a list but a hashmap, i.e. if the key already exists you are overriding the value, not adding it.

It's a bit more verbose but you can already do it:

$references = $loader->getReferences();
$references->set($key, $object);

$loader->setReferences($references);

Granted it's not very nice, but as this won't be in v3 and is potentially confusing (you're not adding but setting), I would prefer not change that.

@spolischook
Copy link
Author

This code

$loader->setReferences($references);

is too expensive, because it will foreach all references again and again.
So maybe, decouple Nelmio\Alice\Instances\Collection (maybe ReferenceRepository is more best name) from Loader, and inject it? Then I can use it separately.

@theofidry
Copy link
Member

So maybe, decouple Nelmio\Alice\Instances\Collection

But that will end up being a BC break, which is not allowed for v2, and this problem won't happen in v3. So in any case I think the best solution for you is to override Alice loader through inheritance.

@spolischook
Copy link
Author

I think the best solution for you is to override Alice loader through inheritance

yep, I'll do. And what will happen in v3 that it's resolve my problem with references?

@theofidry
Copy link
Member

You will have to inject the references in the load, cf. LoaderInterface and the default loader will not hold the objects. That said it will still be possible to have a loader keeping the references by decorating the native one and implementing the loader interface.

If you wish to know more about what will change in v3 check #265.

@spolischook
Copy link
Author

It would be nice upgrade! Wish you the best.

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

No branches or pull requests

2 participants