omnipay
omnipay copied to clipboard
Get list of ALL gateways
Hello What is the best way to list ALL gateways including the unofficial ones? \Omnipay\Omnipay::getSupportedGateways() only returns an array of predefined "official" gateways
So far I did it like so:
/**
* Retuns an array of registered namespaces from composer's autoload file
* @return array
*/
protected function getGatewayNamespaces()
{
$file = __DIR__ . '/vendor/composer/autoload_psr4.php';
if (is_readable($file)) {
$namespaces = include $file;
return array_keys($namespaces);
}
return array();
}
/**
* Returns an array of gateway IDs (short names) extracted from the registered namespaces
* @return array
*/
public function getGatewayIds()
{
$gateways = array();
foreach ($this->getGatewayNamespaces() as $namespace) {
if (strpos($namespace, 'Omnipay') !== 0) {
continue;
}
$matches = array();
preg_match('/Omnipay\\\(.+?)\\\/', $namespace, $matches);
if (isset($matches[1])) {
$gateways[] = trim($matches[1]);
}
}
return $gateways;
}
/**
* Returns an array of ALL registered gateway instances
* @return array
*/
public function getGatewayInstances()
{
foreach ($this->getGatewayIds() as $id) {
$class = \Omnipay\Common\Helper::getGatewayClassName($id);
if (class_exists($class)) {
\Omnipay\Omnipay::register($id);
}
}
$registered = array();
foreach (\Omnipay\Omnipay::find() as $id) {
$registered[$id] = \Omnipay\Omnipay::create($id);
}
return $registered;
}
Is there a better method?
That's pretty genius TBH. I've been looking for a way to automatically get a list of the installed drivers for the example application. Since they all use PSR-4, and all use composer, this is a great way to get a list of installed drivers.
The application does need to know where the vendor directory is. I don't believe there is a general way to discover it from anything in the global space. So your function here would need to know in advance where it is in relation to the vendor directory. If using a framework of some sort, then that may have some configuration settings you can query.
I believe getSupportedGateways() is gone or going anyway, since it was only used by the example application, and was very difficult to maintain.
So your function here would need to know in advance where it is in relation to the vendor directory.
Do you mean
$file = __DIR__ . '/vendor/composer/autoload_psr4.php'; ?
Yes, you're right, but in my case it will always work. I'm worring about namespaces. Can we rely on them?
For example, will GatewayID in namespace \Omnipay\GatewayID\... always point to the corresponding gateway GatewayID
Hmm, probably not. There isn't - so far as I know - a way to ask a base class if it happens to be an OmniPay gateway and what services it supports. I feel like a discovery metadata class is needed for each gateway so it can be queried as to what it is.
Unless there are any other bright ideas I've overlooked, it seems that you always need to start with a list of known OmniPay gateways, then go look for the classes to see if they are installed.
The top-level classes (*Gateway suffix) for each driver will extend Omnipay\Common\AbstractGateway, which is the only thing I can see they have in common, but how you get that list of classes I don't know. You may be able to see that \Omnipay\AuthorizeNet\ is PSR-4 autoloaded, but how do you then discover that \Omnipay\AuthorizeNet\AIMGateway (or CIMGateway or whatever) exists so you can check it extends AbstractGateway and is therefore an OmniPay gateway? Maybe composer has a way to give you this list?
https://packagist.org/packages/omnipay/common/dependents
Outsourcing some of the metadata capture :-) That's handy. But 363 of them! I thought there was 50 or 60!
How about the API details once you have the gateway package name? With Authorize.Net you have AIM, CIM and DPM, but where should these be listed so they can be discovered? I'm not sure if this is the kind of detail that OP is looking for, but it seems essential to me.