We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Is the Service unresolved in DI?

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.

Is there a way of knowing whether "whatever" has been resolved?

Thanks, Temuri

I'm on the purpose that you to create a class that extends phalcon DI.

In it you redefine the get method ; add the _isresolved propertie ; and a isResolved Method:

// make your class
// Class MyDi extend Phalcon\DI...

protected $_resolvedList;

public functoin __construct(){
       parent::__construct();
       $_resolvedList = array();
}

public function get($name,$parameters=array()){
     $this->_resolvedList[$name] = true;
    return parrent::get($name,$parameters);
}

// according to the doc there is also getRaw, getService, getShared they may need to be redefined (need to check the sources to know if they need to be redefined)

public function isResolved($name){
     return isset( $this->_resolvedList[$name] ) &&  $this->_resolvedList[$name] === true;
}

Then you can use it :

$di = new MyDi(); // intead of $di = new Phalcon\Di;

Doc : https://api.phalcon.io/en/1.2.3/Phalcon/DI#get-details



51.4k

Thanks, that's an obvious solution. However, that adds a complexity of supplying your own DI object into all DI instantiations.

What I'd like to do is to simply fetch that information from default DI container.

Thanks

Phalcon shouldn't solve all your problem. It is framework to write your own app.

YOU STILL NEED TO WRITE YOUR OWN CODE

If you can, you should extend DI (I do this in every app) and maybe create pull request in phalcon/incubator. That way people who need it will benefit from your work and people who don't want, will be happy you didn't slow down framework.



51.4k

@Tomasz:

I agree that Phalcon should not solve developer's specific problems, however, IMO, this should be available on system/extension level as fundamental functionality.