From b76e2ff898b23745d4c9aaee49eeb7d88f2896ab Mon Sep 17 00:00:00 2001 From: Carson Fleming Date: Thu, 2 Mar 2017 22:49:24 -0500 Subject: Updated mailgun plugin --- inc/mailgun/php-http/discovery/CHANGELOG.md | 179 ++++++++++++++++++ inc/mailgun/php-http/discovery/LICENSE | 19 ++ inc/mailgun/php-http/discovery/README.md | 46 +++++ inc/mailgun/php-http/discovery/composer.json | 48 +++++ .../php-http/discovery/src/ClassDiscovery.php | 207 +++++++++++++++++++++ inc/mailgun/php-http/discovery/src/Exception.php | 12 ++ .../ClassInstantiationFailedException.php | 14 ++ .../src/Exception/DiscoveryFailedException.php | 51 +++++ .../discovery/src/Exception/NotFoundException.php | 16 ++ .../src/Exception/PuliUnavailableException.php | 12 ++ .../src/Exception/StrategyUnavailableException.php | 15 ++ .../discovery/src/HttpAsyncClientDiscovery.php | 36 ++++ .../php-http/discovery/src/HttpClientDiscovery.php | 36 ++++ .../discovery/src/MessageFactoryDiscovery.php | 36 ++++ .../php-http/discovery/src/NotFoundException.php | 14 ++ .../src/Strategy/CommonClassesStrategy.php | 76 ++++++++ .../discovery/src/Strategy/DiscoveryStrategy.php | 23 +++ .../discovery/src/Strategy/MockClientStrategy.php | 24 +++ .../discovery/src/Strategy/PuliBetaStrategy.php | 91 +++++++++ .../discovery/src/StreamFactoryDiscovery.php | 36 ++++ .../php-http/discovery/src/UriFactoryDiscovery.php | 36 ++++ 21 files changed, 1027 insertions(+) create mode 100644 inc/mailgun/php-http/discovery/CHANGELOG.md create mode 100644 inc/mailgun/php-http/discovery/LICENSE create mode 100644 inc/mailgun/php-http/discovery/README.md create mode 100644 inc/mailgun/php-http/discovery/composer.json create mode 100644 inc/mailgun/php-http/discovery/src/ClassDiscovery.php create mode 100644 inc/mailgun/php-http/discovery/src/Exception.php create mode 100644 inc/mailgun/php-http/discovery/src/Exception/ClassInstantiationFailedException.php create mode 100644 inc/mailgun/php-http/discovery/src/Exception/DiscoveryFailedException.php create mode 100644 inc/mailgun/php-http/discovery/src/Exception/NotFoundException.php create mode 100644 inc/mailgun/php-http/discovery/src/Exception/PuliUnavailableException.php create mode 100644 inc/mailgun/php-http/discovery/src/Exception/StrategyUnavailableException.php create mode 100644 inc/mailgun/php-http/discovery/src/HttpAsyncClientDiscovery.php create mode 100644 inc/mailgun/php-http/discovery/src/HttpClientDiscovery.php create mode 100644 inc/mailgun/php-http/discovery/src/MessageFactoryDiscovery.php create mode 100644 inc/mailgun/php-http/discovery/src/NotFoundException.php create mode 100644 inc/mailgun/php-http/discovery/src/Strategy/CommonClassesStrategy.php create mode 100644 inc/mailgun/php-http/discovery/src/Strategy/DiscoveryStrategy.php create mode 100644 inc/mailgun/php-http/discovery/src/Strategy/MockClientStrategy.php create mode 100644 inc/mailgun/php-http/discovery/src/Strategy/PuliBetaStrategy.php create mode 100644 inc/mailgun/php-http/discovery/src/StreamFactoryDiscovery.php create mode 100644 inc/mailgun/php-http/discovery/src/UriFactoryDiscovery.php (limited to 'inc/mailgun/php-http/discovery') diff --git a/inc/mailgun/php-http/discovery/CHANGELOG.md b/inc/mailgun/php-http/discovery/CHANGELOG.md new file mode 100644 index 0000000..0c56331 --- /dev/null +++ b/inc/mailgun/php-http/discovery/CHANGELOG.md @@ -0,0 +1,179 @@ +# Change Log + + +## 1.2.1 - 2017-03-02 + +### Fixed + +- Fixed minor issue with `MockClientStrategy`, also added more tests. + +## 1.2.0 - 2017-02-12 + +### Added + +- MockClientStrategy class. + +## 1.1.1 - 2016-11-27 + +### Changed + +- Made exception messages clearer. `StrategyUnavailableException` is no longer the previous exception to `DiscoveryFailedException`. +- `CommonClassesStrategy` is using `self` instead of `static`. Using `static` makes no sense when `CommonClassesStrategy` is final. + +## 1.1.0 - 2016-10-20 + +### Added + +- Discovery support for Slim Framework factories + +## 1.0.0 - 2016-07-18 + +### Added + +- Added back `Http\Discovery\NotFoundException` to preserve BC with 0.8 version. You may upgrade from 0.8.x and 0.9.x to 1.0.0 without any BC breaks. +- Added interface `Http\Discovery\Exception` which is implemented by all our exceptions + +### Changed + +- Puli strategy renamed to Puli Beta strategy to prevent incompatibility with a future Puli stable + +### Deprecated + +- For BC reasons, the old `Http\Discovery\NotFoundException` (extending the new exception) will be thrown until version 2.0 + + +## 0.9.1 - 2016-06-28 + +### Changed + +- Dropping PHP 5.4 support because we use the ::class constant. + + +## 0.9.0 - 2016-06-25 + +### Added + +- Discovery strategies to find classes + +### Changed + +- [Puli](http://puli.io) made optional +- Improved exceptions +- **[BC] `NotFoundException` moved to `Http\Discovery\Exception\NotFoundException`** + + +## 0.8.0 - 2016-02-11 + +### Changed + +- Puli composer plugin must be installed separately + + +## 0.7.0 - 2016-01-15 + +### Added + +- Temporary puli.phar (Beta 10) executable + +### Changed + +- Updated HTTPlug dependencies +- Updated Puli dependencies +- Local configuration to make tests passing + +### Removed + +- Puli CLI dependency + + +## 0.6.4 - 2016-01-07 + +### Fixed + +- Puli [not working](https://twitter.com/PuliPHP/status/685132540588507137) with the latest json-schema + + +## 0.6.3 - 2016-01-04 + +### Changed + +- Adjust Puli dependencies + + +## 0.6.2 - 2016-01-04 + +### Changed + +- Make Puli CLI a requirement + + +## 0.6.1 - 2016-01-03 + +### Changed + +- More flexible Puli requirement + + +## 0.6.0 - 2015-12-30 + +### Changed + +- Use [Puli](http://puli.io) for discovery +- Improved exception messages + + +## 0.5.0 - 2015-12-25 + +### Changed + +- Updated message factory dependency (php-http/message) + + +## 0.4.0 - 2015-12-17 + +### Added + +- Array condition evaluation in the Class Discovery + +### Removed + +- Message factories (moved to php-http/utils) + + +## 0.3.0 - 2015-11-18 + +### Added + +- HTTP Async Client Discovery +- Stream factories + +### Changed + +- Discoveries and Factories are final +- Message and Uri factories have the type in their names +- Diactoros Message factory uses Stream factory internally + +### Fixed + +- Improved docblocks for API documentation generation + + +## 0.2.0 - 2015-10-31 + +### Changed + +- Renamed AdapterDiscovery to ClientDiscovery + + +## 0.1.1 - 2015-06-13 + +### Fixed + +- Bad HTTP Adapter class name for Guzzle 5 + + +## 0.1.0 - 2015-06-12 + +### Added + +- Initial release diff --git a/inc/mailgun/php-http/discovery/LICENSE b/inc/mailgun/php-http/discovery/LICENSE new file mode 100644 index 0000000..4558d6f --- /dev/null +++ b/inc/mailgun/php-http/discovery/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2015-2016 PHP HTTP Team + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/inc/mailgun/php-http/discovery/README.md b/inc/mailgun/php-http/discovery/README.md new file mode 100644 index 0000000..7c5151e --- /dev/null +++ b/inc/mailgun/php-http/discovery/README.md @@ -0,0 +1,46 @@ +# HTTPlug Discovery + +[![Latest Version](https://img.shields.io/github/release/php-http/discovery.svg?style=flat-square)](https://github.com/php-http/discovery/releases) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) +[![Build Status](https://img.shields.io/travis/php-http/discovery.svg?style=flat-square)](https://travis-ci.org/php-http/discovery) +[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/php-http/discovery.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/discovery) +[![Quality Score](https://img.shields.io/scrutinizer/g/php-http/discovery.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/discovery) +[![Total Downloads](https://img.shields.io/packagist/dt/php-http/discovery.svg?style=flat-square)](https://packagist.org/packages/php-http/discovery) + +**Finds installed HTTPlug implementations and PSR-7 message factories.** + + +## Install + +Via Composer + +``` bash +$ composer require php-http/discovery +``` + + +## Documentation + +Please see the [official documentation](http://php-http.readthedocs.org/en/latest/discovery.html). + + +## Testing + +``` bash +$ composer test +``` + + +## Contributing + +Please see our [contributing guide](http://docs.php-http.org/en/latest/development/contributing.html). + + +## Security + +If you discover any security related issues, please contact us at [security@php-http.org](mailto:security@php-http.org). + + +## License + +The MIT License (MIT). Please see [License File](LICENSE) for more information. diff --git a/inc/mailgun/php-http/discovery/composer.json b/inc/mailgun/php-http/discovery/composer.json new file mode 100644 index 0000000..128af59 --- /dev/null +++ b/inc/mailgun/php-http/discovery/composer.json @@ -0,0 +1,48 @@ +{ + "name": "php-http/discovery", + "description": "Finds installed HTTPlug implementations and PSR-7 message factories", + "license": "MIT", + "keywords": ["http", "discovery", "client", "adapter", "message", "factory", "psr7"], + "homepage": "http://php-http.org", + "authors": [ + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com" + } + ], + "require": { + "php": "^5.5 || ^7.0" + }, + "require-dev": { + "php-http/httplug": "^1.0", + "php-http/message-factory": "^1.0", + "puli/composer-plugin": "1.0.0-beta10", + "phpspec/phpspec": "^2.4", + "henrikbjorn/phpspec-code-coverage" : "^2.0.2" + }, + "suggest": { + "puli/composer-plugin": "Sets up Puli which is recommended for Discovery to work. Check http://docs.php-http.org/en/latest/discovery.html for more details.", + "php-http/message": "Allow to use Guzzle, Diactoros or Slim Framework factories" + }, + "autoload": { + "psr-4": { + "Http\\Discovery\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "spec\\Http\\Discovery\\": "spec/" + } + }, + "scripts": { + "test": "vendor/bin/phpspec run", + "test-ci": "vendor/bin/phpspec run -c phpspec.ci.yml" + }, + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "prefer-stable": true, + "minimum-stability": "beta" +} diff --git a/inc/mailgun/php-http/discovery/src/ClassDiscovery.php b/inc/mailgun/php-http/discovery/src/ClassDiscovery.php new file mode 100644 index 0000000..2c3e877 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/ClassDiscovery.php @@ -0,0 +1,207 @@ + + * @author Márk Sági-Kazár + * @author Tobias Nyholm + */ +abstract class ClassDiscovery +{ + /** + * A list of strategies to find classes. + * + * @var array + */ + private static $strategies = [ + Strategy\PuliBetaStrategy::class, + Strategy\CommonClassesStrategy::class, + ]; + + /** + * Discovery cache to make the second time we use discovery faster. + * + * @var array + */ + private static $cache = []; + + /** + * Finds a class. + * + * @param string $type + * + * @return string|\Closure + * + * @throws DiscoveryFailedException + */ + protected static function findOneByType($type) + { + // Look in the cache + if (null !== ($class = self::getFromCache($type))) { + return $class; + } + + $exceptions = []; + foreach (self::$strategies as $strategy) { + try { + $candidates = call_user_func($strategy.'::getCandidates', $type); + } catch (StrategyUnavailableException $e) { + $exceptions[] = $e; + continue; + } + + foreach ($candidates as $candidate) { + if (isset($candidate['condition'])) { + if (!self::evaluateCondition($candidate['condition'])) { + continue; + } + } + + // save the result for later use + self::storeInCache($type, $candidate); + + return $candidate['class']; + } + } + + throw DiscoveryFailedException::create($exceptions); + } + + /** + * Get a value from cache. + * + * @param string $type + * + * @return string|null + */ + private static function getFromCache($type) + { + if (!isset(self::$cache[$type])) { + return; + } + + $candidate = self::$cache[$type]; + if (isset($candidate['condition'])) { + if (!self::evaluateCondition($candidate['condition'])) { + return; + } + } + + return $candidate['class']; + } + + /** + * Store a value in cache. + * + * @param string $type + * @param string $class + */ + private static function storeInCache($type, $class) + { + self::$cache[$type] = $class; + } + + /** + * Set new strategies and clear the cache. + * + * @param array $strategies string array of fully qualified class name to a DiscoveryStrategy + */ + public static function setStrategies(array $strategies) + { + self::$strategies = $strategies; + self::clearCache(); + } + + /** + * Append a strategy at the end of the strategy queue. + * + * @param string $strategy Fully qualified class name to a DiscoveryStrategy + */ + public static function appendStrategy($strategy) + { + self::$strategies[] = $strategy; + self::clearCache(); + } + + /** + * Prepend a strategy at the beginning of the strategy queue. + * + * @param string $strategy Fully qualified class name to a DiscoveryStrategy + */ + public static function prependStrategy($strategy) + { + array_unshift(self::$strategies, $strategy); + self::clearCache(); + } + + /** + * Clear the cache. + */ + public static function clearCache() + { + self::$cache = []; + } + + /** + * Evaluates conditions to boolean. + * + * @param mixed $condition + * + * @return bool + */ + protected static function evaluateCondition($condition) + { + if (is_string($condition)) { + // Should be extended for functions, extensions??? + return class_exists($condition); + } elseif (is_callable($condition)) { + return $condition(); + } elseif (is_bool($condition)) { + return $condition; + } elseif (is_array($condition)) { + $evaluatedCondition = true; + + // Immediately stop execution if the condition is false + for ($i = 0; $i < count($condition) && false !== $evaluatedCondition; ++$i) { + $evaluatedCondition &= static::evaluateCondition($condition[$i]); + } + + return $evaluatedCondition; + } + + return false; + } + + /** + * Get an instance of the $class. + * + * @param string|\Closure $class A FQCN of a class or a closure that instantiate the class. + * + * @return object + * + * @throws ClassInstantiationFailedException + */ + protected static function instantiateClass($class) + { + try { + if (is_string($class)) { + return new $class(); + } + + if (is_callable($class)) { + return $class(); + } + } catch (\Exception $e) { + throw new ClassInstantiationFailedException('Unexpected exception when instantiating class.', 0, $e); + } + + throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string'); + } +} diff --git a/inc/mailgun/php-http/discovery/src/Exception.php b/inc/mailgun/php-http/discovery/src/Exception.php new file mode 100644 index 0000000..973c908 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Exception.php @@ -0,0 +1,12 @@ + + */ +interface Exception +{ +} diff --git a/inc/mailgun/php-http/discovery/src/Exception/ClassInstantiationFailedException.php b/inc/mailgun/php-http/discovery/src/Exception/ClassInstantiationFailedException.php new file mode 100644 index 0000000..e95bf5d --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Exception/ClassInstantiationFailedException.php @@ -0,0 +1,14 @@ + + */ +final class ClassInstantiationFailedException extends \RuntimeException implements Exception +{ +} diff --git a/inc/mailgun/php-http/discovery/src/Exception/DiscoveryFailedException.php b/inc/mailgun/php-http/discovery/src/Exception/DiscoveryFailedException.php new file mode 100644 index 0000000..304b727 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Exception/DiscoveryFailedException.php @@ -0,0 +1,51 @@ + + */ +final class DiscoveryFailedException extends \Exception implements Exception +{ + /** + * @var \Exception[] + */ + private $exceptions; + + /** + * @param string $message + * @param \Exception[] $exceptions + */ + public function __construct($message, array $exceptions = []) + { + $this->exceptions = $exceptions; + + parent::__construct($message); + } + + /** + * @param \Exception[] $exceptions + */ + public static function create($exceptions) + { + $message = 'Could not find resource using any discovery strategy. Find more information at http://docs.php-http.org/en/latest/discovery.html#common-errors'; + foreach ($exceptions as $e) { + $message .= "\n - ".$e->getMessage(); + } + $message .= "\n\n"; + + return new self($message, $exceptions); + } + + /** + * @return \Exception[] + */ + public function getExceptions() + { + return $this->exceptions; + } +} diff --git a/inc/mailgun/php-http/discovery/src/Exception/NotFoundException.php b/inc/mailgun/php-http/discovery/src/Exception/NotFoundException.php new file mode 100644 index 0000000..befbf48 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Exception/NotFoundException.php @@ -0,0 +1,16 @@ + + */ +/*final */class NotFoundException extends \RuntimeException implements Exception +{ +} diff --git a/inc/mailgun/php-http/discovery/src/Exception/PuliUnavailableException.php b/inc/mailgun/php-http/discovery/src/Exception/PuliUnavailableException.php new file mode 100644 index 0000000..a6ade73 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Exception/PuliUnavailableException.php @@ -0,0 +1,12 @@ + + */ +final class PuliUnavailableException extends StrategyUnavailableException +{ +} diff --git a/inc/mailgun/php-http/discovery/src/Exception/StrategyUnavailableException.php b/inc/mailgun/php-http/discovery/src/Exception/StrategyUnavailableException.php new file mode 100644 index 0000000..89ecf35 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Exception/StrategyUnavailableException.php @@ -0,0 +1,15 @@ + + */ +class StrategyUnavailableException extends \RuntimeException implements Exception +{ +} diff --git a/inc/mailgun/php-http/discovery/src/HttpAsyncClientDiscovery.php b/inc/mailgun/php-http/discovery/src/HttpAsyncClientDiscovery.php new file mode 100644 index 0000000..6ef60e7 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/HttpAsyncClientDiscovery.php @@ -0,0 +1,36 @@ + + */ +final class HttpAsyncClientDiscovery extends ClassDiscovery +{ + /** + * Finds an HTTP Async Client. + * + * @return HttpAsyncClient + * + * @throws Exception\NotFoundException + */ + public static function find() + { + try { + $asyncClient = static::findOneByType(HttpAsyncClient::class); + } catch (DiscoveryFailedException $e) { + throw new NotFoundException( + 'No HTTPlug async clients found. Make sure to install a package providing "php-http/async-client-implementation". Example: "php-http/guzzle6-adapter".', + 0, + $e + ); + } + + return static::instantiateClass($asyncClient); + } +} diff --git a/inc/mailgun/php-http/discovery/src/HttpClientDiscovery.php b/inc/mailgun/php-http/discovery/src/HttpClientDiscovery.php new file mode 100644 index 0000000..2654b7e --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/HttpClientDiscovery.php @@ -0,0 +1,36 @@ + + */ +final class HttpClientDiscovery extends ClassDiscovery +{ + /** + * Finds an HTTP Client. + * + * @return HttpClient + * + * @throws Exception\NotFoundException + */ + public static function find() + { + try { + $client = static::findOneByType(HttpClient::class); + } catch (DiscoveryFailedException $e) { + throw new NotFoundException( + 'No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter".', + 0, + $e + ); + } + + return static::instantiateClass($client); + } +} diff --git a/inc/mailgun/php-http/discovery/src/MessageFactoryDiscovery.php b/inc/mailgun/php-http/discovery/src/MessageFactoryDiscovery.php new file mode 100644 index 0000000..c21b9bf --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/MessageFactoryDiscovery.php @@ -0,0 +1,36 @@ + + */ +final class MessageFactoryDiscovery extends ClassDiscovery +{ + /** + * Finds a Message Factory. + * + * @return MessageFactory + * + * @throws Exception\NotFoundException + */ + public static function find() + { + try { + $messageFactory = static::findOneByType(MessageFactory::class); + } catch (DiscoveryFailedException $e) { + throw new NotFoundException( + 'No message factories found. To use Guzzle, Diactoros or Slim Framework factories install php-http/message and the chosen message implementation.', + 0, + $e + ); + } + + return static::instantiateClass($messageFactory); + } +} diff --git a/inc/mailgun/php-http/discovery/src/NotFoundException.php b/inc/mailgun/php-http/discovery/src/NotFoundException.php new file mode 100644 index 0000000..d59dadb --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/NotFoundException.php @@ -0,0 +1,14 @@ + + * + * @deprecated since since version 1.0, and will be removed in 2.0. Use {@link \Http\Discovery\Exception\NotFoundException} instead. + */ +final class NotFoundException extends \Http\Discovery\Exception\NotFoundException +{ +} diff --git a/inc/mailgun/php-http/discovery/src/Strategy/CommonClassesStrategy.php b/inc/mailgun/php-http/discovery/src/Strategy/CommonClassesStrategy.php new file mode 100644 index 0000000..cbed15d --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Strategy/CommonClassesStrategy.php @@ -0,0 +1,76 @@ + + */ +final class CommonClassesStrategy implements DiscoveryStrategy +{ + /** + * @var array + */ + private static $classes = [ + 'Http\Message\MessageFactory' => [ + ['class' => GuzzleMessageFactory::class, 'condition' => [GuzzleRequest::class, GuzzleMessageFactory::class]], + ['class' => DiactorosMessageFactory::class, 'condition' => [DiactorosRequest::class, DiactorosMessageFactory::class]], + ['class' => SlimMessageFactory::class, 'condition' => [SlimRequest::class, SlimMessageFactory::class]], + ], + 'Http\Message\StreamFactory' => [ + ['class' => GuzzleStreamFactory::class, 'condition' => [GuzzleRequest::class, GuzzleStreamFactory::class]], + ['class' => DiactorosStreamFactory::class, 'condition' => [DiactorosRequest::class, DiactorosStreamFactory::class]], + ['class' => SlimStreamFactory::class, 'condition' => [SlimRequest::class, SlimStreamFactory::class]], + ], + 'Http\Message\UriFactory' => [ + ['class' => GuzzleUriFactory::class, 'condition' => [GuzzleRequest::class, GuzzleUriFactory::class]], + ['class' => DiactorosUriFactory::class, 'condition' => [DiactorosRequest::class, DiactorosUriFactory::class]], + ['class' => SlimUriFactory::class, 'condition' => [SlimRequest::class, SlimUriFactory::class]], + ], + 'Http\Client\HttpAsyncClient' => [ + ['class' => Guzzle6::class, 'condition' => Guzzle6::class], + ['class' => Curl::class, 'condition' => Curl::class], + ['class' => React::class, 'condition' => React::class], + ], + 'Http\Client\HttpClient' => [ + ['class' => Guzzle6::class, 'condition' => Guzzle6::class], + ['class' => Guzzle5::class, 'condition' => Guzzle5::class], + ['class' => Curl::class, 'condition' => Curl::class], + ['class' => Socket::class, 'condition' => Socket::class], + ['class' => Buzz::class, 'condition' => Buzz::class], + ['class' => React::class, 'condition' => React::class], + ], + ]; + + /** + * {@inheritdoc} + */ + public static function getCandidates($type) + { + if (isset(self::$classes[$type])) { + return self::$classes[$type]; + } + + return []; + } +} diff --git a/inc/mailgun/php-http/discovery/src/Strategy/DiscoveryStrategy.php b/inc/mailgun/php-http/discovery/src/Strategy/DiscoveryStrategy.php new file mode 100644 index 0000000..641485a --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Strategy/DiscoveryStrategy.php @@ -0,0 +1,23 @@ + + */ +interface DiscoveryStrategy +{ + /** + * Find a resource of a specific type. + * + * @param string $type + * + * @return array The return value is always an array with zero or more elements. Each + * element is an array with two keys ['class' => string, 'condition' => mixed]. + * + * @throws StrategyUnavailableException if we cannot use this strategy. + */ + public static function getCandidates($type); +} diff --git a/inc/mailgun/php-http/discovery/src/Strategy/MockClientStrategy.php b/inc/mailgun/php-http/discovery/src/Strategy/MockClientStrategy.php new file mode 100644 index 0000000..ea464cf --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Strategy/MockClientStrategy.php @@ -0,0 +1,24 @@ + + */ +final class MockClientStrategy implements DiscoveryStrategy +{ + /** + * {@inheritdoc} + */ + public static function getCandidates($type) + { + return ($type === HttpClient::class) + ? [['class' => Mock::class, 'condition' => Mock::class]] + : []; + } +} diff --git a/inc/mailgun/php-http/discovery/src/Strategy/PuliBetaStrategy.php b/inc/mailgun/php-http/discovery/src/Strategy/PuliBetaStrategy.php new file mode 100644 index 0000000..2666fb3 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/Strategy/PuliBetaStrategy.php @@ -0,0 +1,91 @@ + + * @author Márk Sági-Kazár + */ +class PuliBetaStrategy implements DiscoveryStrategy +{ + /** + * @var GeneratedPuliFactory + */ + protected static $puliFactory; + + /** + * @var Discovery + */ + protected static $puliDiscovery; + + /** + * @return GeneratedPuliFactory + * + * @throws PuliUnavailableException + */ + private static function getPuliFactory() + { + if (null === self::$puliFactory) { + if (!defined('PULI_FACTORY_CLASS')) { + throw new PuliUnavailableException('Puli Factory is not available'); + } + + $puliFactoryClass = PULI_FACTORY_CLASS; + + if (!class_exists($puliFactoryClass)) { + throw new PuliUnavailableException('Puli Factory class does not exist'); + } + + self::$puliFactory = new $puliFactoryClass(); + } + + return self::$puliFactory; + } + + /** + * Returns the Puli discovery layer. + * + * @return Discovery + * + * @throws PuliUnavailableException + */ + private static function getPuliDiscovery() + { + if (!isset(self::$puliDiscovery)) { + $factory = self::getPuliFactory(); + $repository = $factory->createRepository(); + + self::$puliDiscovery = $factory->createDiscovery($repository); + } + + return self::$puliDiscovery; + } + + /** + * {@inheritdoc} + */ + public static function getCandidates($type) + { + $returnData = []; + $bindings = self::getPuliDiscovery()->findBindings($type); + + foreach ($bindings as $binding) { + $condition = true; + if ($binding->hasParameterValue('depends')) { + $condition = $binding->getParameterValue('depends'); + } + $returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition]; + } + + return $returnData; + } +} diff --git a/inc/mailgun/php-http/discovery/src/StreamFactoryDiscovery.php b/inc/mailgun/php-http/discovery/src/StreamFactoryDiscovery.php new file mode 100644 index 0000000..7bcc8ce --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/StreamFactoryDiscovery.php @@ -0,0 +1,36 @@ + + */ +final class StreamFactoryDiscovery extends ClassDiscovery +{ + /** + * Finds a Stream Factory. + * + * @return StreamFactory + * + * @throws Exception\NotFoundException + */ + public static function find() + { + try { + $streamFactory = static::findOneByType(StreamFactory::class); + } catch (DiscoveryFailedException $e) { + throw new NotFoundException( + 'No stream factories found. To use Guzzle, Diactoros or Slim Framework factories install php-http/message and the chosen message implementation.', + 0, + $e + ); + } + + return static::instantiateClass($streamFactory); + } +} diff --git a/inc/mailgun/php-http/discovery/src/UriFactoryDiscovery.php b/inc/mailgun/php-http/discovery/src/UriFactoryDiscovery.php new file mode 100644 index 0000000..1eef1e6 --- /dev/null +++ b/inc/mailgun/php-http/discovery/src/UriFactoryDiscovery.php @@ -0,0 +1,36 @@ + + */ +final class UriFactoryDiscovery extends ClassDiscovery +{ + /** + * Finds a URI Factory. + * + * @return UriFactory + * + * @throws Exception\NotFoundException + */ + public static function find() + { + try { + $uriFactory = static::findOneByType(UriFactory::class); + } catch (DiscoveryFailedException $e) { + throw new NotFoundException( + 'No uri factories found. To use Guzzle, Diactoros or Slim Framework factories install php-http/message and the chosen message implementation.', + 0, + $e + ); + } + + return static::instantiateClass($uriFactory); + } +} -- cgit v1.2.3