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

Tagging services #7

Open
h4cc opened this issue Feb 3, 2015 · 3 comments
Open

Tagging services #7

h4cc opened this issue Feb 3, 2015 · 3 comments

Comments

@h4cc
Copy link

h4cc commented Feb 3, 2015

I know such a feature is not part of pimple by Fabién, but it would be cool feature for dependency injection.

How about beeing abled to "tag" services, so all tagged services could be retrieved?

Example:

pimple.set('foo', function () {
  return 'foo';
});
pimple.tag('foo', ['bar']);

console.log(pimple.taggedWith('bar')); // Will output ['foo']
@h4cc
Copy link
Author

h4cc commented Feb 4, 2015

FYI: Using currently this code.

    var tagged = {};

    /**
     * Will add a list of tags to a service name.
     *
     * @param service string
     * @param tags array
     */
    pimple.tag = function (service, tags) {
        tags.forEach(function (tag) {
            if (!tagged[tag]) {
                tagged[tag] = [];
            }
            tagged[tag].push(service);
        });
    };

    /**
     * Will return the names of services tagged with given tag.
     * 
     * @param tag
     * @returns [string]
     */
    pimple.tagged = function (tag) {
        if (!tagged[tag]) {
            return [];
        }

        // Return unique list.
        return tagged[tag].filter(function (value, index, self) {
            return self.indexOf(value) === index;
        });
    };

@tmilos
Copy link

tmilos commented Mar 22, 2016

It would be great to have this built in. But think API would be better like this

pimple.set('service.A', function () { ... });
pimple.set('service.B', function () { ... });

pimple.tag('service.A', 'kernel.request', { method: "onRequest" } );
pimple.tag('service.A', 'kernel.request', { method: "handleRequest", priority: 10 } );
pimple.tag('service.B', 'kernel.request', { method: "onRequest", priority: -20 } );

console.log(pimple.taggedWith('kernel.request')); 
// Will output 
{
    "service.A": [
        { method: "onRequest" },
        { method: "handleRequest", priority: 10 }
    ],
    "service.B": [
        { method: "onRequest", priority: -20 }
    ]
}

If there's no will to include this in the core lib, I would definitely extend it in separate lib... pimple-tags for example, so who needs simple DPI can use just pimple, and those who needs tags also could use that other lib. @Mparaiso what do you say?

@Mparaiso
Copy link
Owner

Mparaiso commented Apr 6, 2016

What's the use case of tagging ? I don't really get it. Why do you need the same service with different tags ?

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

3 participants