Skip to content

Commit 59f98ff

Browse files
#1 Rework Tasks & Transfomers using AdapterRegistry
1 parent b0592fd commit 59f98ff

20 files changed

+274
-647
lines changed

config/services/registry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
cleverage_cache_process.registry.adapter:
3+
class: CleverAge\CacheProcessBundle\Registry\AdapterRegistry
4+
public: false

config/services/task.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
services:
2+
cleverage_cache_process.task.get:
3+
class: CleverAge\CacheProcessBundle\Task\GetTask
4+
public: false
5+
shared: false
6+
arguments:
7+
- '@cleverage_cache_process.registry.adapter'
8+
CleverAge\CacheProcessBundle\Task\GetTask:
9+
alias: cleverage_cache_process.task.get
10+
public: true
11+
12+
cleverage_cache_process.task.set:
13+
class: CleverAge\CacheProcessBundle\Task\SetTask
14+
public: false
15+
shared: false
16+
arguments:
17+
- '@cleverage_cache_process.registry.adapter'
18+
CleverAge\CacheProcessBundle\Task\SetTask:
19+
alias: cleverage_cache_process.task.set
20+
public: true

config/services/task.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

config/services/transformer.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Adapter/Adapter.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CleverAge\CacheProcessBundle\Adapter;
6+
7+
use Psr\Cache\CacheItemInterface;
8+
use Symfony\Component\Cache\CacheItem;
9+
10+
class Adapter implements AdapterInterface
11+
{
12+
public function __construct(
13+
private readonly \Symfony\Component\Cache\Adapter\AdapterInterface $adapter,
14+
private readonly string $code,
15+
) {}
16+
17+
public function getCode(): string
18+
{
19+
return $this->code;
20+
}
21+
22+
public function getItem(mixed $key): CacheItem
23+
{
24+
return $this->adapter->getItem($key);
25+
}
26+
27+
public function getItems(array $keys = []): iterable
28+
{
29+
return $this->adapter->getItems($keys);
30+
}
31+
32+
public function clear(string $prefix = ''): bool
33+
{
34+
return $this->adapter->clear($prefix);
35+
}
36+
37+
public function hasItem(string $key): bool
38+
{
39+
return $this->adapter->hasItem($key);
40+
}
41+
42+
public function deleteItem(string $key): bool
43+
{
44+
return $this->adapter->deleteItem($key);
45+
}
46+
47+
public function deleteItems(array $keys): bool
48+
{
49+
return $this->adapter->deleteItems($keys);
50+
}
51+
52+
public function save(CacheItemInterface $item): bool
53+
{
54+
return $this->adapter->save($item);
55+
}
56+
57+
public function saveDeferred(CacheItemInterface $item): bool
58+
{
59+
return $this->adapter->saveDeferred($item);
60+
}
61+
62+
public function commit(): bool
63+
{
64+
return $this->adapter->commit();
65+
}
66+
}

src/Adapter/AdapterInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CleverAge\CacheProcessBundle\Adapter;
6+
7+
interface AdapterInterface extends \Symfony\Component\Cache\Adapter\AdapterInterface
8+
{
9+
/**
10+
* Return the code of the adapter used in adapter registry.
11+
*/
12+
public function getCode(): string;
13+
}

src/CleverAgeCacheProcessBundle.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace CleverAge\CacheProcessBundle\src;
11+
namespace CleverAge\CacheProcessBundle;
1212

13+
use CleverAge\ProcessBundle\DependencyInjection\Compiler\RegistryCompilerPass;
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
1315
use Symfony\Component\HttpKernel\Bundle\Bundle;
1416

1517
class CleverAgeCacheProcessBundle extends Bundle
1618
{
19+
/**
20+
* Adding compiler passes to inject services into registry.
21+
*/
22+
public function build(ContainerBuilder $container): void
23+
{
24+
$container->addCompilerPass(
25+
new RegistryCompilerPass(
26+
'cleverage_cache_process.registry.adapter',
27+
'cleverage.cache.adapter',
28+
'addAdapter'
29+
)
30+
);
31+
}
32+
1733
public function getPath(): string
1834
{
1935
return \dirname(__DIR__);

src/DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CleverAge\CacheProcessBundle\Exception;
6+
7+
/**
8+
* Exception thrown when trying to fetch a missing Cache Adapter.
9+
*/
10+
class MissingAdapterException extends \Exception {}

src/Registry/AdapterRegistry.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CleverAge\CacheProcessBundle\Registry;
6+
7+
use CleverAge\CacheProcessBundle\Adapter\AdapterInterface;
8+
use CleverAge\CacheProcessBundle\Exception\MissingAdapterException;
9+
10+
/**
11+
* Holds all tagged cache adapters services.
12+
*/
13+
class AdapterRegistry
14+
{
15+
/** @var AdapterInterface[] */
16+
private array $adapters = [];
17+
18+
public function addAdapter(AdapterInterface $adapter): void
19+
{
20+
if (\array_key_exists($adapter->getCode(), $this->adapters)) {
21+
throw new \UnexpectedValueException("Adapter {$adapter->getCode()} is already defined");
22+
}
23+
$this->adapters[$adapter->getCode()] = $adapter;
24+
}
25+
26+
/**
27+
* @throws MissingAdapterException
28+
*/
29+
public function getAdapter(string $code): AdapterInterface
30+
{
31+
if (!\array_key_exists($code, $this->adapters)) {
32+
throw new MissingAdapterException("Adapter {$code} is missing");
33+
}
34+
35+
return $this->adapters[$code];
36+
}
37+
}

0 commit comments

Comments
 (0)