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

[NFR] Phalcon\DI: Info on whether DI service has been resolved #1242

Closed
temuri416 opened this issue Sep 17, 2013 · 9 comments
Closed

[NFR] Phalcon\DI: Info on whether DI service has been resolved #1242

temuri416 opened this issue Sep 17, 2013 · 9 comments

Comments

@temuri416
Copy link
Contributor

In Phalcon\DI: add an attribute (and getter method) to indicate whether a service has been resolved.

I have my DI container with custom service in it:

$di = new Phalcon\Di;
$di->set('whatever', function(){
    return new Whatever();
}, true); // It is shared.

Shared service "whatever" may or may not be resolved (instantiated/set) by my application.

At the end of request cycle I need to know whether it was resolved or not.

Obviously, I don't want to do:

$di->get('whatever');

because that will resolve it for sure.

I need a way of knowing whether shared service "whatever" has been resolved or not.

Thanks,
Temuri

@ghost
Copy link

ghost commented Sep 18, 2013

The quick fix is to override Whatever::resolve() method — it will be called when DI tries to resolve the service. You can add a static variable into Whatever and set it to true when resolve() is called.

@temuri416
Copy link
Contributor Author

@sjinks Thanks for the tip. Is there a plan to implement it on DI level?

@ghost
Copy link

ghost commented Sep 24, 2013

@phalcon What do you think?

@phalcon
Copy link
Collaborator

phalcon commented Sep 25, 2013

@temuri416 why do you need to do this?

@temuri416
Copy link
Contributor Author

@phalcon

Here's what I'm doing in my project.

  1. I have multiple file-based log devices that register groups of application events (auth, exceptions, model operations and so on). They are registered in DI, they are all separate entities and I do not want put wrappers around them.
  2. Each log device has a fixed name in DI (LOGD_AUTH, LOGD_MODEL, etc).
  3. I am using Transactions in my log devices.
  4. At the end of execution cycle I am looping over known log device names and running ->commit() to flush log buffer.

The problem is that I do not know whether certain log devices have been used at all (LOGD_AUTH, for example, is only used once per user session).

I do not want to run into overhead of resolving log devices in DI that may have not been used.

(On a side note - there's another related problem - I need to know whether a log device has initiated a transaction - see #1238).

I have implemented a temporary workaround. But in my opinion, this belongs to system level.

@temuri416
Copy link
Contributor Author

@phalcon

Can you please update me - is this categorized as "won't fix" ?

Thanks!

@phalcon
Copy link
Collaborator

phalcon commented Sep 30, 2013

#1238 is merged on 1.3.0

I'm keeping this open, if anyone wants to implement this

@ghost
Copy link

ghost commented Oct 1, 2013

See #1319

$di = new \Phalcon\Di();
$di->set('resolved', function() { return new Whatever(); });
$di->set('notresolved', function() { return new Whatever(); });

$this->assertFalse($di->getService('resolved')->isResolved());
$this->assertFalse($di->getService('notresolved')->isResolved());

$di->get('resolved');

$this->assertTrue($di->getService('resolved')->isResolved());
$this->assertFalse($di->getService('notresolved')->isResolved());

@phalcon
Copy link
Collaborator

phalcon commented Oct 2, 2013

This is implemented in 1.3.0

@phalcon phalcon closed this as completed Oct 2, 2013
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

1 participant