Skip to content

Commit bcf756c

Browse files
committed
Refactor CouchbaseServiceProvider to skip database operations for certain commands
1 parent bdb83c4 commit bcf756c

File tree

1 file changed

+54
-57
lines changed

1 file changed

+54
-57
lines changed

app/Providers/CouchbaseServiceProvider.php

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313

1414
class CouchbaseServiceProvider extends ServiceProvider
1515
{
16+
/**
17+
* Commands that don't require database access
18+
*/
19+
private $nonDatabaseCommands = [
20+
'key:generate',
21+
'config:cache',
22+
'config:clear',
23+
'view:cache',
24+
'view:clear',
25+
'route:cache',
26+
'route:clear',
27+
'package:discover'
28+
];
29+
30+
private function shouldSkipDatabaseOperations(): bool
31+
{
32+
if (!$this->app->runningInConsole()) {
33+
return false;
34+
}
35+
36+
$command = $_SERVER['argv'][1] ?? null;
37+
return $command && in_array($command, $this->nonDatabaseCommands);
38+
}
39+
1640
private function createClusterConnection($config, $retries = 3)
1741
{
1842
$lastException = null;
@@ -63,72 +87,31 @@ private function createClusterConnection($config, $retries = 3)
6387
*/
6488
public function register()
6589
{
90+
if ($this->shouldSkipDatabaseOperations()) {
91+
return;
92+
}
93+
94+
// Bind cluster as a singleton but don't create it yet
6695
$this->app->singleton('couchbase.cluster', function ($app) {
6796
$config = $app['config']['couchbase'];
6897
return $this->createClusterConnection($config);
6998
});
7099

100+
// Bind bucket as a singleton but don't create it yet
71101
$this->app->singleton('couchbase.bucket', function ($app) {
72-
try {
73-
$cluster = $app->make('couchbase.cluster');
74-
$config = $app['config']['couchbase'];
75-
return $cluster->bucket($config['bucket']);
76-
} catch (\Exception $e) {
77-
\Log::error('Failed to connect to Couchbase bucket', [
78-
'error' => $e->getMessage(),
79-
'bucket' => $config['bucket']
80-
]);
81-
throw $e;
82-
}
83-
});
84-
85-
$this->app->singleton('couchbase.airlineCollection', function ($app) {
86-
try {
87-
$bucket = $app->make('couchbase.bucket');
88-
return $bucket->scope('inventory')->collection('airline');
89-
} catch (\Exception $e) {
90-
\Log::error('Failed to get airline collection', [
91-
'error' => $e->getMessage()
92-
]);
93-
throw $e;
94-
}
95-
});
96-
97-
$this->app->singleton('couchbase.airportCollection', function ($app) {
98-
try {
99-
$bucket = $app->make('couchbase.bucket');
100-
return $bucket->scope('inventory')->collection('airport');
101-
} catch (\Exception $e) {
102-
\Log::error('Failed to get airport collection', [
103-
'error' => $e->getMessage()
104-
]);
105-
throw $e;
106-
}
107-
});
108-
109-
$this->app->singleton('couchbase.routeCollection', function ($app) {
110-
try {
111-
$bucket = $app->make('couchbase.bucket');
112-
return $bucket->scope('inventory')->collection('route');
113-
} catch (\Exception $e) {
114-
\Log::error('Failed to get route collection', [
115-
'error' => $e->getMessage()
116-
]);
117-
throw $e;
118-
}
102+
$cluster = $app->make('couchbase.cluster');
103+
$config = $app['config']['couchbase'];
104+
return $cluster->bucket($config['bucket']);
119105
});
120106

121-
$this->app->singleton('couchbase.hotelCollection', function ($app) {
122-
try {
107+
// Bind collections as singletons but don't create them yet
108+
$collections = ['airline', 'airport', 'route', 'hotel'];
109+
foreach ($collections as $collection) {
110+
$this->app->bind("couchbase.{$collection}Collection", function ($app) use ($collection) {
123111
$bucket = $app->make('couchbase.bucket');
124-
return $bucket->scope('inventory')->collection('hotel');
125-
} catch (\Exception $e) {
126-
\Log::error('Failed to get hotel collection', [
127-
'error' => $e->getMessage()
128-
]);
129-
throw $e;
130-
}
131-
});
112+
return $bucket->scope('inventory')->collection($collection);
113+
});
114+
}
132115
}
133116

134117
/**
@@ -137,6 +120,20 @@ public function register()
137120
* @return void
138121
*/
139122
public function boot()
123+
{
124+
if ($this->shouldSkipDatabaseOperations()) {
125+
return;
126+
}
127+
128+
if (!$this->app->environment('testing')) {
129+
$this->createSearchIndex();
130+
}
131+
}
132+
133+
/**
134+
* Create the search index if needed
135+
*/
136+
private function createSearchIndex()
140137
{
141138
try {
142139
$indexFilePath = 'hotel_search_index.json';

0 commit comments

Comments
 (0)