Skip to content

Commit adc1d79

Browse files
committed
Containers
1 parent d789ab0 commit adc1d79

9 files changed

+215
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"fig/http-message-util": "^1",
3030
"psr/http-message": "^2",
3131
"webservco/data": "^0",
32+
"webservco/dependency-container": "^0",
3233
"webservco/form": "^0",
3334
"webservco/http": "^0",
3435
"webservco/jsonapi": "^0",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Container;
6+
7+
use WebServCo\JSONAPI\Contract\Service\Container\APIJSONAPIServiceContainerInterface;
8+
9+
/**
10+
* A default JSONAPI service container implementation for API endpoints.
11+
*
12+
* Can be used directly if no special dependencies are required.
13+
*/
14+
final class APIJSONAPIServiceContainer extends AbstractJSONAPIServiceContainer implements
15+
APIJSONAPIServiceContainerInterface
16+
{
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Container;
6+
7+
use WebServCo\Data\Contract\Extraction\DataExtractionContainerInterface;
8+
use WebServCo\JSONAPI\Contract\Service\Container\APIJSONAPIServiceContainerInterface;
9+
use WebServCo\JSONAPI\Contract\Service\Container\APILocalServiceContainerInterface;
10+
11+
/**
12+
* A default local service container implementation for API endpoints.
13+
*
14+
* Can be used directly if no special dependencies are required.
15+
*/
16+
final class APILocalServiceContainer implements APILocalServiceContainerInterface
17+
{
18+
private ?APIJSONAPIServiceContainerInterface $jsonApiServiceContainer = null;
19+
20+
public function __construct(private DataExtractionContainerInterface $dataExtractionContainer)
21+
{
22+
}
23+
24+
public function getJsonApiServiceContainer(): APIJSONAPIServiceContainerInterface
25+
{
26+
if ($this->jsonApiServiceContainer === null) {
27+
$this->jsonApiServiceContainer = new APIJSONAPIServiceContainer($this->dataExtractionContainer);
28+
}
29+
30+
return $this->jsonApiServiceContainer;
31+
}
32+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Container;
6+
7+
use WebServCo\Data\Contract\Extraction\DataExtractionContainerInterface;
8+
use WebServCo\Http\Service\Message\Request\RequestBodyService;
9+
use WebServCo\Http\Service\Message\Request\RequestHeaderService;
10+
use WebServCo\JSONAPI\Contract\Factory\Handler\JSONAPIHandlerFactoryInterface;
11+
use WebServCo\JSONAPI\Contract\Service\Container\JSONAPIServiceContainerInterface;
12+
use WebServCo\JSONAPI\Contract\Service\JSONAPIRequestServiceInterface;
13+
use WebServCo\JSONAPI\Factory\Handler\JSONAPIDefaultHandlerFactory;
14+
use WebServCo\JSONAPI\Service\JSONAPIRequestService;
15+
16+
/**
17+
* An abstract JSONAPIServiceContainerInterface implementation.
18+
*/
19+
abstract class AbstractJSONAPIServiceContainer implements JSONAPIServiceContainerInterface
20+
{
21+
private ?JSONAPIHandlerFactoryInterface $defaultItemHandlerFactory = null;
22+
23+
private ?JSONAPIRequestServiceInterface $jsonApiRequestService = null;
24+
25+
public function __construct(protected DataExtractionContainerInterface $dataExtractionContainer)
26+
{
27+
}
28+
29+
public function getDefaultHandlerFactory(): JSONAPIHandlerFactoryInterface
30+
{
31+
if ($this->defaultItemHandlerFactory === null) {
32+
$this->defaultItemHandlerFactory = new JSONAPIDefaultHandlerFactory(
33+
$this->dataExtractionContainer,
34+
$this->getJsonApiRequestService(),
35+
);
36+
}
37+
38+
return $this->defaultItemHandlerFactory;
39+
}
40+
41+
public function getJsonApiRequestService(): JSONAPIRequestServiceInterface
42+
{
43+
if ($this->jsonApiRequestService === null) {
44+
$this->jsonApiRequestService = new JSONAPIRequestService(
45+
$this->dataExtractionContainer,
46+
new RequestBodyService(),
47+
new RequestHeaderService(),
48+
);
49+
}
50+
51+
return $this->jsonApiRequestService;
52+
}
53+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Contract\Factory\Handler;
6+
7+
use WebServCo\JSONAPI\Contract\Service\JSONAPIHandlerInterface;
8+
9+
/**
10+
* A JSONAPI Handler Factory Interface.
11+
*/
12+
interface JSONAPIHandlerFactoryInterface
13+
{
14+
public function createHandler(): JSONAPIHandlerInterface;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Contract\Service\Container;
6+
7+
/**
8+
* A default JSONAPI Service Container Interface for API endpoints.
9+
*
10+
* Can be used directly if no special dependencies are required.
11+
*/
12+
interface APIJSONAPIServiceContainerInterface extends JSONAPIServiceContainerInterface
13+
{
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Contract\Service\Container;
6+
7+
use WebServCo\DependencyContainer\Contract\LocalDependencyContainerInterface;
8+
9+
/**
10+
* A default Local service container for API endpoints.
11+
*
12+
* Can be used directly if no special dependencies are required.
13+
*/
14+
interface APILocalServiceContainerInterface extends LocalDependencyContainerInterface
15+
{
16+
public function getJsonApiServiceContainer(): APIJSONAPIServiceContainerInterface;
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Contract\Service\Container;
6+
7+
use WebServCo\JSONAPI\Contract\Factory\Handler\JSONAPIHandlerFactoryInterface;
8+
use WebServCo\JSONAPI\Contract\Service\JSONAPIRequestServiceInterface;
9+
10+
/**
11+
* A JSONAPI Service Container Interface.
12+
*/
13+
interface JSONAPIServiceContainerInterface
14+
{
15+
/**
16+
* A factory for a default JSONAPI handler.
17+
*
18+
* Use case: no custom JSONAPI handler implementation is needed.
19+
*/
20+
public function getDefaultHandlerFactory(): JSONAPIHandlerFactoryInterface;
21+
22+
public function getJsonApiRequestService(): JSONAPIRequestServiceInterface;
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebServCo\JSONAPI\Factory\Handler;
6+
7+
use Fig\Http\Message\RequestMethodInterface;
8+
use WebServCo\Data\Contract\Extraction\DataExtractionContainerInterface;
9+
use WebServCo\JSONAPI\Contract\Factory\Handler\JSONAPIHandlerFactoryInterface;
10+
use WebServCo\JSONAPI\Contract\Service\JSONAPIHandlerInterface;
11+
use WebServCo\JSONAPI\Contract\Service\JSONAPIRequestServiceInterface;
12+
use WebServCo\JSONAPI\Service\Handler\JSONAPIItemHandler;
13+
14+
/**
15+
* Creates a default JSONAPI GET handler.
16+
*
17+
* Use case: take advantage of form validation to handle JSONAPI request errors.
18+
*/
19+
final class JSONAPIDefaultHandlerFactory implements JSONAPIHandlerFactoryInterface
20+
{
21+
public function __construct(
22+
private DataExtractionContainerInterface $dataExtractionContainer,
23+
private JSONAPIRequestServiceInterface $jsonApiRequestService,
24+
) {
25+
}
26+
27+
public function createHandler(): JSONAPIHandlerInterface
28+
{
29+
return new JSONAPIItemHandler(
30+
[
31+
RequestMethodInterface::METHOD_GET,
32+
],
33+
$this->dataExtractionContainer,
34+
$this->jsonApiRequestService,
35+
// no fields
36+
[],
37+
// no filters
38+
[],
39+
// no validators
40+
[],
41+
);
42+
}
43+
}

0 commit comments

Comments
 (0)