-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented generic
LockedResource
object
- Loading branch information
Showing
12 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
use PetrKnap\Shorts\Exception\CouldNotProcessData; | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @extends CouldNotProcessData<T> | ||
*/ | ||
final class CouldNotGetUnlockedResource extends CouldNotProcessData implements LockedResourceException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
use Throwable; | ||
|
||
interface LockableResourceException extends Throwable | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
interface LockedResourceException extends LockableResourceException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use Symfony\Component\Lock\LockInterface; | ||
|
||
final class LockableResource | ||
{ | ||
/** | ||
* @template T of mixed | ||
* | ||
* @param T $resource | ||
* | ||
* @return LockedResource<T> | ||
*/ | ||
public static function create( | ||
mixed $resource, | ||
LockInterface $lock1, | ||
LockInterface ...$lockN, | ||
): LockedResource { | ||
return new Symfony\Lock\LockedResource($resource, $lock1, ...$lockN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @mixin T | ||
*/ | ||
abstract class LockedResource | ||
{ | ||
/** | ||
* @param T $resource | ||
*/ | ||
public function __construct( | ||
private readonly mixed $resource, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<mixed> $arguments | ||
*/ | ||
public function __call(string $name, array $arguments): mixed | ||
{ | ||
return $this->get()->$name(...$arguments); | ||
} | ||
|
||
public function __set(string $name, mixed $value): void | ||
{ | ||
$this->get()->$name = $value; | ||
} | ||
|
||
public function __get(string $name): mixed | ||
{ | ||
return $this->get()->$name; | ||
} | ||
|
||
/** | ||
* @return T | ||
* | ||
* @throws Exception\CouldNotGetUnlockedResource<T> | ||
*/ | ||
public function get(): mixed | ||
{ | ||
if (!$this->isLocked()) { | ||
throw new Exception\CouldNotGetUnlockedResource( | ||
__METHOD__, | ||
$this->resource, | ||
); | ||
} | ||
return $this->resource; | ||
} | ||
|
||
abstract protected function isLocked(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Symfony\Lock; | ||
|
||
use PetrKnap\CriticalSection\LockedResource as Base; | ||
use Symfony\Component\Lock\LockInterface; | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @extends Base<T> | ||
*/ | ||
final class LockedResource extends Base | ||
{ | ||
/** | ||
* @var array<LockInterface> | ||
*/ | ||
private readonly array $locks; | ||
|
||
/** | ||
* @param T $resource | ||
*/ | ||
public function __construct( | ||
mixed $resource, | ||
LockInterface $lock1, | ||
LockInterface ...$lockN, | ||
) { | ||
parent::__construct($resource); | ||
$this->locks = [$lock1, ...$lockN]; | ||
} | ||
|
||
protected function isLocked(): bool | ||
{ | ||
foreach ($this->locks as $lock) { | ||
if (!$lock->isAcquired()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\LockedResource; | ||
|
||
final class Resource | ||
{ | ||
public function __construct( | ||
public string $value, | ||
) { | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
abstract class LockedResourceTestCase extends TestCase | ||
{ | ||
public function testCouldNotGetUnlockedResource(): void | ||
{ | ||
self::expectException(Exception\CouldNotGetUnlockedResource::class); | ||
|
||
$this->getUnlockedResource(null)->get(); | ||
} | ||
|
||
public function testCouldGetLockedResource(): void | ||
{ | ||
$instance = new stdClass(); | ||
|
||
self::assertSame( | ||
$instance, | ||
$this->getLockedResource($instance)->get(), | ||
); | ||
} | ||
|
||
public function testIsMixin(): void | ||
{ | ||
$string = 'string'; | ||
/** @var LockedResource<LockedResource\Resource> $locked */ | ||
$locked = $this->getLockedResource(new LockedResource\Resource('')); | ||
|
||
$locked->value = $string; | ||
self::assertSame($string, $locked->value); | ||
self::assertSame($string, $locked->getValue()); | ||
} | ||
|
||
abstract protected function getUnlockedResource(mixed $resource): LockedResource; | ||
abstract protected function getLockedResource(mixed $resource): LockedResource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Symfony\Lock; | ||
|
||
use PetrKnap\CriticalSection\LockedResourceTestCase; | ||
use Symfony\Component\Lock\LockInterface; | ||
|
||
final class LockedResourceTest extends LockedResourceTestCase | ||
{ | ||
protected function getUnlockedResource(mixed $resource): LockedResource | ||
{ | ||
$lock = $this->createMock(LockInterface::class); | ||
$lock->expects(self::any())->method('isAcquired')->willReturn(false); | ||
return new LockedResource($resource, $lock); | ||
} | ||
|
||
protected function getLockedResource(mixed $resource): LockedResource | ||
{ | ||
$lock = $this->createMock(LockInterface::class); | ||
$lock->expects(self::any())->method('isAcquired')->willReturn(true); | ||
return new LockedResource($resource, $lock); | ||
} | ||
} |