Skip to content

Commit 73a1c6b

Browse files
Feature/fix json mapper interface not available for di (#4)
* Fix JsonMapperInterface to be availalbe in ServiceProvider * Add changelog entry
1 parent e715492 commit 73a1c6b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- Make JsonMapperInterface availalbe in ServiceProvider
810

911
## [1.1.0] - 2020-07-12
1012
### Fixed

src/ServiceProvider.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
88
use JsonMapper\JsonMapper;
99
use JsonMapper\JsonMapperFactory;
10+
use JsonMapper\JsonMapperInterface;
1011

1112
class ServiceProvider extends BaseServiceProvider
1213
{
@@ -19,19 +20,18 @@ public function register()
1920
{
2021
$this->mergeConfigFrom(self::CONFIG_FILE, 'json-mapper');
2122

22-
switch (config('json-mapper.type')) {
23-
case 'best-fit':
24-
$this->app->singleton(JsonMapper::class, function () {
23+
$config = config('json-mapper.type');
24+
$this->app->singleton(JsonMapperInterface::class, static function () use ($config) {
25+
switch ($config) {
26+
case 'best-fit':
2527
return (new JsonMapperFactory())->bestFit();
26-
});
27-
break;
28-
case 'default':
29-
default:
30-
$this->app->singleton(JsonMapper::class, function () {
28+
case 'default':
29+
default:
3130
return (new JsonMapperFactory())->default();
32-
});
33-
break;
34-
}
31+
}
32+
});
33+
34+
$this->app->alias(JsonMapperInterface::class, JsonMapper::class);
3535
}
3636

3737
/**

tests/Unit/ServiceProviderTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Config\Repository;
88
use JsonMapper\JsonMapper;
9+
use JsonMapper\JsonMapperInterface;
910
use JsonMapper\LaravelPackage\ServiceProvider;
1011
use PHPUnit\Framework\TestCase;
1112

@@ -38,6 +39,9 @@ public function testRegisterMakesJsonMapperAvailableInApp(): void
3839

3940
$serviceProvider->register();
4041

42+
self::assertTrue($app->has(JsonMapperInterface::class));
43+
self::assertInstanceOf(JsonMapperInterface::class, $app->make(JsonMapperInterface::class));
44+
4145
self::assertTrue($app->has(JsonMapper::class));
4246
self::assertInstanceOf(JsonMapper::class, $app->make(JsonMapper::class));
4347
}

0 commit comments

Comments
 (0)