93 lines
3.2 KiB
PHP
93 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enum\SystemSettingKey;
|
|
use Doctrine\ORM\Configuration;
|
|
use App\Settings;
|
|
use Doctrine\DBAL\Connection;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\DBAL\DriverManager;
|
|
use Doctrine\ORM\EntityManager;
|
|
use Doctrine\ORM\ORMSetup;
|
|
use Slim\Views\Twig;
|
|
use Monolog\Logger;
|
|
use Monolog\Handler\StreamHandler;
|
|
use Monolog\Processor\UidProcessor;
|
|
use App\Repository\DrinkTypeRepository;
|
|
use App\Repository\InventoryRecordRepository;
|
|
use App\Repository\OrderRepository;
|
|
use App\Repository\OrderItemRepository;
|
|
use App\Repository\SystemConfigRepository;
|
|
|
|
return [
|
|
Configuration::class => fn(Settings $s): Configuration => ORMSetup::createAttributeMetadataConfiguration(
|
|
paths: [__DIR__ . '/../src/Entity'],
|
|
isDevMode: $s->isTestMode,
|
|
proxyDir: __DIR__ . '/../var/cache/doctrine/proxy',
|
|
),
|
|
Connection::class => fn(
|
|
Configuration $configuration,
|
|
Settings $s,
|
|
): Connection => DriverManager::getConnection(
|
|
params: $s->isTestMode ? [
|
|
'driver' => 'pdo_sqlite',
|
|
'memory' => true,
|
|
] : [
|
|
'driver' => 'pdo_sqlite',
|
|
'path' => __DIR__ . '/../var/database.sqlite',
|
|
],
|
|
config: $configuration
|
|
),
|
|
EntityManagerInterface::class => fn(
|
|
Connection $connection,
|
|
Configuration $configuration
|
|
): EntityManagerInterface => new EntityManager($connection, $configuration),
|
|
|
|
Twig::class => function (\App\Service\ConfigurationService $config): Twig {
|
|
$paths = [__DIR__ . '/../templates'];
|
|
$cache = __DIR__ . '/../var/cache/twig';
|
|
|
|
// Ensure cache directory exists
|
|
if (!is_dir($cache)) {
|
|
mkdir($cache, 0o755, true);
|
|
}
|
|
|
|
$twig = Twig::create($paths, [
|
|
'cache' => $cache,
|
|
'auto_reload' => true,
|
|
]);
|
|
$twig['appName'] = $config->getConfigByKey(SystemSettingKey::SYSTEM_NAME->value)->getValue();
|
|
return $twig;
|
|
},
|
|
|
|
// Logger
|
|
Logger::class => function (): Logger {
|
|
$logDir = __DIR__ . '/../var/logs';
|
|
|
|
// Ensure log directory exists
|
|
if (!is_dir($logDir)) {
|
|
mkdir($logDir, 0o755, true);
|
|
}
|
|
|
|
$logger = new Logger('app');
|
|
|
|
$processor = new UidProcessor();
|
|
$logger->pushProcessor($processor);
|
|
|
|
$handler = new StreamHandler(
|
|
$logDir . '/app.log',
|
|
Logger::DEBUG
|
|
);
|
|
$logger->pushHandler($handler);
|
|
|
|
return $logger;
|
|
},
|
|
|
|
// Repositories
|
|
DrinkTypeRepository::class => fn(EntityManagerInterface $entityManager): DrinkTypeRepository => new DrinkTypeRepository($entityManager),
|
|
InventoryRecordRepository::class => fn(EntityManagerInterface $entityManager): InventoryRecordRepository => new InventoryRecordRepository($entityManager),
|
|
OrderRepository::class => fn(EntityManagerInterface $entityManager): OrderRepository => new OrderRepository($entityManager),
|
|
OrderItemRepository::class => fn(EntityManagerInterface $entityManager): OrderItemRepository => new OrderItemRepository($entityManager),
|
|
SystemConfigRepository::class => fn(EntityManagerInterface $entityManager): SystemConfigRepository => new SystemConfigRepository($entityManager),
|
|
];
|