120 lines
4.3 KiB
PHP
Executable file
120 lines
4.3 KiB
PHP
Executable file
#!/usr/bin/env php
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Pairity CLI Entry Point
|
|
*
|
|
* This script bootstraps the Pairity ORM CLI environment,
|
|
* initializes the service container, and runs the console application.
|
|
*/
|
|
|
|
// Search for the autoloader.
|
|
$autoloadPath = __DIR__ . '/../vendor/autoload.php';
|
|
|
|
if (!file_exists($autoloadPath)) {
|
|
// Translate if possible, otherwise show English
|
|
$msg = "Composer autoloader not found. Please run 'composer install'.";
|
|
fwrite(STDERR, $msg . "\n");
|
|
exit(1);
|
|
}
|
|
|
|
require $autoloadPath;
|
|
|
|
use Pairity\Container\Container;
|
|
use Pairity\Console\Application;
|
|
use Pairity\Contracts\Translation\TranslatorInterface;
|
|
use Pairity\Translation\Translator;
|
|
use Pairity\Contracts\Database\DatabaseManagerInterface;
|
|
use Pairity\Database\DatabaseManager;
|
|
use Pairity\Console\Commands\DatabaseHealthCheckCommand;
|
|
|
|
try {
|
|
// 1. Initialize the Service Container.
|
|
$container = new Container();
|
|
|
|
// 1.0 Register Identity Map (singleton).
|
|
$container->singleton(\Pairity\DTO\IdentityMap::class);
|
|
|
|
// 1.0.1 Register Proxy Factory (singleton).
|
|
$container->singleton(\Pairity\DTO\ProxyFactory::class);
|
|
|
|
// 1.1 Register Translator service (singleton) with path to translations.
|
|
$container->singleton(TranslatorInterface::class, function ($c) {
|
|
return new Translator(__DIR__ . '/../src/Translations');
|
|
});
|
|
|
|
// 1.2 Register Database Manager (singleton).
|
|
$container->singleton(DatabaseManagerInterface::class, function ($c) {
|
|
// In a real app, this config would come from a file.
|
|
$config = [
|
|
'default' => 'sqlite',
|
|
'connections' => [
|
|
'sqlite' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
],
|
|
],
|
|
];
|
|
return new DatabaseManager($config);
|
|
});
|
|
|
|
// 1.3 Register Cache and Metadata services.
|
|
$container->singleton(\Psr\SimpleCache\CacheInterface::class, function ($c) {
|
|
return new \Pairity\Cache\FileCache(__DIR__ . '/../storage/cache');
|
|
});
|
|
|
|
$container->singleton(\Pairity\Schema\MetadataManager::class, function ($c) {
|
|
return new \Pairity\Schema\MetadataManager(
|
|
$c->make(\Pairity\Schema\YamlSchemaParser::class),
|
|
$c->make(\Psr\SimpleCache\CacheInterface::class)
|
|
);
|
|
});
|
|
|
|
$container->singleton(\Pairity\Schema\CodeGenerator::class, function ($c) {
|
|
return new \Pairity\Schema\CodeGenerator(__DIR__ . '/../src/Stubs');
|
|
});
|
|
|
|
// 2. Initialize the Console Application.
|
|
$app = new Application($container);
|
|
|
|
// 3. Register Core Commands.
|
|
$app->add(\Pairity\Console\Commands\InitCommand::class);
|
|
$app->add(DatabaseHealthCheckCommand::class);
|
|
$app->add(\Pairity\Console\Commands\SyncCheckCommand::class);
|
|
$app->add(\Pairity\Console\Commands\GenerateJsonSchemaCommand::class);
|
|
$app->add(\Pairity\Console\Commands\SchemaLintCommand::class);
|
|
$app->add(\Pairity\Console\Commands\GenerateSchemaSnapshotCommand::class);
|
|
$app->add(\Pairity\Console\Commands\GenerateModelsCommand::class);
|
|
$app->add(\Pairity\Console\Commands\SeedCommand::class);
|
|
$app->add(\Pairity\Console\Commands\MakeSeederCommand::class);
|
|
$app->add(\Pairity\Console\Commands\MakeFactoryCommand::class);
|
|
$app->add(\Pairity\Console\Commands\MakeMigrationCommand::class);
|
|
$app->add(\Pairity\Console\Commands\RunDataMigrationCommand::class);
|
|
$app->add(\Pairity\Console\Commands\IntrospectCommand::class);
|
|
$app->add(\Pairity\Console\Commands\CacheClearCommand::class);
|
|
// $app->add(\Pairity\Console\Commands\MigrateCommand::class);
|
|
|
|
// 4. Run the Application.
|
|
$exitCode = $app->run($_SERVER['argv']);
|
|
|
|
exit($exitCode);
|
|
|
|
} catch (\Throwable $e) {
|
|
// Try to use translator if available
|
|
$message = 'Uncaught Exception: ' . $e->getMessage();
|
|
try {
|
|
if (isset($container) && $container->has(TranslatorInterface::class)) {
|
|
/** @var TranslatorInterface $t */
|
|
$t = $container->get(TranslatorInterface::class);
|
|
$message = $t->trans('error.uncaught_exception', ['message' => $e->getMessage()]);
|
|
}
|
|
} catch (\Throwable $ignored) {
|
|
// ignore translation errors in emergency handler
|
|
}
|
|
fwrite(STDERR, $message . "\n");
|
|
fwrite(STDERR, $e->getTraceAsString() . "\n");
|
|
exit(1);
|
|
}
|