1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123:
<?php
/**
* Omnipay Gateway Factory class
*/
namespace Omnipay\Common;
use Guzzle\Http\ClientInterface;
use Omnipay\Common\Exception\RuntimeException;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
/**
* Omnipay Gateway Factory class
*
* This class abstracts a set of gateways that can be independently
* registered, accessed, and used.
*
* Note that static calls to the Omnipay class are routed to this class by
* the static call router (__callStatic) in Omnipay.
*
* Example:
*
* <code>
* // Create a gateway for the PayPal ExpressGateway
* // (routes to GatewayFactory::create)
* $gateway = Omnipay::create('ExpressGateway');
* </code>
*
* @see Omnipay\Omnipay
*/
class GatewayFactory
{
/**
* Internal storage for all available gateways
*
* @var array
*/
private $gateways = array();
/**
* All available gateways
*
* @return array An array of gateway names
*/
public function all()
{
return $this->gateways;
}
/**
* Replace the list of available gateways
*
* @param array $gateways An array of gateway names
*/
public function replace(array $gateways)
{
$this->gateways = $gateways;
}
/**
* Register a new gateway
*
* @param string $className Gateway name
*/
public function register($className)
{
if (!in_array($className, $this->gateways)) {
$this->gateways[] = $className;
}
}
/**
* Automatically find and register all officially supported gateways
*
* @return array An array of gateway names
*/
public function find()
{
foreach ($this->getSupportedGateways() as $gateway) {
$class = Helper::getGatewayClassName($gateway);
if (class_exists($class)) {
$this->register($gateway);
}
}
ksort($this->gateways);
return $this->all();
}
/**
* Create a new gateway instance
*
* @param string $class Gateway name
* @param ClientInterface|null $httpClient A Guzzle HTTP Client implementation
* @param HttpRequest|null $httpRequest A Symfony HTTP Request implementation
* @throws RuntimeException If no such gateway is found
* @return GatewayInterface An object of class $class is created and returned
*/
public function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
{
$class = Helper::getGatewayClassName($class);
if (!class_exists($class)) {
throw new RuntimeException("Class '$class' not found");
}
return new $class($httpClient, $httpRequest);
}
/**
* Get a list of supported gateways which may be available
*
* @return array
*/
public function getSupportedGateways()
{
$package = json_decode(file_get_contents(__DIR__.'/../../../composer.json'), true);
return $package['extra']['gateways'];
}
}