Skip to content

Commit

Permalink
feat: LockableResource replaced by alias
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Jun 28, 2024
1 parent 8cfe33f commit 529d30a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 47 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ var_dump($criticalOutput);

## Do you need to accept only locked resources?

Use [`LockedResource`s](./src/LockedResource.php) if you need to be sure that you are not processing resource outside it's critical section.
Use [`LockedResource`](./src/LockedResource.php) if you need to be sure that you are not processing resource outside it's critical section.

```php
namespace PetrKnap\CriticalSection;

use Symfony\Component\Lock\NoLock;

/** @param LockedResource<Example\Resource> $lockedResource */
function f(LockedResource $lockedResource) {
echo $lockedResource->value;
/** @param Locked<Example\Resource> $resource */
function f(Locked $resource) {
echo $resource->value;
}

$lock = new NoLock();
$resource = LockableResource::create(new Example\Resource('data'), $lock);
$resource = LockableResource::of(new Example\Resource('data'), $lock);
CriticalSection::withLock($lock)(fn () => f($resource));
```

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"autoload-dev": {
"psr-4": {
"PetrKnap\\CriticalSection\\": "tests"
}
},
"files": [
"src/aliases.php"
]
},
"config": {
"allow-plugins": false,
Expand Down
11 changes: 0 additions & 11 deletions src/Exception/LockableResourceException.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Exception/LockedResourceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PetrKnap\CriticalSection\Exception;

interface LockedResourceException extends LockableResourceException
use Throwable;

interface LockedResourceException extends Throwable
{
}
25 changes: 0 additions & 25 deletions src/LockableResource.php

This file was deleted.

20 changes: 19 additions & 1 deletion src/LockedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PetrKnap\CriticalSection;

use Symfony\Component\Lock\LockInterface;

/**
* @template T of mixed
*
Expand All @@ -14,7 +16,7 @@ abstract class LockedResource
/**
* @param T $resource
*/
public function __construct(
protected function __construct(
private readonly mixed $resource,
) {
}
Expand All @@ -37,6 +39,22 @@ public function __set(string $name, mixed $value): void
$this->get()->$name = $value;
}

/**
* @template U of mixed
*
* @param U $resource
*
* @return Locked<U>
*/
public static function of(
mixed $resource,
LockInterface $lock1,
LockInterface ...$lockN,
): self {
/** @var Locked<U> */
return new Symfony\Lock\LockedResource($resource, $lock1, ...$lockN);
}

/**
* @return T
*
Expand Down
12 changes: 12 additions & 0 deletions src/aliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* Aliases for better code readability
*/

declare(strict_types=1);

namespace PetrKnap\CriticalSection;

class_alias(LockedResource::class, 'PetrKnap\CriticalSection\LockableResource');
class_alias(LockedResource::class, 'PetrKnap\CriticalSection\Locked');
22 changes: 22 additions & 0 deletions tests/AliasesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace PetrKnap\CriticalSection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\LockInterface;

final class AliasesTest extends TestCase
{
public function testLockedResourceAliases(): void
{
self::assertInstanceOf(
Locked::class,
LockableResource::of(
new Example\Resource(),
self::createStub(LockInterface::class),
),
);
}
}
2 changes: 1 addition & 1 deletion tests/Example/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final class Resource
{
public function __construct(
public string $value,
public string $value = '',
) {
}

Expand Down
4 changes: 2 additions & 2 deletions tests/LockedResourceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function testCouldGetLockedResource(): void
public function testIsMixin(): void
{
$string = 'string';
/** @var LockedResource<Example\Resource> $locked */
$locked = $this->getLockedResource(new Example\Resource(''));
/** @var Locked<Example\Resource> $locked */
$locked = $this->getLockedResource(new Example\Resource());

$locked->value = $string;
self::assertSame($string, $locked->value);
Expand Down

0 comments on commit 529d30a

Please sign in to comment.