8000 [Feature] Inject the application instance into server classes · alescx/laravel-json-api-core@85ae7e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 85ae7e2

Browse files
committed
[Feature] Inject the application instance into server classes
This allows child classes to call methods such as environment(). Previously the type-hinting was only for the Illuminate container, so there was no guarantee that the container was the application instance (even though it probably was).
1 parent 9cdee0a commit 85ae7e2

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

CHANGELOG.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ All notable changes to this project will be documented in this file. This projec
2828
- The `Core\Resources\Factory` class constructor was amended to only expect a schema container. Additionally the method
2929
signature of the protected `build()` method was changed to receive a schema instance and the model being converted to
3030
a JSON:API resource.
31-
32-
### Removed
33-
34-
- The `Contracts\Resources\Factory::handles()` method has been removed in favour of using the new `canCreate()` method
35-
instead.
36-
- The `Contracts\Schema\Container::resources()` method has been removed, in favour of resource factories using the
37-
schema container's `existsForModel()` and `schemaForModel()` methods.
31+
- The `Core\Server\Server` and `Core\Server\ServerRepository` classes are now injected with the Laravel application
32+
instance, instead of just type-hinting the container. This change was made to allow code within servers to access the
33+
application environment, using `$this->app->environment()` rather than having to use `app()->environment()`
34+
(which used to be the case as the injection was only type-hinted as the container contract).
3835

3936
### Fixed
4037

@@ -43,6 +40,18 @@ All notable changes to this project will be documented in this file. This projec
4340
path. The server's `url()` method now passes these as we *always* went them appended, regardless of whether the API's
4441
base path has a HTTP host or not.
4542

43+
### Deprecated
44+
45+
- The `Core\Server\Server::$container` property is deprecated and will be removed in `1.0.0-stable`. Child classes
46+
should use the new `Server::$app` property instead.
47+
48+
### Removed
49+
50+
- The `Contracts\Resources\Factory::handles()` method has been removed in favour of using the new `canCreate()` method
51+
instead.
52+
- The `Contracts\Schema\Container::resources()` method has been removed, in favour of resource factories using the
53+
schema container's `existsForModel()` and `schemaForModel()` methods.
54+
4655
## [1.0.0-beta.1] - 2021-03-30
4756

4857
### Added

src/Core/Server/Server.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace LaravelJsonApi\Core\Server;
2121

2222
use Illuminate\Contracts\Container\Container as IlluminateContainer;
23+
use Illuminate\Contracts\Foundation\Application;
2324
use Illuminate\Contracts\Routing\UrlGenerator;
2425
use Illuminate\Contracts\Routing\UrlRoutable;
2526
use Illuminate\Support\Arr;
@@ -48,8 +49,14 @@ abstract class Server implements ServerContract
4849
*/
4950
protected string $baseUri = '';
5051

52+
/**
53+
* @var Application
54+
*/
55+
protected Application $app;
56+
5157
/**
5258
* @var IlluminateContainer
59+
* @deprecated 1.0-stable use `$this->app` instead.
5360
*/
5461
protected IlluminateContainer $container;
5562

@@ -78,16 +85,17 @@ abstract protected function allSchemas(): array;
7885
/**
7986
* Server constructor.
8087
*
81-
* @param IlluminateContainer $container
88+
* @param Application $app
8289
* @param string $name
8390
*/
84-
public function __construct(IlluminateContainer $container, string $name)
91+
public function __construct(Application $app, string $name)
8592
{
8693
if (empty($name)) {
8794
throw new InvalidArgumentException('Expecting a non-empty string.');
8895
}
8996

90-
$this->container = $container;
97+
$this->app = $app;
98+
$this->container = $app;
9199
$this->name = $name;
92100
}
93101

src/Core/Server/ServerRepository.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace LaravelJsonApi\Core\Server;
2121

2222
use Illuminate\Contracts\Config\Repository as ConfigRepository;
23-
use Illuminate\Contracts\Container\Container as IlluminateContainer;
23+
use Illuminate\Contracts\Foundation\Application;
2424
use InvalidArgumentException;
2525
use LaravelJsonApi\Contracts\Server\Repository as RepositoryContract;
2626
use LaravelJsonApi\Contracts\Server\Server as ServerContract;
@@ -31,9 +31,9 @@ class ServerRepository implements RepositoryContract
3131
{
3232

3333
/**
34-
* @var IlluminateContainer
34+
* @var Application
3535
*/
36-
private IlluminateContainer $container;
36+
private Application $app;
3737

3838
/**
3939
* @var ConfigRepository
@@ -48,12 +48,12 @@ class ServerRepository implements RepositoryContract
4848
/**
4949
* ServerRepository constructor.
5050
*
51-
* @param IlluminateContainer $container
51+
* @param Application $app
5252
* @param ConfigRepository $config
5353
*/
54-
public function __construct(IlluminateContainer $container, ConfigRepository $config)
54+
public function __construct(Application $app, ConfigRepository $config)
5555
{
56-
$this->container = $container;
56+
$this->app = $app;
5757
$this->config = $config;
5858
$this->cache = [];
5959
}
@@ -78,7 +78,7 @@ public function server(string $name): ServerContract
7878
}
7979

8080
try {
81-
$server = new $class($this->container, $name);
81+
$server = new $class($this->app, $name);
8282
} catch (Throwable $ex) {
8383
throw new RuntimeException(
8484
"Unable to construct server {$name} using class {$class}.",

tests/Unit/Server/ServerRepositoryTest.php

Lines changed: 4 additions & 4 deletions
D984
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace LaravelJsonApi\Core\Tests\Unit\Server;
2121

2222
use Illuminate\Contracts\Config\Repository as ConfigRepository;
23-
use Illuminate\Contracts\Container\Container as IlluminateContainer;
23+
use Illuminate\Contracts\Foundation\Application;
2424
use LaravelJsonApi\Core\Server\ServerRepository;
2525
use PHPUnit\Framework\TestCase;
2626

@@ -32,7 +32,7 @@ public function test(): void
3232
$name = 'v1';
3333
$klass = TestServer::class;
3434

35-
$container = $this->createMock(IlluminateContainer::class);
35+
$app = $this->createMock(Application::class);
3636
$config = $this->createMock(ConfigRepository::class);
3737

3838
$config
@@ -41,9 +41,9 @@ public function test(): void
4141
->with("jsonapi.servers.{$name}")
4242
->willReturn($klass);
4343

44-
$expected = new TestServer($container, $name);
44+
$expected = new TestServer($app, $name);
4545

46-
$repository = new ServerRepository($container, $config);
46+
$repository = new ServerRepository($app, $config);
4747

4848
$actual = $repository->server($name);
4949

0 commit comments

Comments
 (0)
0