vendor/payum/payum/src/Payum/Core/CoreGatewayFactory.php line 218

Open in your IDE?
  1. <?php
  2. namespace Payum\Core;
  3. use Http\Adapter\Guzzle7\Client as HttpGuzzle7Client;
  4. use Http\Adapter\Guzzle6\Client as HttpGuzzle6Client;
  5. use Http\Adapter\Guzzle5\Client as HttpGuzzle5Client;
  6. use Http\Adapter\Buzz\Client as HttpBuzzClient;
  7. use Http\Client\Curl\Client as HttpCurlClient;
  8. use Http\Client\Socket\Client as HttpSocketClient;
  9. use Http\Discovery\HttpClientDiscovery;
  10. use Http\Discovery\MessageFactoryDiscovery;
  11. use Http\Discovery\StreamFactoryDiscovery;
  12. use Http\Message\MessageFactory\DiactorosMessageFactory;
  13. use Http\Message\MessageFactory\GuzzleMessageFactory;
  14. use Http\Message\StreamFactory\DiactorosStreamFactory;
  15. use Http\Message\StreamFactory\GuzzleStreamFactory;
  16. use Nyholm\Psr7\Factory\HttplugFactory;
  17. use Payum\Core\Action\AuthorizePaymentAction;
  18. use Payum\Core\Action\CapturePaymentAction;
  19. use Payum\Core\Action\ExecuteSameRequestWithModelDetailsAction;
  20. use Payum\Core\Action\GetCurrencyAction;
  21. use Payum\Core\Action\GetTokenAction;
  22. use Payum\Core\Action\PayoutPayoutAction;
  23. use Payum\Core\Bridge\Httplug\HttplugClient;
  24. use Payum\Core\Bridge\PlainPhp\Action\GetHttpRequestAction;
  25. use Payum\Core\Bridge\Spl\ArrayObject;
  26. use Payum\Core\Bridge\Twig\Action\RenderTemplateAction;
  27. use Payum\Core\Bridge\Twig\TwigUtil;
  28. use Payum\Core\Extension\EndlessCycleDetectorExtension;
  29. use Symfony\Component\HttpClient\HttplugClient as SymfonyHttplugClient;
  30. use Twig\Environment;
  31. use Twig\Loader\ChainLoader;
  32. class CoreGatewayFactory implements GatewayFactoryInterface
  33. {
  34.     /**
  35.      * @var array
  36.      */
  37.     protected $defaultConfig;
  38.     /**
  39.      * @param array $defaultConfig
  40.      */
  41.     public function __construct(array $defaultConfig = [])
  42.     {
  43.         $this->defaultConfig $defaultConfig;
  44.     }
  45.     /**
  46.      * {@inheritDoc}
  47.      */
  48.     public function create(array $config = [])
  49.     {
  50.         $config ArrayObject::ensureArrayObject($config);
  51.         $config->defaults($this->createConfig());
  52.         $gateway = new Gateway();
  53.         $this->buildClosures($config);
  54.         $this->buildActions($gateway$config);
  55.         $this->buildApis($gateway$config);
  56.         $this->buildExtensions($gateway$config);
  57.         return $gateway;
  58.     }
  59.     /**
  60.      * {@inheritDoc}
  61.      */
  62.     public function createConfig(array $config = [])
  63.     {
  64.         $config ArrayObject::ensureArrayObject($config);
  65.         $config->defaults($this->defaultConfig);
  66.         $config->defaults([
  67.             'httplug.message_factory'=>function (ArrayObject $config) {
  68.                 if (class_exists(MessageFactoryDiscovery::class)) {
  69.                     return MessageFactoryDiscovery::find();
  70.                 }
  71.                 if (class_exists(\GuzzleHttp\Psr7\Request::class)) {
  72.                     return new GuzzleMessageFactory();
  73.                 }
  74.                 if (class_exists(\Laminas\Diactoros\Request::class) || class_exists(\Zend\Diactoros\Request::class)) {
  75.                     return new DiactorosMessageFactory();
  76.                 }
  77.                 if (class_exists(\Nyholm\Psr7\Request::class)) {
  78.                     return new HttplugFactory();
  79.                 }
  80.                 throw new \LogicException('The httplug.message_factory could not be guessed. Install one of the following packages: php-http/guzzle6-adapter, zendframework/zend-diactoros. You can also overwrite the config option with your implementation.');
  81.             },
  82.             'httplug.stream_factory'=>function (ArrayObject $config) {
  83.                 if (class_exists(StreamFactoryDiscovery::class)) {
  84.                     return StreamFactoryDiscovery::find();
  85.                 }
  86.                 if (class_exists(\GuzzleHttp\Psr7\Request::class)) {
  87.                     return new GuzzleStreamFactory();
  88.                 }
  89.                 if (class_exists(\Zend\Diactoros\Request::class)) {
  90.                     return new DiactorosStreamFactory();
  91.                 }
  92.                 if (class_exists(\Nyholm\Psr7\Request::class)) {
  93.                     return new HttplugFactory();
  94.                 }
  95.                 throw new \LogicException('The httplug.stream_factory could not be guessed. Install one of the following packages: php-http/guzzle6-adapter, zendframework/zend-diactoros. You can also overwrite the config option with your implementation.');
  96.             },
  97.             'httplug.client'=>function (ArrayObject $config) {
  98.                 if (class_exists(HttpClientDiscovery::class)) {
  99.                     return HttpClientDiscovery::find();
  100.                 }
  101.                 if (class_exists(HttpGuzzle7Client::class)) {
  102.                     return new HttpGuzzle7Client();
  103.                 }
  104.                 if (class_exists(HttpGuzzle6Client::class)) {
  105.                     return new HttpGuzzle6Client();
  106.                 }
  107.                 if (class_exists(HttpGuzzle5Client::class)) {
  108.                     return new HttpGuzzle5Client();
  109.                 }
  110.                 if (class_exists(SymfonyHttplugClient::class)) {
  111.                     return new SymfonyHttplugClient();
  112.                 }
  113.                 if (class_exists(HttpSocketClient::class)) {
  114.                     return new HttpSocketClient();
  115.                 }
  116.                 if (class_exists(HttpCurlClient::class)) {
  117.                     return new HttpCurlClient($config['httplug.message_factory'], $config['httplug.stream_factory']);
  118.                 }
  119.                 if (class_exists(HttpBuzzClient::class)) {
  120.                     return new HttpBuzzClient();
  121.                 }
  122.                 throw new \LogicException('The httplug.client could not be guessed. Install one of the following packages: php-http/guzzle7-adapter, php-http/guzzle6-adapter. You can also overwrite the config option with your implementation.');
  123.             },
  124.             'payum.http_client'=>function (ArrayObject $config) {
  125.                 return new HttplugClient($config['httplug.client']);
  126.             },
  127.             'payum.template.layout' => '@PayumCore/layout.html.twig',
  128.             'twig.env' => function () {
  129.                 return new Environment(new ChainLoader());
  130.             },
  131.             'twig.register_paths' => function (ArrayObject $config) {
  132.                 $twig $config['twig.env'];
  133.                 if (false == $twig instanceof Environment) {
  134.                     throw new \LogicException(sprintf(
  135.                         'The `twig.env config option must contains instance of Twig\Environment but got %s`',
  136.                         is_object($twig) ? get_class($twig) : gettype($twig)
  137.                     ));
  138.                 }
  139.                 TwigUtil::registerPaths($twig$config['payum.paths']);
  140.                 return null;
  141.             },
  142.             'payum.action.get_http_request' => new GetHttpRequestAction(),
  143.             'payum.action.capture_payment' => new CapturePaymentAction(),
  144.             'payum.action.authorize_payment' => new AuthorizePaymentAction(),
  145.             'payum.action.payout_payout' => new PayoutPayoutAction(),
  146.             'payum.action.execute_same_request_with_model_details' => new ExecuteSameRequestWithModelDetailsAction(),
  147.             'payum.action.render_template' => function (ArrayObject $config) {
  148.                 return new RenderTemplateAction($config['twig.env'], $config['payum.template.layout']);
  149.             },
  150.             'payum.extension.endless_cycle_detector' => new EndlessCycleDetectorExtension(),
  151.             'payum.action.get_currency' => function (ArrayObject $config) {
  152.                 return new GetCurrencyAction($config['payum.iso4217']);
  153.             },
  154.             'payum.prepend_actions' => [],
  155.             'payum.prepend_extensions' => [],
  156.             'payum.prepend_apis' => [],
  157.             'payum.default_options' => [],
  158.             'payum.required_options' => [],
  159.             'payum.api.http_client' => function (ArrayObject $config) {
  160.                 return $config['payum.http_client'];
  161.             },
  162.             'payum.security.token_storage' => null,
  163.         ]);
  164.         if ($config['payum.security.token_storage']) {
  165.             $config['payum.action.get_token'] = function (ArrayObject $config) {
  166.                 return new GetTokenAction($config['payum.security.token_storage']);
  167.             };
  168.         }
  169.         $config['payum.paths'] = array_replace([
  170.             'PayumCore' => __DIR__.'/Resources/views',
  171.         ], $config['payum.paths'] ?: []);
  172.         return (array) $config;
  173.     }
  174.     /**
  175.      * @param ArrayObject $config
  176.      */
  177.     protected function buildClosures(ArrayObject $config)
  178.     {
  179.         // with higher priority
  180.         foreach (['httplug.message_factory''httplug.stream_factory''httplug.client''payum.http_client''payum.paths''twig.env''twig.register_paths'] as $name) {
  181.             $value $config[$name];
  182.             if (is_callable($value)) {
  183.                 $config[$name] = call_user_func($value$config);
  184.             }
  185.         }
  186.         foreach ($config as $name => $value) {
  187.             if (is_callable($value) && !(is_string($value) && function_exists('\\'.$value))) {
  188.                 $config[$name] = call_user_func($value$config);
  189.             }
  190.         }
  191.     }
  192.     /**
  193.      * @param Gateway     $gateway
  194.      * @param ArrayObject $config
  195.      */
  196.     protected function buildActions(Gateway $gatewayArrayObject $config)
  197.     {
  198.         foreach ($config as $name => $value) {
  199.             if (=== strpos($name'payum.action')) {
  200.                 $prepend in_array($name$config['payum.prepend_actions']);
  201.                 $gateway->addAction($value$prepend);
  202.             }
  203.         }
  204.     }
  205.     /**
  206.      * @param Gateway     $gateway
  207.      * @param ArrayObject $config
  208.      */
  209.     protected function buildApis(Gateway $gatewayArrayObject $config)
  210.     {
  211.         foreach ($config as $name => $value) {
  212.             if (=== strpos($name'payum.api')) {
  213.                 $prepend in_array($name$config['payum.prepend_apis']);
  214.                 $gateway->addApi($value$prepend);
  215.             }
  216.         }
  217.     }
  218.     /**
  219.      * @param Gateway     $gateway
  220.      * @param ArrayObject $config
  221.      */
  222.     protected function buildExtensions(Gateway $gatewayArrayObject $config)
  223.     {
  224.         foreach ($config as $name => $value) {
  225.             if (=== strpos($name'payum.extension')) {
  226.                 $prepend in_array($name$config['payum.prepend_extensions']);
  227.                 $gateway->addExtension($value$prepend);
  228.             }
  229.         }
  230.     }
  231. }