I’m currently building a small console (symfony 2.1) app (handling semantic version numbers) that can optionally analyze a given git repository, if gitlib is available. Ideally I would prefer some mechanism provided by composer to detect whether the lib is available (similar to the provided autoload), but as far as I can see I have to check myself.
I’m torn between parsing the composer.lock (seems cleaner) and try-ing to instantiate a class from the library (independent of composer, but abusing the Exception for flow control). The latter feels particularly bad, because I would have to do it even if I’m not going to use the instance at all, to configure the app (specifically: to decide whether or not to register the Command in the Application at all).
Any recommendations/suggestions?
PHP provides you with
class_exists()for just that purpose. Check if the class is loaded with it – it will call the autoloader by default so no need to instantiate it with a try/catch block beforehand.If you are trying to test for an extension by the way you can also use
extension_loaded()orfunction_exists().