Skip to content

Commit

Permalink
more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Sep 12, 2024
1 parent 083efc7 commit 7ef1993
Show file tree
Hide file tree
Showing 18 changed files with 407 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ To get the diff between two versions, go to https://github.com/SonsOfPHP/sonsofp

## [Unreleased]

* [PR #226](https://github.com/SonsOfPHP/sonsofphp/pull/226) [Registry] New Component and Contract
* [PR #225](https://github.com/SonsOfPHP/sonsofphp/pull/225) Maintenance
* [PR #222](https://github.com/SonsOfPHP/sonsofphp/pull/222) [Assert] New Component
* [PR #221](https://github.com/SonsOfPHP/sonsofphp/pull/221) [StateMachine] New Contract and Component
Expand Down
8 changes: 8 additions & 0 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 12 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"psr/http-server-middleware-implementation": "^1.0",
"sonsofphp/http-handler-implementation": "0.3.x-dev",
"sonsofphp/mailer-implementation": "0.3.x-dev",
"sonsofphp/state-machine-implementation": "0.3.x-dev"
"sonsofphp/state-machine-implementation": "0.3.x-dev",
"sonsofphp/registry-implementation": "0.3.x-dev"
},
"require": {
"php": ">=8.2",
Expand Down Expand Up @@ -87,7 +88,8 @@
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"aws/aws-sdk-php": "^3.0",
"liip/imagine-bundle": "^2.0"
"liip/imagine-bundle": "^2.0",
"sonsofphp/registry-contract": "0.3.x-dev"
},
"replace": {
"sonsofphp/bard": "self.version",
Expand Down Expand Up @@ -135,7 +137,9 @@
"sonsofphp/filesystem-liip-imagine": "self.version",
"sonsofphp/state-machine": "self.version",
"sonsofphp/state-machine-contract": "self.version",
"sonsofphp/assert": "self.version"
"sonsofphp/assert": "self.version",
"sonsofphp/registry": "self.version",
"sonsofphp/registry-contract": "self.version"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -183,6 +187,8 @@
"SonsOfPHP\\Contract\\Logger\\": "src/SonsOfPHP/Contract/Logger",
"SonsOfPHP\\Contract\\Money\\": "src/SonsOfPHP/Contract/Money",
"SonsOfPHP\\Contract\\Pager\\": "src/SonsOfPHP/Contract/Pager",
"SonsOfPHP\\Component\\Registry\\": "src/SonsOfPHP/Component/Registry",
"SonsOfPHP\\Contract\\Registry\\": "src/SonsOfPHP/Contract/Registry",
"SonsOfPHP\\Contract\\Version\\": "src/SonsOfPHP/Contract/Version"
},
"exclude-from-classmap": [
Expand Down Expand Up @@ -217,7 +223,8 @@
"src/SonsOfPHP/Bridge/Doctrine/Collections/Pager/Tests",
"src/SonsOfPHP/Bridge/Doctrine/DBAL/Pager/Tests",
"src/SonsOfPHP/Bridge/Doctrine/ORM/Pager/Tests",
"src/SonsOfPHP/Component/Version/Tests"
"src/SonsOfPHP/Component/Version/Tests",
"src/SonsOfPHP/Component/Registry/Tests"
]
},
"extra": {
Expand All @@ -240,4 +247,4 @@
"SonsOfPHP\\Bridge\\Symfony\\Cqrs\\Tests\\": "src/SonsOfPHP/Bridge/Symfony/Cqrs/Tests"
}
}
}
}
2 changes: 2 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* [Queries](components/money/queries.md)
* [Pager](components/pager/README.md)
* [Adapters](components/pager/adapters.md)
* [Registry](components/registry.md)
* [State Machine](components/state-machine.md)
* [Version](components/version.md)

Expand All @@ -63,6 +64,7 @@
* [Filesystem](contracts/filesystem.md)
* [Mailer](contracts/mailer.md)
* [Pager](contracts/pager.md)
* [Registry](contracts/registry.md)
* [State Machine](contracts/state-machine.md)

## 💁 Contributing
Expand Down
32 changes: 32 additions & 0 deletions docs/components/registry.md
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) {
// ...
}
```
9 changes: 9 additions & 0 deletions docs/contracts/registry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Registry Contract
---

## Installation

```shell
composer require sonsofphp/registry-contract
```
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Component/Registry/.gitattributes
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
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Component/Registry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
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 {}
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 {}
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Component/Registry/LICENSE
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.
52 changes: 52 additions & 0 deletions src/SonsOfPHP/Component/Registry/README.md
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
77 changes: 77 additions & 0 deletions src/SonsOfPHP/Component/Registry/ServiceRegistry.php
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];
}
}
Loading

0 comments on commit 7ef1993

Please sign in to comment.