Files
MeshVault/config/container.php
T

105 lines
5.3 KiB
PHP

<?php
declare(strict_types=1);
use MeshVault\Catalog\CatalogService;
use MeshVault\Catalog\CatalogServiceInterface;
use MeshVault\Indexer\LegacyGuessedImportGroupingAdapter;
use MeshVault\Config\AppConfig;
use MeshVault\Console\ConsoleServices;
use MeshVault\Console\ContainerConsoleServices;
use MeshVault\Console\ImportWorkerFactory;
use MeshVault\Http\View;
use MeshVault\Indexer\Contracts\FileEnumeratorInterface;
use MeshVault\Indexer\FileEnumerator;
use MeshVault\Indexer\Filesystem\LocalFilesystem;
use MeshVault\Indexer\Filesystem\PathSafety;
use MeshVault\Indexer\ImportService;
use MeshVault\Indexer\ImportWorker;
use MeshVault\Indexer\SpaceChecker;
use MeshVault\Indexer\StlValidator;
use MeshVault\Indexer\StreamingHasher;
use MeshVault\Indexer\VerifiedCopier;
use MeshVault\Logging\LoggerFactory;
use MeshVault\Repository\CatalogRepositoryInterface;
use MeshVault\Repository\ImportJobRepositoryInterface;
use MeshVault\Repository\PdoCatalogRepository;
use MeshVault\Repository\PdoImportJobRepository;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
return static fn (AppConfig $config): array => [
AppConfig::class => $config,
View::class => new View(dirname(__DIR__) . '/templates'),
LoggerInterface::class => static fn (): LoggerInterface => LoggerFactory::create($config),
PDO::class => static fn (): PDO => new PDO(
sprintf('pgsql:host=%s;port=%d;dbname=%s', $config->dbHost, $config->dbPort, $config->dbName),
$config->dbUser,
$config->dbPassword,
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false],
),
PathSafety::class => static fn (): PathSafety => new PathSafety($config->libraryPath),
FileEnumeratorInterface::class => static fn (ContainerInterface $c): FileEnumeratorInterface
=> new FileEnumerator(new LocalFilesystem(), $c->get(PathSafety::class)),
SpaceChecker::class => static fn (): SpaceChecker => new SpaceChecker(),
StreamingHasher::class => static fn (ContainerInterface $c): StreamingHasher
=> new StreamingHasher($c->get(PathSafety::class)),
StlValidator::class => static fn (ContainerInterface $c): StlValidator
=> new StlValidator($c->get(PathSafety::class)),
VerifiedCopier::class => static fn (ContainerInterface $c): VerifiedCopier
=> new VerifiedCopier($c->get(PathSafety::class)),
// Repositories share one PDO connection so the job repository's atomic() is a real transaction.
CatalogRepositoryInterface::class => static fn (ContainerInterface $c): CatalogRepositoryInterface
=> new PdoCatalogRepository($c->get(PDO::class)),
CatalogService::class => static fn (ContainerInterface $c): CatalogService
=> new CatalogService($c->get(CatalogRepositoryInterface::class)),
ImportJobRepositoryInterface::class => static fn (ContainerInterface $c): ImportJobRepositoryInterface
=> new PdoImportJobRepository($c->get(PDO::class)),
CatalogServiceInterface::class => DI\get(CatalogService::class),
LegacyGuessedImportGroupingAdapter::class => static fn (ContainerInterface $c): LegacyGuessedImportGroupingAdapter
=> new LegacyGuessedImportGroupingAdapter($c->get(CatalogRepositoryInterface::class)),
// Explicit grouping is opt-in; the default ImportService below remains legacy-compatible.
\MeshVault\Indexer\ExplicitImportGroupingAdapter::class => static fn (ContainerInterface $c)
=> new \MeshVault\Indexer\ExplicitImportGroupingAdapter(
$c->get(CatalogRepositoryInterface::class),
$c->get(ImportJobRepositoryInterface::class),
),
\MeshVault\Indexer\Contracts\ExplicitImportGroupingAdapterInterface::class
=> DI\get(\MeshVault\Indexer\ExplicitImportGroupingAdapter::class),
ImportService::class => static fn (ContainerInterface $c): ImportService => new ImportService(
$c->get(FileEnumeratorInterface::class),
$c->get(ImportJobRepositoryInterface::class),
$c->get(LegacyGuessedImportGroupingAdapter::class),
$c->get(SpaceChecker::class),
$config->libraryPath,
),
ImportWorkerFactory::class => static fn (ContainerInterface $c): ImportWorkerFactory
=> new class ($c) implements ImportWorkerFactory {
public function __construct(private readonly ContainerInterface $c)
{
}
public function __invoke(callable $shouldStop): ImportWorker
{
/** @var PathSafety $safety */
$safety = $this->c->get(PathSafety::class);
return new ImportWorker(
$this->c->get(ImportJobRepositoryInterface::class),
$this->c->get(CatalogServiceInterface::class),
$this->c->get(StreamingHasher::class),
$this->c->get(VerifiedCopier::class),
$this->c->get(StlValidator::class),
$safety,
$this->c->get(SpaceChecker::class),
$safety->libraryRoot(),
$this->c->get(LoggerInterface::class),
$shouldStop,
);
}
},
ConsoleServices::class => static fn (ContainerInterface $c): ConsoleServices => new ContainerConsoleServices(
$c->get(ImportService::class),
$c->get(ImportWorkerFactory::class),
),
];