-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added serialization helper and restructured tests
- Loading branch information
Showing
44 changed files
with
486 additions
and
59 deletions.
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
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,24 @@ | ||
<?php | ||
|
||
namespace Aternos\Taskmaster\Communication\Serialization; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Attribute NotSerializable | ||
* | ||
* This attribute is used to mark properties that should not be serialized. | ||
* When using only the #[NotSerializable] attribute, all properties that are not marked with the | ||
* #[NotSerializable] attribute will be serialized. | ||
* | ||
* When using both attributes, all properties MUST be marked with either the #[Serializable] or #[NotSerializable]. | ||
* | ||
* You can use the {@link SerializationTrait} to implement serialization with these attributes. | ||
* | ||
* @package Aternos\Taskmaster\Communication\Serialization | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class NotSerializable | ||
{ | ||
|
||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace Aternos\Taskmaster\Communication\Serialization; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Attribute Serializable | ||
* | ||
* This attribute is used to mark properties that should be serialized. | ||
* When using only the #[Serializable] attribute, all properties that are not marked with the | ||
* #[Serializable] attribute will be ignored. | ||
* | ||
* When using both attributes, all properties MUST be marked with either the #[Serializable] or #[NotSerializable]. | ||
* | ||
* You can use the {@link SerializationTrait} to implement serialization with these attributes. | ||
* | ||
* @package Aternos\Taskmaster\Communication\Serialization | ||
*/ | ||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class Serializable | ||
{ | ||
|
||
} |
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,73 @@ | ||
<?php | ||
|
||
namespace Aternos\Taskmaster\Communication\Serialization; | ||
|
||
use ReflectionClass; | ||
|
||
/** | ||
* Trait SerializationTrait | ||
* | ||
* This trait is used to serialize objects using the #[Serializable] and #[NotSerializable] attributes. | ||
* | ||
* You can use the #[Serializable] attribute to mark properties that should be serialized. | ||
* When using only the #[Serializable] attribute, all properties that are not marked with the | ||
* #[Serializable] attribute will be ignored. | ||
* | ||
* You can use the #[NotSerializable] attribute to mark properties that should not be serialized. | ||
* When using only the #[NotSerializable] attribute, all properties that are not marked with the | ||
* #[NotSerializable] attribute will be serialized. | ||
* | ||
* When using both attributes, all properties MUST be marked with either the #[Serializable] or #[NotSerializable] | ||
* attribute, otherwise an exception will be thrown. | ||
* | ||
* @package Aternos\Taskmaster\Communication\Serialization | ||
*/ | ||
trait SerializationTrait | ||
{ | ||
public function __serialize(): array | ||
{ | ||
$unknown = []; | ||
$serializable = []; | ||
$hasNotSerializable = false; | ||
$hasSerializable = false; | ||
$hasUnknown = false; | ||
foreach ((new ReflectionClass($this))->getProperties() as $property) { | ||
if ($property->isStatic()) { | ||
continue; | ||
} | ||
|
||
if ($property->getAttributes(NotSerializable::class)) { | ||
$hasNotSerializable = true; | ||
continue; | ||
} | ||
|
||
if ($property->getAttributes(Serializable::class)) { | ||
$hasSerializable = true; | ||
if ($property->isInitialized($this)) { | ||
$serializable[$property->getName()] = $property->getValue($this); | ||
} | ||
continue; | ||
} | ||
|
||
$hasUnknown = true; | ||
if ($property->isInitialized($this)) { | ||
$unknown[$property->getName()] = $property->getValue($this); | ||
} | ||
} | ||
|
||
if ($hasNotSerializable) { | ||
if (!$hasSerializable) { | ||
return $unknown; | ||
} | ||
if ($hasUnknown) { | ||
throw new \LogicException("Found unknown properties (" . implode(", ", array_keys($unknown)) . ") on object using both, #[Serializable] and #[NotSerializable] attributes."); | ||
} | ||
return $serializable; | ||
} | ||
|
||
if ($hasSerializable) { | ||
return $serializable; | ||
} | ||
return $unknown; | ||
} | ||
} |
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
9 changes: 4 additions & 5 deletions
9
...vironment/ExitableAsyncWorkerTestCase.php → ...tegration/ExitableAsyncWorkerTestCase.php
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
2 changes: 1 addition & 1 deletion
2
test/Environment/ForkWorkerTest.php → test/Integration/ForkWorkerTest.php
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
2 changes: 1 addition & 1 deletion
2
test/Environment/ProcessWorkerTest.php → test/Integration/ProcessWorkerTest.php
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
2 changes: 1 addition & 1 deletion
2
test/Environment/ProxiedForkWorkerTest.php → test/Integration/ProxiedForkWorkerTest.php
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
2 changes: 1 addition & 1 deletion
2
.../Environment/ProxiedProcessWorkerTest.php → .../Integration/ProxiedProcessWorkerTest.php
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
2 changes: 1 addition & 1 deletion
2
test/Environment/ProxiedThreadWorkerTest.php → test/Integration/ProxiedThreadWorkerTest.php
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
2 changes: 1 addition & 1 deletion
2
test/Environment/SyncWorkerTest.php → test/Integration/SyncWorkerTest.php
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
2 changes: 1 addition & 1 deletion
2
test/Environment/ThreadWorkerTest.php → test/Integration/ThreadWorkerTest.php
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
Oops, something went wrong.