Skip to content

Commit

Permalink
Business interface examples (#283)
Browse files Browse the repository at this point in the history
* Business interface examples

* examples

* example

* examples
  • Loading branch information
dgafka authored Dec 29, 2023
1 parent 565f372 commit 35a8a2a
Show file tree
Hide file tree
Showing 20 changed files with 438 additions and 0 deletions.
37 changes: 37 additions & 0 deletions quickstart-examples/BusinessInterface/Asynchronous/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"type": "ecotone-quickstart",
"license": "MIT",
"authors": [
{
"name": "Dariusz Gafka",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"App\\BusinessInterface\\": "src"
}
},
"require": {
"ecotone/lite-dbal-starter": "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"wikimedia/composer-merge-plugin": "^2.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true,
"allow-plugins": {
"wikimedia/composer-merge-plugin": true
}
},
"extra": {
"merge-plugin": {
"include": [
"../../../packages/local_packages.json"
]
}
}
}
26 changes: 26 additions & 0 deletions quickstart-examples/BusinessInterface/Asynchronous/run_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use App\WorkingWithAggregateDirectly\Command\ChangePrice;
use App\WorkingWithAggregateDirectly\Command\RegisterProduct;
use App\WorkingWithAggregateDirectly\Product;
use App\WorkingWithAggregateDirectly\ProductRepository;
use App\WorkingWithAggregateDirectly\ProductService;
use Ecotone\Lite\EcotoneLiteApplication;
use Enqueue\Dbal\DbalConnectionFactory;
use PHPUnit\Framework\Assert;
use Ramsey\Uuid\Uuid;

require __DIR__ . "/vendor/autoload.php";
$messagingSystem = EcotoneLiteApplication::boostrap([DbalConnectionFactory::class => new DbalConnectionFactory(getenv('DATABASE_DSN') ? getenv('DATABASE_DSN') : 'pgsql://ecotone:secret@localhost:5432/ecotone')], pathToRootCatalog: __DIR__);

$cacheService = $messagingSystem->getGatewayByName(\App\BusinessInterface\CacheService::class);

$cacheService->set(new \App\BusinessInterface\CachedItem("pageViews", "12333", "data"));

Assert::assertNull($cacheService->get("pageViews"));

// running Message Consumer
$messagingSystem->run('async', \Ecotone\Messaging\Endpoint\ExecutionPollingMetadata::createWithTestingSetup());

Assert::assertEquals('12333', $cacheService->get('pageViews'));
echo "Cache set and get successfully\n";
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

use Ecotone\Messaging\Attribute\BusinessMethod;

