-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8b217d
commit 475f1f6
Showing
7 changed files
with
252 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/** @var string $type */ | ||
/** @var string $content */ | ||
[$bgBadgeColor, $bgBadgeText] = match ($type) { | ||
'INFO' => ['blue', 'INFO'], | ||
'ERROR' => ['red', 'ERROR'], | ||
}; | ||
|
||
?> | ||
|
||
<div class="my-1"> | ||
<span class="ml-2 px-1 bg-<?php echo $bgBadgeColor ?>-600 font-bold"><?php echo htmlspecialchars($bgBadgeText) ?></span> | ||
<span class="ml-1"> | ||
<?php echo htmlspecialchars($content) ?> | ||
</span> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div class="flex mx-2 max-w-150"> | ||
<span> | ||
<?php echo htmlspecialchars($left) ?> | ||
</span> | ||
<span class="flex-1 content-repeat-[.] text-gray ml-1"></span> | ||
<?php if ($right !== '') { ?> | ||
<span class="ml-1 text-gray"> | ||
<?php echo htmlspecialchars($right) ?> | ||
</span> | ||
<?php } ?> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
namespace OpenAI\Laravel\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use OpenAI\Laravel\ServiceProvider; | ||
use OpenAI\Laravel\Support\View; | ||
|
||
class InstallCommand extends Command | ||
{ | ||
private const LINKS = [ | ||
'Repository' => 'https://github.com/openai-php/laravel', | ||
'OpenAI PHP Docs' => 'https://github.com/openai-php/client#readme', | ||
'Join us on Telegram' => 'https://t.me/+66GDs6UM6RcxY2U8', | ||
]; | ||
|
||
private const FUNDING_LINKS = [ | ||
'Sponsor Sandro' => 'https://github.com/sponsors/gehrisandro', | ||
'Sponsor Nuno' => 'https://github.com/sponsors/nunomaduro', | ||
]; | ||
|
||
protected $signature = 'openai:install'; | ||
|
||
protected $description = 'Prepares the OpenAI client for use.'; | ||
|
||
public function handle(): void | ||
{ | ||
View::renderUsing($this->output); | ||
|
||
View::render('components.badge', [ | ||
'type' => 'INFO', | ||
'content' => 'Installing OpenAI for Laravel.', | ||
]); | ||
|
||
$this->copyConfig(); | ||
|
||
View::render('components.new-line'); | ||
|
||
$this->addEnvKeys('.env'); | ||
$this->addEnvKeys('.env.example'); | ||
|
||
View::render('components.new-line'); | ||
|
||
$wantsToSupport = $this->askToStarRepository(); | ||
|
||
$this->showLinks(); | ||
|
||
View::render('components.badge', [ | ||
'type' => 'INFO', | ||
'content' => 'Open your .env and add your OpenAI API key and organization id.', | ||
]); | ||
|
||
if ($wantsToSupport) { | ||
$this->openRepositoryInBrowser(); | ||
} | ||
} | ||
|
||
private function copyConfig(): void | ||
{ | ||
if (file_exists(config_path('openai.php'))) { | ||
View::render('components.two-column-detail', [ | ||
'left' => 'config/openai.php', | ||
'right' => 'File already exists.', | ||
]); | ||
|
||
return; | ||
} | ||
|
||
View::render('components.two-column-detail', [ | ||
'left' => 'config/openai.php', | ||
'right' => 'File created.', | ||
]); | ||
|
||
$this->callSilent('vendor:publish', [ | ||
'--provider' => ServiceProvider::class, | ||
]); | ||
} | ||
|
||
private function addEnvKeys(string $envFile): void | ||
{ | ||
$fileContent = file_get_contents(base_path($envFile)); | ||
|
||
if ($fileContent === false) { | ||
return; | ||
} | ||
|
||
if (str_contains($fileContent, 'OPENAI_API_KEY')) { | ||
View::render('components.two-column-detail', [ | ||
'left' => $envFile, | ||
'right' => 'Variables already exists.', | ||
]); | ||
|
||
return; | ||
} | ||
|
||
file_put_contents(base_path($envFile), PHP_EOL.'OPENAI_API_KEY='.PHP_EOL.'OPENAI_ORGANIZATION='.PHP_EOL, FILE_APPEND); | ||
|
||
View::render('components.two-column-detail', [ | ||
'left' => $envFile, | ||
'right' => 'OPENAI_API_KEY and OPENAI_ORGANIZATION variables added.', | ||
]); | ||
} | ||
|
||
private function askToStarRepository(): bool | ||
{ | ||
if (! $this->input->isInteractive()) { | ||
return false; | ||
} | ||
|
||
return $this->confirm(' <options=bold>Wanna show OpenAI for Laravel some love by starring it on GitHub?</>', false); | ||
} | ||
|
||
private function openRepositoryInBrowser(): void | ||
{ | ||
if (PHP_OS_FAMILY == 'Darwin') { | ||
exec('open https://github.com/openai-php/laravel'); | ||
} | ||
if (PHP_OS_FAMILY == 'Windows') { | ||
exec('start https://github.com/openai-php/laravel'); | ||
} | ||
if (PHP_OS_FAMILY == 'Linux') { | ||
exec('xdg-open https://github.com/openai-php/laravel'); | ||
} | ||
} | ||
|
||
private function showLinks(): void | ||
{ | ||
$links = [ | ||
...self::LINKS, | ||
...rand(0, 1) ? self::FUNDING_LINKS : array_reverse(self::FUNDING_LINKS, true), | ||
]; | ||
|
||
foreach ($links as $message => $link) { | ||
View::render('components.two-column-detail', [ | ||
'left' => $message, | ||
'right' => $link, | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Laravel\Support; | ||
|
||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Termwind\Termwind; | ||
|
||
use function Termwind\render; | ||
use function Termwind\renderUsing; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class View | ||
{ | ||
/** | ||
* The implementation of the output. | ||
*/ | ||
private static OutputInterface $output; | ||
|
||
/** | ||
* Renders views using the given Output instance. | ||
*/ | ||
public static function renderUsing(OutputInterface $output): void | ||
{ | ||
self::$output = $output; | ||
} | ||
|
||
/** | ||
* Renders the given view. | ||
* | ||
* @param array<string, mixed> $data | ||
*/ | ||
public static function render(string $path, array $data = []): void | ||
{ | ||
$contents = self::compile($path, $data); | ||
|
||
$existing = Termwind::getRenderer(); | ||
|
||
renderUsing(self::$output); | ||
|
||
try { | ||
render($contents); | ||
} finally { | ||
renderUsing($existing); | ||
} | ||
} | ||
|
||
/** | ||
* Compiles the given view. | ||
* | ||
* @param array<string, mixed> $data | ||
*/ | ||
private static function compile(string $path, array $data): string | ||
{ | ||
extract($data); | ||
|
||
ob_start(); | ||
|
||
$path = str_replace('.', '/', $path); | ||
|
||
include sprintf('%s/../../resources/views/%s.php', __DIR__, $path); | ||
|
||
$contents = ob_get_contents(); | ||
|
||
ob_clean(); | ||
|
||
return (string) $contents; | ||
} | ||
} |