Skip to content

Commit 5eca9d0

Browse files
authored
Merge pull request #14 from tectonic/feature/scoped-services
Change singletons to scoped for Octane
2 parents 159f184 + 2942b95 commit 5eca9d0

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Install via composer:
1010

1111
composer require tectonic/laravel-localisation
1212

13+
Publish the configuration
14+
15+
```shell
16+
php artisan vendor:publish --provider="Tectonic\LaravelLocalisation\ServiceProvider"
17+
```
1318
## Documentation
1419

1520
You can view the rest of the documentation here: (https://github.com/tectonic/laravel-localisation/wiki)

config/localisation.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@
44
/**
55
* Define the model you wish to use for any translation features.
66
*/
7-
'model' => \Tectonic\LaravelLocalisation\Database\Translation::class
7+
'model' => \Tectonic\LaravelLocalisation\Database\Translation::class,
8+
9+
/**
10+
* Define your custom transformers that will be used to transform the data.
11+
*/
12+
'transformers' => [
13+
//
14+
]
815
];

src/ServiceProvider.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function boot()
3434
*/
3535
private function registerTranslationRepository()
3636
{
37-
$this->app->singleton(TranslationRepository::class, function($app) {
37+
$this->app->scoped(TranslationRepository::class, function($app) {
3838
$model = $app['config']->get('localisation.model');
3939

4040
return new EloquentTranslationRepository(new $model);
@@ -47,7 +47,7 @@ private function registerTranslationRepository()
4747
*/
4848
private function registerModelTransformer()
4949
{
50-
$this->app->singleton(ModelTransformer::class, function($app) {
50+
$this->app->scoped(ModelTransformer::class, function($app) {
5151
$modelTransformer = new ModelTransformer;
5252
$modelTransformer->setTranslationRepository($app->make(TranslationRepository::class));
5353

@@ -61,7 +61,7 @@ private function registerModelTransformer()
6161
*/
6262
private function registerCollectionTransformer()
6363
{
64-
$this->app->singleton(CollectionTransformer::class, function($app) {
64+
$this->app->scoped(CollectionTransformer::class, function($app) {
6565
$collectionTransformer = new CollectionTransformer;
6666
$collectionTransformer->setTranslationRepository($app->make(TranslationRepository::class));
6767

@@ -74,13 +74,17 @@ private function registerCollectionTransformer()
7474
*/
7575
private function registerTranslator()
7676
{
77-
$this->app->singleton('localisation.translator', function($app) {
77+
$this->app->scoped('localisation.translator', function($app) {
7878
$translatorEngine = new Engine;
79-
79+
8080
$translatorEngine->registerTransformer(
8181
$app->make(ModelTransformer::class),
8282
$app->make(CollectionTransformer::class),
83-
$app->make(PaginationTransformer::class)
83+
$app->make(PaginationTransformer::class),
84+
...array_map(
85+
fn ($transformer) => $app->make($transformer),
86+
$app['config']->get('localisation.transformers', [])
87+
)
8488
);
8589

8690
return $translatorEngine;

0 commit comments

Comments
 (0)