interface CacheService
{
#[BusinessMethod('cache.set')]
public function set(CachedItem $item): void;

#[BusinessMethod('cache.get')]
public function get(string $key): ?string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

final readonly class CachedItem
{
public function __construct(
public string $key,
public string $value
)
{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

use Ecotone\Messaging\Attribute\Asynchronous;
use Ecotone\Messaging\Attribute\ServiceActivator;

final class InMemoryCache
{
private array $items;

#[Asynchronous("async")]
#[ServiceActivator('cache.set', endpointId: "cache.set.endpoint")]
public function set(CachedItem $item): void
{
$this->items[$item->key] = $item->value;
}

#[ServiceActivator('cache.get')]
public function get(string $key): ?string
{
return $this->items[$key] ?? null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

use Ecotone\Dbal\DbalBackedMessageChannelBuilder;
use Ecotone\Messaging\Attribute\ServiceContext;

final readonly class MessagingConfiguration
{
#[ServiceContext]
public function asyncChannel(): DbalBackedMessageChannelBuilder
{
return DbalBackedMessageChannelBuilder::create("async");
}
}
7 changes: 7 additions & 0 deletions quickstart-examples/BusinessInterface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Business Interface

This provides examples of how to use the business interface to interact with the system.

1. [ServiceActivator](ServiceActivator)
2. [Routing](Routing)
3. [Asynchronous](Asynchronous)
37 changes: 37 additions & 0 deletions quickstart-examples/BusinessInterface/Routing/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"type": "ecotone-quickstart",
"license": "MIT",
"authors": [
{
"name": "Dariusz Gafka",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"App\\BusinessInterface\\": "src"
}
},
"require": {
"ecotone/lite-dbal-starter": "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"wikimedia/composer-merge-plugin": "^2.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true,
"allow-plugins": {
"wikimedia/composer-merge-plugin": true
}
},
"extra": {
"merge-plugin": {
"include": [
"../../../packages/local_packages.json"
]
}
}
}
25 changes: 25 additions & 0 deletions quickstart-examples/BusinessInterface/Routing/run_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use App\WorkingWithAggregateDirectly\Command\ChangePrice;
use App\WorkingWithAggregateDirectly\Command\RegisterProduct;
use App\WorkingWithAggregateDirectly\Product;
use App\WorkingWithAggregateDirectly\ProductRepository;
use App\WorkingWithAggregateDirectly\ProductService;
use Ecotone\Lite\EcotoneLiteApplication;
use Enqueue\Dbal\DbalConnectionFactory;
use PHPUnit\Framework\Assert;
use Ramsey\Uuid\Uuid;

require __DIR__ . "/vendor/autoload.php";
$messagingSystem = EcotoneLiteApplication::boostrap([DbalConnectionFactory::class => new DbalConnectionFactory(getenv('DATABASE_DSN') ? getenv('DATABASE_DSN') : 'pgsql://ecotone:secret@localhost:5432/ecotone')], pathToRootCatalog: __DIR__);

$cacheService = $messagingSystem->getGatewayByName(\App\BusinessInterface\CacheService::class);

$cacheService->set(
new \App\BusinessInterface\CachedItem("pageViews", "12333", "data"),
\App\BusinessInterface\CacheType::IN_MEMORY
);

Assert::assertEquals("12333", $cacheService->get("pageViews", \App\BusinessInterface\CacheType::IN_MEMORY));
Assert::assertNull($cacheService->get("pageViews", \App\BusinessInterface\CacheType::FILE_SYSTEM));
echo "Cache set and get successfully\n";
17 changes: 17 additions & 0 deletions quickstart-examples/BusinessInterface/Routing/src/CacheService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

use Ecotone\Messaging\Attribute\BusinessMethod;
use Ecotone\Messaging\Attribute\Parameter\Header;

interface CacheService
{
#[BusinessMethod('cache.set')]
public function set(CachedItem $item, #[Header('cache.type')] CacheType $type): void;

#[BusinessMethod('cache.get')]
public function get(string $key, #[Header('cache.type')] CacheType $type): ?string;
}
11 changes: 11 additions & 0 deletions quickstart-examples/BusinessInterface/Routing/src/CacheType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

enum CacheType: string
{
case IN_MEMORY = 'in_memory';
case FILE_SYSTEM = 'file_system';
}
14 changes: 14 additions & 0 deletions quickstart-examples/BusinessInterface/Routing/src/CachedItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

final readonly class CachedItem
{
public function __construct(
public string $key,
public string $value,
)
{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface;

use Ecotone\Messaging\Attribute\Parameter\Header;
use Ecotone\Messaging\Attribute\Router;

final readonly class CachingRouter
{
#[Router('cache.set')]
public function routeSet(#[Header('cache.type')] CacheType $type): string
{
return match ($type) {
CacheType::FILE_SYSTEM => 'cache.set.file_system',
CacheType::IN_MEMORY => 'cache.set.in_memory',
};
}

#[Router('cache.get')]
public function routeGet(#[Header('cache.type')] CacheType $type): string
{
return match ($type) {
CacheType::FILE_SYSTEM => 'cache.get.file_system',
CacheType::IN_MEMORY => 'cache.get.in_memory',
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface\FileSystem;

use App\BusinessInterface\CachedItem;
use Ecotone\Messaging\Attribute\ServiceActivator;

final readonly class FileSystemCache
{
#[ServiceActivator('cache.set.file_system')]
public function set(CachedItem $item): void
{
file_put_contents('/tmp/' . $item->key, $item->value);
}

#[ServiceActivator('cache.get.file_system')]
public function get(string $key): ?string
{
$data = @file_get_contents('/tmp/' . $key);

return $data === false ? null : $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\BusinessInterface\InMemory;

use App\BusinessInterface\CachedItem;
use Ecotone\Messaging\Attribute\ServiceActivator;

final class InMemoryCache
{
private array $items;

#[ServiceActivator('cache.set.in_memory')]
public function set(CachedItem $item): void
{
$this->items[$item->key] = $item->value;
}

#[ServiceActivator('cache.get.in_memory')]
public function get(string $key): ?string
{
return $this->items[$key] ?? null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"type": "ecotone-quickstart",
"license": "MIT",
"authors": [
{
"name": "Dariusz Gafka",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"App\\BusinessInterface\\": "src"
}
},
"require": {
"ecotone/lite-dbal-starter": "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"wikimedia/composer-merge-plugin": "^2.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true,
"allow-plugins": {
"wikimedia/composer-merge-plugin": true
}
},
"extra": {
"merge-plugin": {
"include": [
"../../../packages/local_packages.json"
]
}
}
}
Loading

0 comments on commit 35a8a2a

Please sign in to comment.