|
2 | 2 |
|
3 | 3 | namespace Omnipay\Common;
|
4 | 4 |
|
5 |
| -use RecursiveDirectoryIterator; |
6 |
| -use RecursiveIteratorIterator; |
7 |
| -use ReflectionClass; |
8 | 5 | use Guzzle\Http\ClientInterface;
|
9 | 6 | use Omnipay\Common\Exception\RuntimeException;
|
10 | 7 | use Symfony\Component\HttpFoundation\Request as HttpRequest;
|
11 | 8 |
|
12 | 9 | class GatewayFactory
|
13 | 10 | {
|
14 |
| - public static function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null) |
15 |
| - { |
16 |
| - $class = Helper::getGatewayClassName($class); |
| 11 | + private static $gateways = array(); |
17 | 12 |
|
18 |
| - if (!class_exists($class)) { |
19 |
| - throw new RuntimeException("Class '$class' not found"); |
20 |
| - } |
| 13 | + /** |
| 14 | + * All available gateways |
| 15 | + * |
| 16 | + * @return array An array of gateway names |
| 17 | + */ |
| 18 | + public static function all() |
| 19 | + { |
| 20 | + return static::$gateways; |
| 21 | + } |
21 | 22 |
|
22 |
| - $gateway = new $class($httpClient, $httpRequest); |
| 23 | + /** |
| 24 | + * Replace the list of available gateways |
| 25 | + * |
| 26 | + * @param array $gateways An array of gateway names |
| 27 | + */ |
| 28 | + public static function replace(array $gateways) |
| 29 | + { |
| 30 | + static::$gateways = $gateways; |
| 31 | + } |
23 | 32 |
|
24 |
| - return $gateway; |
| 33 | + /** |
| 34 | + * Register a new gateway |
| 35 | + * |
| 36 | + * @param string $className Gateway name |
| 37 | + */ |
| 38 | + public static function register($className) |
| 39 | + { |
| 40 | + static::$gateways[] = $className; |
25 | 41 | }
|
26 | 42 |
|
27 | 43 | /**
|
28 |
| - * Get a list of supported gateways, in friendly format (e.g. PayPal_Express) |
| 44 | + * Create a new gateway instance |
| 45 | + * |
| 46 | + * @param string $class Gateway name |
| 47 | + * @param ClientInterface|null $httpClient A Guzzle HTTP Client implementation |
| 48 | + * @param HttpRequest|null $httpRequest A Symfony HTTP Request implementation |
29 | 49 | */
|
30 |
| - public static function find($directory = null) |
| 50 | + public static function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null) |
31 | 51 | {
|
32 |
| - $result = array(); |
33 |
| - |
34 |
| - // find all gateways in the Billing directory |
35 |
| - $directory = dirname(__DIR__); |
36 |
| - $it = new RecursiveDirectoryIterator($directory); |
37 |
| - foreach (new RecursiveIteratorIterator($it) as $file) { |
38 |
| - $filepath = $file->getPathName(); |
39 |
| - if ('Gateway.php' === substr($filepath, -11)) { |
40 |
| - // determine class name |
41 |
| - $type = substr($filepath, 0, -11); |
42 |
| - $type = str_replace(array($directory, DIRECTORY_SEPARATOR), array('', '_'), $type); |
43 |
| - $type = trim($type, '_'); |
44 |
| - $class = Helper::getGatewayClassName($type); |
45 |
| - |
46 |
| - // ensure class exists and is not abstract |
47 |
| - if (class_exists($class)) { |
48 |
| - $reflection = new ReflectionClass($class); |
49 |
| - if (!$reflection->isAbstract() and |
50 |
| - $reflection->implementsInterface('\\Omnipay\\Common\\GatewayInterface')) { |
51 |
| - $result[] = $type; |
52 |
| - } |
53 |
| - } |
54 |
| - } |
| 52 | + $class = Helper::getGatewayClassName($class); |
| 53 | + |
| 54 | + if (!class_exists($class)) { |
| 55 | + throw new RuntimeException("Class '$class' not found"); |
55 | 56 | }
|
56 | 57 |
|
57 |
| - return $result; |
| 58 | + return new $class($httpClient, $httpRequest); |
58 | 59 | }
|
59 | 60 | }
|
0 commit comments