Skip to content

Commit

Permalink
Add install command
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 30, 2023
1 parent a8b217d commit 475f1f6
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 3 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ First, install OpenAI via the [Composer](https://getcomposer.org/) package manag
composer require openai-php/laravel
```

Next, publish the configuration file:
Then execute the install command:

```bash
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
php artisan openai:install
```

This will create a `config/openai.php` configuration file in your project, which you can modify to your needs
using environment variables:
using environment variables.
The environment variables for the OpenAI API key and organization id are already added to your `.env` file.

```env
OPENAI_API_KEY=sk-...
OPENAI_ORGANIZATION=org-...
```

Finally, you may use the `OpenAI` facade to access the OpenAI API:
Expand Down
17 changes: 17 additions & 0 deletions resources/views/components/badge.php
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>
1 change: 1 addition & 0 deletions resources/views/components/new-line.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div></div>
12 changes: 12 additions & 0 deletions resources/views/components/two-column-detail.php
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>

140 changes: 140 additions & 0 deletions src/Commands/InstallCommand.php
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,
]);
}
}
}
5 changes: 5 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenAI;
use OpenAI\Client;
use OpenAI\Contracts\ClientContract;
use OpenAI\Laravel\Commands\InstallCommand;
use OpenAI\Laravel\Exceptions\ApiKeyIsMissing;

/**
Expand Down Expand Up @@ -50,6 +51,10 @@ public function boot(): void
$this->publishes([
__DIR__.'/../config/openai.php' => config_path('openai.php'),
]);

$this->commands([
InstallCommand::class,
]);
}
}

Expand Down
72 changes: 72 additions & 0 deletions src/Support/View.php
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;
}
}

0 comments on commit 475f1f6

Please sign in to comment.