Skip to content

Commit

Permalink
feat!: Removed Guzzle dependencies in favor of Symfony Http Client
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Dec 2, 2024
1 parent 8b6564c commit bfc4634
Show file tree
Hide file tree
Showing 33 changed files with 290 additions and 290 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"symfony/form": "6.4.*",
"symfony/framework-bundle": "6.4.*",
"symfony/http-client": "6.4.*",
"symfony/http-client-contracts": "^3.5",
"symfony/intl": "6.4.*",
"symfony/lock": "6.4.*",
"symfony/mailer": "6.4.*",
Expand Down
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ services:
#
RZ\Roadiz\CoreBundle\Document\MediaFinder\UnsplashPictureFinder:
arguments:
- '@http_client'
- '%roadiz_core.medias.unsplash_client_id%'
RZ\Roadiz\Documents\MediaFinders\RandomImageFinder:
alias: RZ\Roadiz\CoreBundle\Document\MediaFinder\UnsplashPictureFinder
Expand Down Expand Up @@ -641,6 +642,7 @@ services:

RZ\Roadiz\Documents\MediaFinders\EmbedFinderFactory:
arguments:
- '@http_client'
- '%roadiz_core.medias.supported_platforms%'

RZ\Roadiz\Documents\Renderer\ChainRenderer:
Expand Down
12 changes: 6 additions & 6 deletions src/DependencyInjection/RoadizCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\Repository\NodesSourcesRepository;
use RZ\Roadiz\CoreBundle\Webhook\Message\GenericJsonPostMessage;
use RZ\Roadiz\CoreBundle\Webhook\Message\GitlabPipelineTriggerMessage;
use RZ\Roadiz\CoreBundle\Webhook\Message\NetlifyBuildHookMessage;
use RZ\Roadiz\CoreBundle\Webhook\Message\GenericJsonPostMessageInterface;
use RZ\Roadiz\CoreBundle\Webhook\Message\GitlabPipelineTriggerMessageInterface;
use RZ\Roadiz\CoreBundle\Webhook\Message\NetlifyBuildHookMessageInterface;
use RZ\Roadiz\Markdown\CommonMark;
use RZ\Roadiz\Markdown\MarkdownInterface;
use Solarium\Core\Client\Adapter\Curl;
Expand Down Expand Up @@ -113,9 +113,9 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('roadiz_core.medias.supported_platforms', []);

$container->setParameter('roadiz_core.webhook.message_types', [
'webhook.type.generic_json_post' => GenericJsonPostMessage::class,
'webhook.type.gitlab_pipeline' => GitlabPipelineTriggerMessage::class,
'webhook.type.netlify_build_hook' => NetlifyBuildHookMessage::class,
'webhook.type.generic_json_post' => GenericJsonPostMessageInterface::class,
'webhook.type.gitlab_pipeline' => GitlabPipelineTriggerMessageInterface::class,
'webhook.type.netlify_build_hook' => NetlifyBuildHookMessageInterface::class,
]);

