Skip to content

Commit

Permalink
Merge branch '3.x' into feature/ssl-conection-support
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
#	src/Bootloader/TemporalBridgeBootloader.php
  • Loading branch information
roxblnfk committed Aug 28, 2024
2 parents 6a72992 + 6251b2e commit c97bbc3
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 180 deletions.
20 changes: 10 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@
}
],
"require": {
"php": ">=8.1",
"spiral/boot": "^3.0",
"spiral/attributes": "^2.8 || ^3.0",
"spiral/tokenizer": "^3.0",
"spiral/scaffolder": "^3.0",
"spiral/roadrunner-bridge": "^2.0 || ^3.0",
"temporal/sdk": "^2.7"
"php": "^8.1",
"spiral/boot": "^3.13",
"spiral/attributes": "^2.8 || ^3.1.5",
"spiral/tokenizer": "^3.13",
"spiral/scaffolder": "^3.13",
"spiral/roadrunner-bridge": "^2.0 || ^3.6",
"temporal/sdk": "^2.10"
},
"require-dev": {
"spiral/framework": "^3.0",
"spiral/testing": "^2.6",
"vimeo/psalm": "^5.17"
"spiral/framework": "^3.13",
"spiral/testing": "^2.8",
"vimeo/psalm": "^5.23"
},
"autoload": {
"psr-4": {
Expand Down
45 changes: 26 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false"
backupStaticAttributes="false" colors="true" verbose="false" convertErrorsToExceptions="true"
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
stopOnFailure="false" stopOnError="false" stderr="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>tests/src</directory>
</testsuite>
</testsuites>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
stopOnError="false"
stderr="false"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Test Suite">
<directory>tests/src</directory>
</testsuite>
</testsuites>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
</php>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
7 changes: 7 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MissingClassConstType errorLevel="suppress" />
<DeprecatedInterface>
<errorLevel type="suppress">
<file name="src/Commands/InfoCommand.php"/>
<file name="src/DeclarationLocator.php"/>
</errorLevel>
</DeprecatedInterface>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<file name="src/Commands/Scaffolder/ActivityCommand.php"/>
Expand Down
113 changes: 43 additions & 70 deletions src/Bootloader/TemporalBridgeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
use Spiral\TemporalBridge\Connection\SslConnection;
use Spiral\TemporalBridge\DeclarationLocator;
use Spiral\TemporalBridge\DeclarationLocatorInterface;
use Spiral\TemporalBridge\DeclarationRegistryInterface;
use Spiral\TemporalBridge\Dispatcher;
use Spiral\TemporalBridge\WorkerFactory;
use Spiral\TemporalBridge\WorkerFactoryInterface;
use Spiral\TemporalBridge\WorkersRegistry;
use Spiral\TemporalBridge\WorkersRegistryInterface;
use Spiral\Tokenizer\ClassesInterface;
use Spiral\Tokenizer\TokenizerListenerRegistryInterface;
use Temporal\Client\GRPC\ServiceClient;
use Temporal\Client\WorkflowClient;
use Temporal\Client\WorkflowClientInterface;
Expand Down Expand Up @@ -58,13 +59,43 @@ public function defineDependencies(): array
public function defineSingletons(): array
{
return [
TemporalWorkerFactoryInterface::class => [self::class, 'initWorkerFactory'],
TemporalWorkerFactoryInterface::class => static fn(
DataConverterInterface $dataConverter,
): TemporalWorkerFactoryInterface => new TemporalWorkerFactory(
dataConverter: $dataConverter,
rpc: Goridge::create(),
),
WorkerFactoryInterface::class => WorkerFactory::class,
DeclarationLocatorInterface::class => [self::class, 'initDeclarationLocator'],
WorkflowClientInterface::class => [self::class, 'initWorkflowClient'],
DeclarationLocator::class => static fn (): DeclarationLocator => new DeclarationLocator(
reader: new AttributeReader(),
),
DeclarationLocatorInterface::class => DeclarationLocator::class,
DeclarationRegistryInterface::class => DeclarationLocator::class,

WorkflowClientInterface::class => static fn(
TemporalConfig $config,
DataConverterInterface $dataConverter,
PipelineProvider $pipelineProvider,
ServiceClientInterface $serviceClient,
): WorkflowClientInterface => new WorkflowClient(
serviceClient: $serviceClient,
options: $config->getClientOptions(),
converter: $dataConverter,
interceptorProvider: $pipelineProvider,
),
WorkersRegistryInterface::class => WorkersRegistry::class,
ScheduleClientInterface::class => [self::class, 'initScheduleClient'],
DataConverterInterface::class => [self::class, 'initDataConverter'],

ScheduleClientInterface::class => static fn(
TemporalConfig $config,
DataConverterInterface $dataConverter,
ServiceClientInterface $serviceClient,
): ScheduleClientInterface => new ScheduleClient(
serviceClient: $serviceClient,
options: $config->getClientOptions(),
converter: $dataConverter,
),

DataConverterInterface::class => static fn() => DataConverter::createDefault(),
PipelineProvider::class => [self::class, 'initPipelineProvider'],
ServiceClientInterface::class => [self::class, 'initServiceClient'],
];
Expand All @@ -80,9 +111,11 @@ public function init(
AbstractKernel $kernel,
EnvironmentInterface $env,
ConsoleBootloader $console,
TokenizerListenerRegistryInterface $tokenizer,
DeclarationLocator $locator,
): void {
$this->initConfig($env);

$tokenizer->addListener($locator);
$console->addCommand(Commands\InfoCommand::class);
$kernel->addDispatcher($this->factory->make(Dispatcher::class));
}
Expand Down Expand Up @@ -123,6 +156,8 @@ protected function initConfig(EnvironmentInterface $env): void
$this->config->setDefaults(
TemporalConfig::CONFIG,
[
// 'address' => $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'),
// 'namespace' => 'App\\Endpoint\\Temporal\\Workflow',
'connection' => $env->get('TEMPORAL_CONNECTION', 'default'),
'connections' => [
'default' => new Connection(address: $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233')),
Expand All @@ -137,64 +172,14 @@ protected function initConfig(EnvironmentInterface $env): void
);
}

protected function initWorkflowClient(
TemporalConfig $config,
DataConverterInterface $dataConverter,
PipelineProvider $pipelineProvider,
ServiceClientInterface $serviceClient,
): WorkflowClientInterface {
return new WorkflowClient(
serviceClient: $serviceClient,
options: $config->getClientOptions(),
converter: $dataConverter,
interceptorProvider: $pipelineProvider,
);
}

protected function initDataConverter(): DataConverterInterface
{
return DataConverter::createDefault();
}

protected function initWorkerFactory(DataConverterInterface $dataConverter,): TemporalWorkerFactoryInterface
{
return new TemporalWorkerFactory(
dataConverter: $dataConverter,
rpc: Goridge::create(),
);
}

protected function initDeclarationLocator(ClassesInterface $classes,): DeclarationLocatorInterface
{
return new DeclarationLocator(
classes: $classes,
reader: new AttributeReader(),
);
}

protected function initPipelineProvider(TemporalConfig $config, FactoryInterface $factory): PipelineProvider
{
/** @var Interceptor[] $interceptors */
$interceptors = \array_map(
static fn(mixed $interceptor) => match (true) {
\is_string($interceptor) => $factory->make($interceptor),
$interceptor instanceof Autowire => $interceptor->resolve($factory),
default => $interceptor
},
$config->getInterceptors(),
);

return new SimplePipelineProvider($interceptors);
}

protected function initServiceClient(TemporalConfig $config): ServiceClientInterface
{
$connection = $config->getConnection($config->getDefaultConnection());

if ($connection instanceof SslConnection) {
return ServiceClient::createSSL(
address: $connection->address,
crt: (string) $connection->crt,
crt: $connection->crt,
clientKey: $connection->clientKey,
clientPem: $connection->clientPem,
overrideServerName: $connection->overrideServerName,
Expand All @@ -203,16 +188,4 @@ protected function initServiceClient(TemporalConfig $config): ServiceClientInter

return ServiceClient::create(address: $connection->address);
}

protected function initScheduleClient(
TemporalConfig $config,
DataConverterInterface $dataConverter,
ServiceClientInterface $serviceClient,
): ScheduleClientInterface {
return new ScheduleClient(
serviceClient: $serviceClient,
options: $config->getClientOptions(),
converter: $dataConverter,
);
}
}
47 changes: 33 additions & 14 deletions src/Commands/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Attribute\Option;
use Spiral\Console\Command;
use Spiral\TemporalBridge\DeclarationLocatorInterface;
use Spiral\TemporalBridge\Declaration\DeclarationType;
use Spiral\TemporalBridge\DeclarationRegistryInterface;
use Spiral\TemporalBridge\DeclarationWorkerResolver;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Output\OutputInterface;
use Temporal\Internal\Declaration\Reader\ActivityReader;
use Temporal\Internal\Declaration\Reader\WorkflowReader;
use Temporal\Workflow\WorkflowInterface;

#[AsCommand(
name: 'temporal:info',
Expand All @@ -26,7 +26,7 @@ final class InfoCommand extends Command
private bool $showActivities = false;

public function perform(
DeclarationLocatorInterface $locator,
DeclarationRegistryInterface $registry,
DeclarationWorkerResolver $workerResolver,
WorkflowReader $workflowReader,
ActivityReader $activityReader,
Expand All @@ -35,25 +35,27 @@ public function perform(
$workflows = [];
$activities = [];

foreach ($locator->getDeclarations() as $type => $declaration) {
$taskQueue = $workerResolver->resolve($declaration);
foreach ($registry->getDeclarationList() as $declaration) {
$taskQueue = $declaration->taskQueue === null
? $workerResolver->resolve($declaration->class)
: [$declaration->taskQueue];

if ($type === WorkflowInterface::class) {
$prototype = $workflowReader->fromClass($declaration->getName());
if ($declaration->type === DeclarationType::Workflow) {
$prototype = $workflowReader->fromClass($declaration->class->getName());
$workflows[$prototype->getID()] = [
'class' => $declaration->getName(),
'file' => $declaration->getFileName(),
'class' => $declaration->class->getName(),
'file' => $declaration->class->getFileName(),
'name' => $prototype->getID(),
'task_queue' => \implode(', ', $taskQueue),
];
} else {
$taskQueueShown = false;

foreach ($activityReader->fromClass($declaration->getName()) as $prototype) {
$activities[$declaration->getName()][$prototype->getID()] = [
'file' => $declaration->getFileName(),
foreach ($activityReader->fromClass($declaration->class->getName()) as $prototype) {
$activities[$declaration->class->getName()][$prototype->getID()] = [
'file' => $declaration->class->getFileName(),
'name' => $prototype->getID(),
'handler' => $declaration->getShortName() . '::' . $prototype->getHandler()->getName(),
'handler' => $declaration->class->getShortName() . '::' . $prototype->getHandler()->getName(),
'task_queue' => !$taskQueueShown ? \implode(', ', $taskQueue) : '',
];

Expand All @@ -72,7 +74,10 @@ public function perform(
foreach ($workflows as $workflow) {
$table->addRow([
\sprintf('<fg=green>%s</>', $workflow['name']),
$workflow['class'] . "\n" . \sprintf('<fg=blue>%s</>', \str_replace($rootDir, '', $workflow['file'])),
$workflow['class'] . "\n" . \sprintf(
'<fg=blue>%s</>',
self::normalizePath($rootDir, $workflow['file']),
),
$workflow['task_queue'],
]);
}
Expand Down Expand Up @@ -100,4 +105,18 @@ public function perform(

return self::SUCCESS;
}

/**
* @param non-empty-string $rootDir
* @param non-empty-string $file
*/
private static function normalizePath(string $rootDir, string $file): string
{
$file = \str_replace('\\', '/', $file);
$rootDir = \str_replace('\\', '/', $rootDir);

return \str_starts_with($file, $rootDir)
? \substr($file, \strlen($rootDir))
: $file;
}
}
1 change: 1 addition & 0 deletions src/Config/TemporalConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
final class TemporalConfig extends InjectableConfig
{
/** @var non-empty-string */
public const CONFIG = 'temporal';

protected array $config = [
Expand Down
15 changes: 15 additions & 0 deletions src/Declaration/DeclarationDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Spiral\TemporalBridge\Declaration;

final class DeclarationDto
{
public function __construct(
public readonly DeclarationType $type,
public readonly \ReflectionClass $class,
public readonly ?string $taskQueue = null,
) {
}
}
11 changes: 11 additions & 0 deletions src/Declaration/DeclarationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Spiral\TemporalBridge\Declaration;

enum DeclarationType: string
{
case Workflow = 'workflow';
case Activity = 'activity';
}
Loading

0 comments on commit c97bbc3

Please sign in to comment.