|
| 1 | +<?php |
| 2 | + |
| 3 | +use Dotenv\Dotenv; |
| 4 | +use Illuminate\Container\Container; |
| 5 | +use Illuminate\Database\Capsule\Manager; |
| 6 | +use Illuminate\Database\Migrations\DatabaseMigrationRepository; |
| 7 | +use Illuminate\Database\Migrations\Migrator; |
| 8 | +use Illuminate\Events\Dispatcher; |
| 9 | +use Illuminate\Filesystem\Filesystem; |
| 10 | +use Illuminate\Support\Facades\Facade; |
| 11 | +use Minicli\App; |
| 12 | +use Minicli\Command\CommandCall; |
| 13 | +use PHPExperts\ConsolePainter\ConsolePainter; |
| 14 | +use PHPExperts\MiniApiBase\ApiKeyManager; |
| 15 | + |
| 16 | +/** @var Manager $capsule */ |
| 17 | +require __DIR__ . '/../database/config.php'; |
| 18 | + |
| 19 | +$container = new Container; |
| 20 | + |
| 21 | +// Set up the event dispatcher |
| 22 | +$container->singleton('events', function() { |
| 23 | + return new Dispatcher($this); |
| 24 | +}); |
| 25 | + |
| 26 | + |
| 27 | +$container->instance('db', $capsule->getDatabaseManager()); |
| 28 | +//$container->instance('db.schema', $capsule->getDatabaseManager()->getSchemaGrammar()); |
| 29 | + |
| 30 | +// Bind 'db.schema' so Schema facade can resolve the schema builder |
| 31 | +$container->singleton('db.schema', function ($app) { |
| 32 | + return $app->make('db')->connection()->getSchemaBuilder(); |
| 33 | +}); |
| 34 | + |
| 35 | +// Set the container instance to Facade |
| 36 | +Facade::setFacadeApplication($container); |
| 37 | + |
| 38 | + |
| 39 | +$app = new App([ |
| 40 | + 'app_path' => [ |
| 41 | + __DIR__ . '/src/Command', |
| 42 | + ], |
| 43 | + 'theme' => '\Dracula', |
| 44 | + 'debug' => false, |
| 45 | +]); |
| 46 | + |
| 47 | +$p = new ConsolePainter(); |
| 48 | + |
| 49 | +// Load environment variables from the .env file |
| 50 | +$ensureDotEnv = function () { |
| 51 | + if (!file_exists(__DIR__ . '/../.env')) { |
| 52 | + if (file_exists('.env.dist')) { |
| 53 | + system('cp .env.dist .env'); |
| 54 | + } |
| 55 | + |
| 56 | + file_put_contents('.env', ''); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +$makeAppKey = function () { |
| 61 | + $appKey = env('APP_KEY'); |
| 62 | + if (empty($appKey)) { |
| 63 | + $appKey = hash('sha256', bin2hex(random_bytes(8))); |
| 64 | + file_put_contents('.env', "\nAPP_KEY=$appKey\n", FILE_APPEND); |
| 65 | + putenv("APP_KEY=$appKey"); |
| 66 | + $_ENV['APP_KEY'] = $appKey; |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +$ensureDotEnv(); |
| 71 | + |
| 72 | +$dotenv = Dotenv::createImmutable(__DIR__ . '/../'); |
| 73 | +$dotenv->load(); |
| 74 | + |
| 75 | +$makeAppKey(); |
| 76 | + |
| 77 | +// Validate the environment variables |
| 78 | +$validateEnv = function () use ($p) { |
| 79 | + $missing = false; |
| 80 | + foreach (['APP_KEY'] as $e) { |
| 81 | + if (empty($_ENV[$e])) { |
| 82 | + echo $p->bold()->red("ERROR: ")->lightCyan($e)->text(" is not set in ")->lightPurple('.env')->text(" or environment variable\n"); |
| 83 | + $missing = true; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if ($missing === true) { |
| 88 | + exit(1); |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +$validateEnv(); |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +$app->registerCommand('migrate', function () { |
| 97 | + global $capsule; |
| 98 | + |
| 99 | + $repository = new DatabaseMigrationRepository($capsule->getDatabaseManager(), 'migrations'); |
| 100 | + if (!$repository->repositoryExists()) { |
| 101 | + $repository->createRepository(); |
| 102 | + } |
| 103 | + |
| 104 | + $migrator = new Migrator($repository, $capsule->getDatabaseManager(), new Filesystem()); |
| 105 | + $migrationKeys = $migrator->run(__DIR__ . '/../database/migrations'); |
| 106 | + |
| 107 | + foreach ($migrationKeys as $filepath) { |
| 108 | + $migrationKey = basename($filepath, '.php'); |
| 109 | + dump("Successfully ran the migration $migrationKey."); |
| 110 | + } |
| 111 | + |
| 112 | +}, '<option>', 'Runs database migrations.'); |
| 113 | + |
| 114 | +$app->registerCommand('migrate:rollback', function () { |
| 115 | + global $capsule; |
| 116 | + |
| 117 | + $repository = new DatabaseMigrationRepository($capsule->getDatabaseManager(), 'migrations'); |
| 118 | + if (!$repository->repositoryExists()) { |
| 119 | + $repository->createRepository(); |
| 120 | + } |
| 121 | + |
| 122 | + $migrator = new Migrator($repository, $capsule->getDatabaseManager(), new Filesystem()); |
| 123 | + $migrationKeys = $migrator->rollback(__DIR__ . '/../database/migrations'); |
| 124 | + |
| 125 | + foreach ($migrationKeys as $filepath) { |
| 126 | + $migrationKey = basename($filepath, '.php'); |
| 127 | + dump("Successfully rolled back the migration $migrationKey."); |
| 128 | + } |
| 129 | + |
| 130 | +}, '', 'Rolls back one migration at a time.'); |
| 131 | + |
| 132 | + |
| 133 | +$app->registerCommand('api_key:issue', function (CommandCall $cli) use ($p) { |
| 134 | + $argv = $cli->getRawArgs(); |
| 135 | + if (count($argv) < 3) { |
| 136 | + echo $p->bold()->red("ERROR: ")->text('Missing the "client" CLI argument.') . "\n"; |
| 137 | + exit(1); |
| 138 | + } |
| 139 | + |
| 140 | + $client = $argv[2]; |
| 141 | + |
| 142 | + $keyMaster = new ApiKeyManager(); |
| 143 | + $apiKey = $keyMaster->createApiKey($client); |
| 144 | + |
| 145 | + echo $p->bold(" Here is your API Key:") . "\n"; |
| 146 | + echo "\n"; |
| 147 | + echo $p->bold()->yellow(" $apiKey\n"); |
| 148 | + echo "\n"; |
| 149 | + echo $p->bold()->red(" Store it now, because it will never be reshown.") . "\n"; |
| 150 | + |
| 151 | +}, '[client]', 'Issues a new API key for a client.'); |
0 commit comments