$this->registerEntityGenerator($config, $container);
Expand Down
3 changes: 0 additions & 3 deletions src/Doctrine/EventSubscriber/UserLifeCycleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public function __construct(
) {
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function preUpdate(PreUpdateEventArgs $event): void
{
$user = $event->getObject();
Expand Down
6 changes: 3 additions & 3 deletions src/Document/MediaFinder/EmbedFinderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace RZ\Roadiz\CoreBundle\Document\MediaFinder;

use Doctrine\Persistence\ObjectManager;
use GuzzleHttp\Exception\ClientException;
use RZ\Roadiz\CoreBundle\Entity\Document;
use RZ\Roadiz\CoreBundle\Entity\DocumentTranslation;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\Documents\Exceptions\APINeedsAuthentificationException;
use RZ\Roadiz\Documents\Models\DocumentInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;

trait EmbedFinderTrait
{
Expand Down Expand Up @@ -50,8 +50,8 @@ protected function injectMetaInDocument(ObjectManager $objectManager, DocumentIn
}
}
} catch (APINeedsAuthentificationException $exception) {
// do no prevent from creating document if credentials are not provided.
} catch (ClientException $exception) {
// do not prevent from creating document if credentials are not provided.
} catch (ClientExceptionInterface $exception) {
// do not prevent from creating document if platform has errors, such as
// too much API usage.
}
Expand Down
6 changes: 3 additions & 3 deletions src/Document/MediaFinder/PodcastFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace RZ\Roadiz\CoreBundle\Document\MediaFinder;

use Doctrine\Persistence\ObjectManager;
use GuzzleHttp\Exception\ClientException;
use RZ\Roadiz\CoreBundle\Entity\DocumentTranslation;
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\Documents\MediaFinders\AbstractPodcastFinder;
use RZ\Roadiz\Documents\Models\DocumentInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;

class PodcastFinder extends AbstractPodcastFinder
{
Expand All @@ -33,8 +33,8 @@ protected function injectMetaFromPodcastItem(
$documentTr->setCopyright($this->getPodcastItemCopyright($item));
$objectManager->persist($documentTr);
}
} catch (ClientException $exception) {
// do no prevent from creating document if platform has errors, such as
} catch (ClientExceptionInterface $exception) {
// do not prevent from creating document if platform has errors, such as
// too much API usage.
}
}
Expand Down
72 changes: 31 additions & 41 deletions src/EventSubscriber/CloudflareCacheEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@

namespace RZ\Roadiz\CoreBundle\EventSubscriber;

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
use RZ\Roadiz\CoreBundle\Cache\CloudflareProxyCache;
use RZ\Roadiz\CoreBundle\Cache\ReverseProxyCacheLocator;
use RZ\Roadiz\CoreBundle\Event\Cache\CachePurgeRequestEvent;
use RZ\Roadiz\CoreBundle\Event\NodesSources\NodesSourcesUpdatedEvent;
use RZ\Roadiz\CoreBundle\Message\GuzzleRequestMessage;
use RZ\Roadiz\CoreBundle\Message\HttpRequestMessage;
use RZ\Roadiz\CoreBundle\Message\HttpRequestMessageInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\Exception\ExceptionInterface as MessengerExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;

