Skip to content

Commit c20fdf5

Browse files
First commit.
0 parents  commit c20fdf5

14 files changed

+243
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Drupal Services Examples
2+
3+
A set of examples of different ways to use the services system in Drupal.

drupal_services_examples.info.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'Drupal Services Examples'
2+
type: module
3+
description: 'A set of examples of Drupal services in action.'
4+
package: Example
5+
core_version_requirement: ^10 || ^11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'Services Argument Example'
2+
type: module
3+
description: 'Passing arguments to services.'
4+
package: Example
5+
core_version_requirement: ^10 || ^11
6+
dependencies:
7+
- drupal_services_examples:drupal_services_examples
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
services:
2+
services_argument_example.single_argument:
3+
class: \Drupal\services_argument_example\SingleArgument
4+
arguments: ['@serialization.json']
5+
6+
# uuid?
7+
# state
8+
9+
#parent argumen
10+
11+
#decorates
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Drupal\services_argument_example;
4+
5+
use Drupal\Component\Serialization\SerializationInterface;
6+
7+
/**
8+
* An example of a service that accepts an argument.
9+
*
10+
* This service has a dependency on the serialization.json service. Although
11+
* any of the classes that implement SerializationInterface will work as long
12+
* as the payload is of the correct type.
13+
*/
14+
class SingleArgument implements SingleArgumentInterface {
15+
16+
/**
17+
* Creates a SingleArgument service object.
18+
*
19+
* @param \Drupal\Component\Serialization\SerializationInterface $serializer
20+
* A serializer service.
21+
*/
22+
public function __construct(protected SerializationInterface $serializer) {
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function removeItemFromPayload(string $payload, int $id):string {
29+
$payload = $this->serializer->decode($payload);
30+
foreach ($payload as $key => $item) {
31+
if ($item['id'] === $id) {
32+
unset($payload[$key]);
33+
}
34+
}
35+
return $this->serializer->encode($payload);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Drupal\services_argument_example;
4+
5+
/**
6+
* Interface for the SimpleService service.
7+
*/
8+
interface SingleArgumentInterface {
9+
10+
/**
11+
* Removes an item from a payload.
12+
*
13+
* @param string $payload
14+
* The payload to alter.
15+
* @param int $id
16+
* The ID to remove from the payload.
17+
*
18+
* @return string
19+
* The resulting payload string.
20+
*/
21+
public function removeItemFromPayload(string $payload, int $id):string;
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Drupal\Tests\services_argument_example\Kernel;
4+
5+
use Drupal\KernelTests\KernelTestBase;
6+
7+
/**
8+
* Kernel tests for the SingleArgument service.
9+
*/
10+
class SingleArgumentTest extends KernelTestBase {
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
protected static $modules = [
16+
'services_argument_example',
17+
];
18+
19+
/**
20+
* Test that the getArray method returns an array.
21+
*/
22+
public function testSimpleService() {
23+
$payload = '{"0":{"id":1},"1":{"id":2},"2":{"id":3}}';
24+
$id = 2;
25+
26+
$result = '{"0":{"id":1},"2":{"id":3}}';
27+
28+
/** @var \Drupal\services_argument_example\SingleArgument $simpleService */
29+
$argumentService = \Drupal::service('services_argument_example.single_argument');
30+
$this->assertEquals($result, $argumentService->removeItemFromPayload($payload, $id));
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Drupal\Tests\services_argument_example\Unit;
4+
5+
use Drupal\Component\Serialization\Json;
6+
use Drupal\services_argument_example\SingleArgument;
7+
use Drupal\Tests\UnitTestCase;
8+
9+
/**
10+
* Unit tests for the SingleArgument service.
11+
*/
12+
class SingleArgumentTest extends UnitTestCase {
13+
14+
/**
15+
* Test that removeItemFromPayload removes an item from the payload.
16+
*/
17+
public function testSimpleService() {
18+
$payload = '{"0":{"id":1},"1":{"id":2},"2":{"id":3}}';
19+
$id = 2;
20+
21+
$result = '{"0":{"id":1},"2":{"id":3}}';
22+
23+
$argumentService = new SingleArgument(new Json());
24+
$this->assertEquals($result, $argumentService->removeItemFromPayload($payload, $id));
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'Services Simple Example'
2+
type: module
3+
description: 'A very simple service.'
4+
package: Example
5+
core_version_requirement: ^10 || ^11
6+
dependencies:
7+
- drupal_services_examples:drupal_services_examples
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
services_simple_example.simple_service:
3+
class: \Drupal\services_simple_example\SimpleService
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Drupal\services_simple_example;
4+
5+
/**
6+
* An example of a simple service.
7+
*
8+
* This service has no dependencies.
9+
*/
10+
class SimpleService implements SimpleServiceInterface {
11+
12+
/**
13+
* {@inheritDoc}
14+
*/
15+
public function getArray():array {
16+
return [];
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Drupal\services_simple_example;
4+
5+
/**
6+
* Interface for the SimpleService service.
7+
*/
8+
interface SimpleServiceInterface {
9+
10+
/**
11+
* Get an array.
12+
*
13+
* @return array
14+
* The array.
15+
*/
16+
public function getArray():array;
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Drupal\Tests\services_simple_example\Kernel;
4+
5+
use Drupal\KernelTests\KernelTestBase;
6+
7+
/**
8+
* Kernel tests for the SimpleService service.
9+
*/
10+
class SimpleServiceTest extends KernelTestBase {
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
protected static $modules = [
16+
'services_simple_example',
17+
];
18+
19+
/**
20+
* Test that the getArray method returns an array.
21+
*/
22+
public function testSimpleService() {
23+
/** @var \Drupal\services_simple_example\SimpleService $simpleService */
24+
$simpleService = \Drupal::service('services_simple_example.simple_service');
25+
$this->assertEquals([], $simpleService->getArray());
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Drupal\Tests\services_simple_example\Unit;
4+
5+
use Drupal\services_simple_example\SimpleService;
6+
use Drupal\Tests\UnitTestCase;
7+
8+
/**
9+
* Unit tests for the SimpleService service.
10+
*/
11+
class SimpleServiceTest extends UnitTestCase {
12+
13+
/**
14+
* Test that the getArray method returns an array.
15+
*/
16+
public function testSimpleService() {
17+
$simpleService = new SimpleService();
18+
$this->assertEquals([], $simpleService->getArray());
19+
}
20+
21+
}

0 commit comments

Comments
 (0)