-
-
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.
- Loading branch information
1 parent
083efc7
commit 7ef1993
Showing
18 changed files
with
407 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,14 @@ | |
"path": "src/SonsOfPHP/Contract/Pager", | ||
"repository": "[email protected]:SonsOfPHP/pager-contract.git" | ||
}, | ||
{ | ||
"path": "src/SonsOfPHP/Component/Registry", | ||
"repository": "[email protected]:SonsOfPHP/registry.git" | ||
}, | ||
{ | ||
"path": "src/SonsOfPHP/Contract/Registry", | ||
"repository": "[email protected]:SonsOfPHP/registry-contract.git" | ||
}, | ||
{ | ||
"path": "src/SonsOfPHP/Contract/Version", | ||
"repository": "[email protected]:SonsOfPHP/version-contract.git" | ||
|
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,32 @@ | ||
--- | ||
title: Registry | ||
--- | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require sonsofphp/registry | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\Registry\ServiceRegistry; | ||
|
||
$registry = new ServiceRegistry($interfaceClassName); | ||
|
||
$registry->register('service.id', $service); | ||
$service = $registry->get('service.id'); | ||
|
||
if ($registry->has('service.id')) { | ||
// ... | ||
} | ||
|
||
$registry->unregister('service.id'); | ||
|
||
foreach ($registry->all() as $identifier => $service) { | ||
// ... | ||
} | ||
``` |
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 @@ | ||
--- | ||
title: Registry Contract | ||
--- | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require sonsofphp/registry-contract | ||
``` |
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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,3 @@ | ||
composer.lock | ||
phpunit.xml | ||
vendor/ |
12 changes: 12 additions & 0 deletions
12
src/SonsOfPHP/Component/Registry/Exception/ExistingServiceException.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Registry\Exception; | ||
|
||
use SonsOfPHP\Contract\Registry\ExistingServiceExceptionInterface; | ||
|
||
/** | ||
* @author Joshua Estes <[email protected]> | ||
*/ | ||
class ExistingServiceException extends \Exception implements ExistingServiceExceptionInterface {} |
12 changes: 12 additions & 0 deletions
12
src/SonsOfPHP/Component/Registry/Exception/NonExistingServiceException.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Registry\Exception; | ||
|
||
use SonsOfPHP\Contract\Registry\NonExistingServiceExceptionInterface; | ||
|
||
/** | ||
* @author Joshua Estes <[email protected]> | ||
*/ | ||
class NonExistingServiceException extends \Exception implements NonExistingServiceExceptionInterface {} |
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,19 @@ | ||
Copyright 2022 to Present Joshua Estes | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,52 @@ | ||
Sons of PHP - State Machine | ||
=========================== | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\StateMachine\StateMachine; | ||
|
||
$sm = new StateMachine([ | ||
'graph' => 'order', | ||
'supports' => [ | ||
OrderInterface::class, | ||
], | ||
'transitions' => [ | ||
'create' => [ | ||
'from' => 'draft', | ||
'to' => 'new', | ||
], | ||
'fulfill' => [ | ||
'from' => 'new', | ||
'to' => 'fulfilled', | ||
], | ||
'cancel' => [ | ||
'from' => ['draft', 'new', 'fulfilled'], | ||
'to' => 'fulfilled', | ||
], | ||
], | ||
]); | ||
|
||
// Check if state can change | ||
$sm->can($order, 'create'); | ||
|
||
// Apply transition | ||
$sm->apply($order, 'fulfil'); | ||
|
||
// Get Current State | ||
$sm->getState($order); | ||
``` | ||
|
||
## Learn More | ||
|
||
* [Documentation][docs] | ||
* [Contributing][contributing] | ||
* [Report Issues][issues] and [Submit Pull Requests][pull-requests] in the [Mother Repository][mother-repo] | ||
* Get Help & Support using [Discussions][discussions] | ||
|
||
[discussions]: https://github.com/orgs/SonsOfPHP/discussions | ||
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp | ||
[contributing]: https://docs.sonsofphp.com/contributing/ | ||
[docs]: https://docs.sonsofphp.com/components/state-machine/ | ||
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3AStateMachine | ||
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3AStateMachine |
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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Registry; | ||
|
||
use SonsOfPHP\Contract\Registry\ServiceRegistryInterface; | ||
use SonsOfPHP\Component\Registry\Exception\ExistingServiceException; | ||
use SonsOfPHP\Component\Registry\Exception\NonExistingServiceException; | ||
|
||
/** | ||
* @author Joshua Estes <[email protected]> | ||
*/ | ||
class ServiceRegistry implements ServiceRegistryInterface | ||
{ | ||
private array $services = []; | ||
|
||
public function __construct( | ||
private string $interface, | ||
) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function all(): iterable | ||
{ | ||
return $this->services; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function register(string $identifier, object $service): void | ||
{ | ||
if ($this->has($identifier)) { | ||
throw new ExistingServiceException(); | ||
} | ||
|
||
if (!$service instanceof $this->interface) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
$this->services[$identifier] = $service; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unregister(string $identifier): void | ||
{ | ||
if (!$this->has($identifier)) { | ||
throw new NonExistingServiceException(); | ||
} | ||
|
||
unset($this->services[$identifier]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has(string $identifier): bool | ||
{ | ||
return array_key_exists($identifier, $this->services); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(string $identifier): object | ||
{ | ||
if (!$this->has($identifier)) { | ||
throw new NonExistingServiceException(); | ||
} | ||
|
||
return $this->services[$identifier]; | ||
} | ||
} |
Oops, something went wrong.