-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Laravel service provider for zero-config setup #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | GitHub Configuration | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | Configure your GitHub access token for pull request operations. | ||
| | You can set this in your .env file as GITHUB_TOKEN or configure | ||
| | it in config/services.php under 'github.token'. | ||
| | | ||
| */ | ||
|
|
||
| 'github' => [ | ||
| 'token' => env('GITHUB_TOKEN'), | ||
| ], | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ConduitUI\Pr; | ||
|
|
||
| use ConduitUi\GitHubConnector\Connector; | ||
| use ConduitUI\Pr\Contracts\PrServiceInterface; | ||
| use ConduitUI\Pr\Services\GitHubPrService; | ||
| use Illuminate\Support\ServiceProvider; | ||
|
|
||
| class PrServiceProvider extends ServiceProvider | ||
| { | ||
| /** | ||
| * Register services. | ||
| */ | ||
| public function register(): void | ||
| { | ||
| $this->mergeConfigFrom( | ||
| __DIR__.'/../config/pr.php', | ||
| 'pr' | ||
| ); | ||
|
|
||
| $this->app->singleton(PrServiceInterface::class, function ($app) { | ||
| $token = $this->resolveToken($app); | ||
|
|
||
| if ($token === null) { | ||
| throw new \RuntimeException( | ||
| 'GitHub token not configured. Set GITHUB_TOKEN environment variable or publish and configure the pr.php config file.' | ||
| ); | ||
| } | ||
|
|
||
| return new GitHubPrService(new Connector($token)); | ||
| }); | ||
|
|
||
| $this->app->singleton(GitHubPrService::class, function ($app) { | ||
| return $app->make(PrServiceInterface::class); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Bootstrap services. | ||
| */ | ||
| public function boot(): void | ||
| { | ||
| if ($this->app->runningInConsole()) { | ||
| $this->publishes([ | ||
| __DIR__.'/../config/pr.php' => config_path('pr.php'), | ||
| ], 'pr-config'); | ||
| } | ||
|
|
||
| // Auto-configure the PullRequests facade only if token is available | ||
| // This prevents crashing apps that don't need PR functionality | ||
| if ($this->resolveToken($this->app) !== null) { | ||
| PullRequests::setService($this->app->make(PrServiceInterface::class)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the GitHub token from available configuration sources. | ||
| * | ||
| * @param \Illuminate\Contracts\Foundation\Application $app | ||
| */ | ||
| private function resolveToken($app): ?string | ||
| { | ||
| $sources = [ | ||
| $app['config']->get('pr.github.token'), | ||
| $app['config']->get('services.github.token'), | ||
| env('GITHUB_TOKEN'), | ||
| ]; | ||
|
|
||
| foreach ($sources as $token) { | ||
| if (is_string($token) && trim($token) !== '') { | ||
| return trim($token); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
Comment on lines
+46
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid eager resolution in public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/pr.php' => config_path('pr.php'),
], 'pr-config');
}
- // Auto-configure the PullRequests facade
- $service = $this->app->make(PrServiceInterface::class);
- PullRequests::setService($service);
+ // Auto-configure the PullRequests facade (lazy / non-fatal if unconfigured)
+ $token = $this->app['config']->get('pr.github.token')
+ ?: $this->app['config']->get('services.github.token')
+ ?: env('GITHUB_TOKEN');
+
+ if ($token) {
+ PullRequests::setService($this->app->make(PrServiceInterface::class));
+ }
}🤖 Prompt for AI Agents |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.