Use GuzzleFactory and new Doctrine creator.

This commit is contained in:
Buster Neece 2022-10-21 22:46:39 -05:00
parent 065e8283eb
commit 3bd91a566c
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
3 changed files with 68 additions and 40 deletions

View File

@ -28,7 +28,7 @@ return [
Slim\Interfaces\ErrorHandlerInterface::class => DI\Get(App\Http\ErrorHandler::class), Slim\Interfaces\ErrorHandlerInterface::class => DI\Get(App\Http\ErrorHandler::class),
// HTTP client // HTTP client
GuzzleHttp\HandlerStack::class => static function (Psr\Log\LoggerInterface $logger) { App\Service\GuzzleFactory::class => static function (Psr\Log\LoggerInterface $logger) {
$stack = GuzzleHttp\HandlerStack::create(); $stack = GuzzleHttp\HandlerStack::create();
$stack->unshift( $stack->unshift(
@ -50,29 +50,35 @@ return [
) )
); );
return $stack; return new App\Service\GuzzleFactory(
[
'handler' => $stack,
GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
GuzzleHttp\RequestOptions::TIMEOUT => 3.0,
]
);
}, },
GuzzleHttp\Client::class => static fn(GuzzleHttp\HandlerStack $stack) => new GuzzleHttp\Client( GuzzleHttp\Client::class => static fn(App\Service\GuzzleFactory $guzzleFactory) => $guzzleFactory->buildClient(),
[
'handler' => $stack,
GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
GuzzleHttp\RequestOptions::TIMEOUT => 3.0,
]
),
// DBAL // DBAL
Doctrine\DBAL\Connection::class => static fn(Doctrine\ORM\EntityManagerInterface $em) => $em->getConnection(), Doctrine\DBAL\Connection::class => static fn(Doctrine\ORM\EntityManagerInterface $em) => $em->getConnection(),
// Doctrine Entity Manager // Doctrine Entity Manager
App\Doctrine\DecoratedEntityManager::class => static function ( App\Doctrine\DecoratedEntityManager::class => static function (
Doctrine\Common\Cache\Cache $doctrineCache, Psr\Cache\CacheItemPoolInterface $psr6Cache,
Environment $environment, Environment $environment,
App\Doctrine\Event\StationRequiresRestart $eventRequiresRestart, App\Doctrine\Event\StationRequiresRestart $eventRequiresRestart,
App\Doctrine\Event\AuditLog $eventAuditLog, App\Doctrine\Event\AuditLog $eventAuditLog,
App\Doctrine\Event\SetExplicitChangeTracking $eventChangeTracking, App\Doctrine\Event\SetExplicitChangeTracking $eventChangeTracking,
Psr\EventDispatcher\EventDispatcherInterface $dispatcher Psr\EventDispatcher\EventDispatcherInterface $dispatcher
) { ) {
if ($environment->isCli()) {
$psr6Cache = new Symfony\Component\Cache\Adapter\ArrayAdapter();
} else {
$psr6Cache = new Symfony\Component\Cache\Adapter\ProxyAdapter($psr6Cache, 'doctrine.');
}
$dbSettings = $environment->getDatabaseSettings(); $dbSettings = $environment->getDatabaseSettings();
if (isset($dbSettings['unix_socket'])) { if (isset($dbSettings['unix_socket'])) {
unset($dbSettings['host'], $dbSettings['port']); unset($dbSettings['host'], $dbSettings['port']);
@ -96,17 +102,6 @@ return [
); );
try { try {
// Fetch and store entity manager.
$config = Doctrine\ORM\Tools\Setup::createConfiguration(
!$environment->isProduction(),
$environment->getTempDirectory() . '/proxies',
$doctrineCache
);
$config->setAutoGenerateProxyClasses(
Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS
);
$mappingClassesPaths = [$environment->getBaseDirectory() . '/src/Entity']; $mappingClassesPaths = [$environment->getBaseDirectory() . '/src/Entity'];
$buildDoctrineMappingPathsEvent = new Event\BuildDoctrineMappingPaths( $buildDoctrineMappingPathsEvent = new Event\BuildDoctrineMappingPaths(
@ -117,10 +112,17 @@ return [
$mappingClassesPaths = $buildDoctrineMappingPathsEvent->getMappingClassesPaths(); $mappingClassesPaths = $buildDoctrineMappingPathsEvent->getMappingClassesPaths();
$attributeDriver = new Doctrine\ORM\Mapping\Driver\AttributeDriver( // Fetch and store entity manager.
$mappingClassesPaths $config = Doctrine\ORM\ORMSetup::createAttributeMetadataConfiguration(
$mappingClassesPaths,
!$environment->isProduction(),
$environment->getTempDirectory() . '/proxies',
$psr6Cache
);
$config->setAutoGenerateProxyClasses(
Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED
); );
$config->setMetadataDriverImpl($attributeDriver);
// Debug mode: // Debug mode:
// $config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger); // $config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger);
@ -205,19 +207,6 @@ return [
return new Symfony\Component\Cache\Psr16Cache($cache); return new Symfony\Component\Cache\Psr16Cache($cache);
}, },
// Doctrine cache
Doctrine\Common\Cache\Cache::class => static function (
Environment $environment,
Psr\Cache\CacheItemPoolInterface $psr6Cache
) {
if ($environment->isCli()) {
$psr6Cache = new Symfony\Component\Cache\Adapter\ArrayAdapter();
}
$proxyCache = new Symfony\Component\Cache\Adapter\ProxyAdapter($psr6Cache, 'doctrine.');
return Doctrine\Common\Cache\Psr6\DoctrineProvider::wrap($proxyCache);
},
// Symfony Lock adapter // Symfony Lock adapter
Symfony\Component\Lock\PersistingStoreInterface::class => static function ( Symfony\Component\Lock\PersistingStoreInterface::class => static function (
ContainerInterface $di, ContainerInterface $di,

View File

@ -0,0 +1,39 @@
<?php
namespace App\Service;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
final class GuzzleFactory
{
public function __construct(
private readonly array $defaultConfig = []
) {
}
public function withConfig(array $defaultConfig): self
{
return new self($defaultConfig);
}
public function withAddedConfig(array $config): self
{
return new self(array_merge($this->defaultConfig, $config));
}
public function getDefaultConfig(): array
{
return $this->defaultConfig;
}
public function getHandlerStack(): HandlerStack
{
return $this->defaultConfig['handler'] ?? HandlerStack::create();
}
public function buildClient(array $config = []): Client
{
return new Client(array_merge($this->defaultConfig, $config));
}
}

View File

@ -5,9 +5,9 @@ declare(strict_types=1);
namespace App\Webhook\Connector; namespace App\Webhook\Connector;
use App\Entity; use App\Entity;
use App\Service\GuzzleFactory;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1; use GuzzleHttp\Subscriber\Oauth\Oauth1;
use Monolog\Logger; use Monolog\Logger;
@ -18,7 +18,7 @@ final class Twitter extends AbstractConnector
public function __construct( public function __construct(
Logger $logger, Logger $logger,
Client $httpClient, Client $httpClient,
private readonly HandlerStack $handlerStack, private readonly GuzzleFactory $guzzleFactory,
) { ) {
parent::__construct($logger, $httpClient); parent::__construct($logger, $httpClient);
} }
@ -53,7 +53,7 @@ final class Twitter extends AbstractConnector
} }
// Set up Twitter OAuth // Set up Twitter OAuth
$stack = clone $this->handlerStack; $stack = clone $this->guzzleFactory->getHandlerStack();
$middleware = new Oauth1( $middleware = new Oauth1(
[ [