|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OpenAI\Laravel\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use OpenAI\Laravel\ServiceProvider; |
| 7 | +use OpenAI\Laravel\Support\View; |
| 8 | + |
| 9 | +class InstallCommand extends Command |
| 10 | +{ |
| 11 | + private const LINKS = [ |
| 12 | + 'Repository' => 'https://github.com/openai-php/laravel', |
| 13 | + 'OpenAI PHP Docs' => 'https://github.com/openai-php/client#readme', |
| 14 | + 'Join us on Telegram' => 'https://t.me/+66GDs6UM6RcxY2U8', |
| 15 | + ]; |
| 16 | + |
| 17 | + private const FUNDING_LINKS = [ |
| 18 | + 'Sponsor Sandro' => 'https://github.com/sponsors/gehrisandro', |
| 19 | + 'Sponsor Nuno' => 'https://github.com/sponsors/nunomaduro', |
| 20 | + ]; |
| 21 | + |
| 22 | + protected $signature = 'openai:install'; |
| 23 | + |
| 24 | + protected $description = 'Prepares the OpenAI client for use.'; |
| 25 | + |
| 26 | + public function handle(): void |
| 27 | + { |
| 28 | + View::renderUsing($this->output); |
| 29 | + |
| 30 | + View::render('components.badge', [ |
| 31 | + 'type' => 'INFO', |
| 32 | + 'content' => 'Installing OpenAI for Laravel.', |
| 33 | + ]); |
| 34 | + |
| 35 | + $this->copyConfig(); |
| 36 | + |
| 37 | + View::render('components.new-line'); |
| 38 | + |
| 39 | + $this->addEnvKeys('.env'); |
| 40 | + $this->addEnvKeys('.env.example'); |
| 41 | + |
| 42 | + View::render('components.new-line'); |
| 43 | + |
| 44 | + $wantsToSupport = $this->askToStarRepository(); |
| 45 | + |
| 46 | + $this->showLinks(); |
| 47 | + |
| 48 | + View::render('components.badge', [ |
| 49 | + 'type' => 'INFO', |
| 50 | + 'content' => 'Open your .env and add your OpenAI API key and organization id.', |
| 51 | + ]); |
| 52 | + |
| 53 | + if ($wantsToSupport) { |
| 54 | + $this->openRepositoryInBrowser(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private function copyConfig(): void |
| 59 | + { |
| 60 | + if (file_exists(config_path('openai.php'))) { |
| 61 | + View::render('components.two-column-detail', [ |
| 62 | + 'left' => 'config/openai.php', |
| 63 | + 'right' => 'File already exists.', |
| 64 | + ]); |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + View::render('components.two-column-detail', [ |
| 70 | + 'left' => 'config/openai.php', |
| 71 | + 'right' => 'File created.', |
| 72 | + ]); |
| 73 | + |
| 74 | + $this->callSilent('vendor:publish', [ |
| 75 | + '--provider' => ServiceProvider::class, |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + private function addEnvKeys(string $envFile): void |
| 80 | + { |
| 81 | + $fileContent = file_get_contents(base_path($envFile)); |
| 82 | + |
| 83 | + if ($fileContent === false) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (str_contains($fileContent, 'OPENAI_API_KEY')) { |
| 88 | + View::render('components.two-column-detail', [ |
| 89 | + 'left' => $envFile, |
| 90 | + 'right' => 'Variables already exists.', |
| 91 | + ]); |
| 92 | + |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + file_put_contents(base_path($envFile), PHP_EOL.'OPENAI_API_KEY='.PHP_EOL.'OPENAI_ORGANIZATION='.PHP_EOL, FILE_APPEND); |
| 97 | + |
| 98 | + View::render('components.two-column-detail', [ |
| 99 | + 'left' => $envFile, |
| 100 | + 'right' => 'OPENAI_API_KEY and OPENAI_ORGANIZATION variables added.', |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + private function askToStarRepository(): bool |
| 105 | + { |
| 106 | + if (! $this->input->isInteractive()) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + return $this->confirm(' <options=bold>Wanna show OpenAI for Laravel some love by starring it on GitHub?</>', false); |
| 111 | + } |
| 112 | + |
| 113 | + private function openRepositoryInBrowser(): void |
| 114 | + { |
| 115 | + if (PHP_OS_FAMILY == 'Darwin') { |
| 116 | + exec('open https://github.com/openai-php/laravel'); |
| 117 | + } |
| 118 | + if (PHP_OS_FAMILY == 'Windows') { |
| 119 | + exec('start https://github.com/openai-php/laravel'); |
| 120 | + } |
| 121 | + if (PHP_OS_FAMILY == 'Linux') { |
| 122 | + exec('xdg-open https://github.com/openai-php/laravel'); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private function showLinks(): void |
| 127 | + { |
| 128 | + $links = [ |
| 129 | + ...self::LINKS, |
| 130 | + ...rand(0, 1) ? self::FUNDING_LINKS : array_reverse(self::FUNDING_LINKS, true), |
| 131 | + ]; |
| 132 | + |
| 133 | + foreach ($links as $message => $link) { |
| 134 | + View::render('components.two-column-detail', [ |
| 135 | + 'left' => $message, |
| 136 | + 'right' => $link, |
| 137 | + ]); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments