1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
11
|
|
|
* THE SOFTWARE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ytake\LaravelCouchbase; |
15
|
|
|
|
16
|
|
|
use Illuminate\Cache\Repository; |
17
|
|
|
use Illuminate\Database\DatabaseManager; |
18
|
|
|
use Illuminate\Queue\QueueManager; |
19
|
|
|
use Illuminate\Session\CacheBasedSessionHandler; |
20
|
|
|
use Illuminate\Support\ServiceProvider; |
21
|
|
|
use Ytake\LaravelCouchbase\Cache\CouchbaseStore; |
22
|
|
|
use Ytake\LaravelCouchbase\Cache\MemcachedBucketStore; |
23
|
|
|
use Ytake\LaravelCouchbase\Database\Connectable; |
24
|
|
|
use Ytake\LaravelCouchbase\Database\CouchbaseConnection; |
25
|
|
|
use Ytake\LaravelCouchbase\Database\CouchbaseConnector; |
26
|
|
|
use Ytake\LaravelCouchbase\Queue\CouchbaseConnector as QueueConnector; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class CouchbaseServiceProvider. |
30
|
|
|
* |
31
|
|
|
* @codeCoverageIgnore |
32
|
|
|
* |
33
|
|
|
* @author Yuuki Takezawa<[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class CouchbaseServiceProvider extends ServiceProvider |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Bootstrap application services. |
39
|
|
|
*/ |
40
|
|
|
public function boot() |
41
|
|
|
{ |
42
|
|
|
$this->registerCouchbaseBucketCacheDriver(); |
43
|
|
|
$this->registerMemcachedBucketCacheDriver(); |
44
|
|
|
$this->registerCouchbaseQueueDriver(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function register() |
51
|
|
|
{ |
52
|
|
|
$configPath = __DIR__ . '/config/couchbase.php'; |
53
|
|
|
$this->mergeConfigFrom($configPath, 'couchbase'); |
54
|
|
|
$this->publishes([$configPath => config_path('couchbase.php')], 'couchbase'); |
55
|
|
|
$this->registerCouchbaseComponent(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function registerCouchbaseComponent() |
59
|
|
|
{ |
60
|
|
|
$this->app->singleton(Connectable::class, function () { |
61
|
|
|
return new CouchbaseConnector(); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$this->app->singleton('couchbase.memcached.connector', function () { |
65
|
|
|
return new MemcachedConnector(); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
// add couchbase session driver |
69
|
|
|
$this->app['session']->extend('couchbase', function ($app) { |
70
|
|
|
$minutes = $app['config']['session.lifetime']; |
71
|
|
|
|
72
|
|
|
return new CacheBasedSessionHandler(clone $this->app['cache']->driver('couchbase'), $minutes); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
// add couchbase session driver |
76
|
|
|
$this->app['session']->extend('couchbase-memcached', function ($app) { |
77
|
|
|
$minutes = $app['config']['session.lifetime']; |
78
|
|
|
|
79
|
|
|
return new CacheBasedSessionHandler(clone $this->app['cache']->driver('couchbase-memcached'), $minutes); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
// add couchbase extension |
83
|
|
|
$this->app['db']->extend('couchbase', function (array $config, $name) { |
84
|
|
|
/* @var \Couchbase\Cluster $cluster */ |
85
|
|
|
return new CouchbaseConnection($config, $name); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* register 'couchbase' cache driver. |
91
|
|
|
* for bucket type couchbase. |
92
|
|
|
*/ |
93
|
|
|
protected function registerCouchbaseBucketCacheDriver(): void |
94
|
|
|
{ |
95
|
|
|
$this->app['cache']->extend('couchbase', function ($app, $config) { |
96
|
|
|
/** @var \Couchbase\Cluster $cluster */ |
97
|
|
|
$cluster = $app['db']->connection($config['driver'])->getCouchbase(); |
98
|
|
|
$password = (isset($config['bucket_password'])) ? $config['bucket_password'] : ''; |
99
|
|
|
|
100
|
|
|
return new Repository( |
101
|
|
|
new CouchbaseStore( |
102
|
|
|
$cluster, |
103
|
|
|
$config['bucket'], |
104
|
|
|
$password, |
105
|
|
|
$app['config']->get('cache.prefix') |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* register 'couchbase' cache driver. |
113
|
|
|
* for bucket type memcached. |
114
|
|
|
*/ |
115
|
|
|
protected function registerMemcachedBucketCacheDriver(): void |
116
|
|
|
{ |
117
|
|
|
$this->app['cache']->extend('couchbase-memcached', function ($app, $config) { |
118
|
|
|
$prefix = $app['config']['cache.prefix']; |
119
|
|
|
$credential = $config['sasl'] ?? []; |
120
|
|
|
$memcachedBucket = $this->app['couchbase.memcached.connector'] |
121
|
|
|
->connect($config['servers']); |
122
|
|
|
|
123
|
|
|
return new Repository( |
124
|
|
|
new MemcachedBucketStore( |
|
|
|
|
125
|
|
|
$memcachedBucket, |
126
|
|
|
strval($prefix), |
127
|
|
|
$config['servers'], |
128
|
|
|
$credential |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
}); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* register custom queue 'couchbase' driver |
136
|
|
|
*/ |
137
|
|
|
protected function registerCouchbaseQueueDriver(): void |
138
|
|
|
{ |
139
|
|
|
/** @var QueueManager $queueManager */ |
140
|
|
|
$queueManager = $this->app['queue']; |
141
|
|
|
$queueManager->addConnector('couchbase', function () { |
142
|
|
|
/** @var DatabaseManager $databaseManager */ |
143
|
|
|
$databaseManager = $this->app['db']; |
144
|
|
|
|
145
|
|
|
return new QueueConnector($databaseManager); |
146
|
|
|
}); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This class, trait or interface has been deprecated.