final class CloudflareCacheEventSubscriber implements EventSubscriberInterface
final readonly class CloudflareCacheEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly MessageBusInterface $bus,
private readonly ReverseProxyCacheLocator $reverseProxyCacheLocator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly LoggerInterface $logger,
private MessageBusInterface $bus,
private ReverseProxyCacheLocator $reverseProxyCacheLocator,
private UrlGeneratorInterface $urlGenerator,
private LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -58,22 +56,14 @@ public function onBanRequest(CachePurgeRequestEvent $event): void
self::class,
'Cloudflare proxy cache'
);
} catch (RequestException $e) {
if (null !== $e->getResponse()) {
$data = \json_decode($e->getResponse()->getBody()->getContents(), true);
$event->addError(
$data['errors'][0]['message'] ?? $e->getMessage(),
self::class,
'Cloudflare proxy cache'
);
} else {
$event->addError(
$e->getMessage(),
self::class,
'Cloudflare proxy cache'
);
}
} catch (ConnectException $e) {
} catch (HttpExceptionInterface $e) {
$data = \json_decode($e->getResponse()->getContent(false), true);
$event->addError(
$data['errors'][0]['message'] ?? $e->getMessage(),
self::class,
'Cloudflare proxy cache'
);
} catch (ExceptionInterface $e) {
$event->addError(
$e->getMessage(),
self::class,
Expand Down Expand Up @@ -105,7 +95,7 @@ public function onPurgeRequest(NodesSourcesUpdatedEvent $event): void
UrlGeneratorInterface::ABSOLUTE_URL
)]);
$this->sendRequest($purgeRequest);
} catch (ClientException $e) {
} catch (ExceptionInterface $e) {
// do nothing
}
}
Expand All @@ -123,7 +113,7 @@ private function getCloudflareCacheProxy(): CloudflareProxyCache
/**
* @throws \JsonException
*/
protected function createRequest(array $body): Request
protected function createRequest(array $body): HttpRequestMessageInterface
{
$headers = [
'Content-type' => 'application/json',
Expand All @@ -139,18 +129,21 @@ protected function createRequest(array $body): Request
);
$body = \json_encode($body, JSON_THROW_ON_ERROR);

return new Request(
return new HttpRequestMessage(
'POST',
$uri,
$headers,
$body
[
'timeout' => $this->getCloudflareCacheProxy()->getTimeout(),
'headers' => $headers,
'body' => $body,
],
);
}

/**
* @throws \JsonException
*/
protected function createBanRequest(): Request
protected function createBanRequest(): HttpRequestMessageInterface
{
return $this->createRequest([
'purge_everything' => true,
Expand All @@ -162,21 +155,18 @@ protected function createBanRequest(): Request
*
* @throws \JsonException
*/
protected function createPurgeRequest(array $uris = []): Request
protected function createPurgeRequest(array $uris = []): HttpRequestMessageInterface
{
return $this->createRequest([
'files' => $uris,
]);
}

protected function sendRequest(RequestInterface $request): void
protected function sendRequest(HttpRequestMessageInterface $requestMessage): void
{
try {
$this->bus->dispatch(new Envelope(new GuzzleRequestMessage($request, [
'debug' => false,
'timeout' => $this->getCloudflareCacheProxy()->getTimeout(),
])));
} catch (ExceptionInterface $exception) {
$this->bus->dispatch(new Envelope($requestMessage));
} catch (MessengerExceptionInterface $exception) {
$this->logger->error($exception->getMessage());
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/EventSubscriber/ReverseProxyCacheEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

namespace RZ\Roadiz\CoreBundle\EventSubscriber;

use GuzzleHttp\Psr7\Request;
use Psr\Log\LoggerInterface;
use RZ\Roadiz\CoreBundle\Cache\ReverseProxyCacheLocator;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Event\Cache\CachePurgeRequestEvent;
use RZ\Roadiz\CoreBundle\Event\NodesSources\NodesSourcesUpdatedEvent;
use RZ\Roadiz\CoreBundle\Message\GuzzleRequestMessage;
use RZ\Roadiz\CoreBundle\Message\HttpRequestMessage;
use RZ\Roadiz\CoreBundle\Message\HttpRequestMessageInterface;
use RZ\Roadiz\CoreBundle\Message\PurgeReverseProxyCacheMessage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\Event\Event;

final class ReverseProxyCacheEventSubscriber implements EventSubscriberInterface
final readonly class ReverseProxyCacheEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly ReverseProxyCacheLocator $reverseProxyCacheLocator,
private readonly MessageBusInterface $bus,
private readonly LoggerInterface $logger,
private ReverseProxyCacheLocator $reverseProxyCacheLocator,
private MessageBusInterface $bus,
private LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public function onPurgeRequest(NodesSourcesUpdatedEvent $event): void
}

/**
* @return Request[]
* @return HttpRequestMessageInterface[]
*/
protected function createBanRequests(): array
{
Expand All @@ -94,11 +94,14 @@ protected function createBanRequests(): array
} else {
$uri = $frontend->getHost();
}
$requests[$frontend->getName()] = new Request(
$requests[$frontend->getName()] = new HttpRequestMessage(
'BAN',
$uri,
[
'Host' => $frontend->getDomainName(),
'timeout' => 3,
'headers' => [
'Host' => $frontend->getDomainName(),
],
]
);
}
Expand All @@ -115,13 +118,10 @@ protected function purgeNodesSources(NodesSources $nodeSource): void
}
}

protected function sendRequest(Request $request): void
protected function sendRequest(HttpRequestMessageInterface $requestMessage): void
{
try {
$this->bus->dispatch(new Envelope(new GuzzleRequestMessage($request, [
'debug' => false,
'timeout' => 3,
])));
$this->bus->dispatch(new Envelope($requestMessage));
} catch (ExceptionInterface $exception) {
$this->logger->error($exception->getMessage());
}
Expand Down
Loading

0 comments on commit bfc4634

Please sign in to comment.