Skip to content

Commit

Permalink
refactor!: Move protobuf code to core
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Jan 7, 2024
1 parent 5b5bfb4 commit be1a80f
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/PhpPact/Plugins/Protobuf/Driver/Pact/ProtobufPactDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace PhpPact\Plugins\Protobuf\Driver\Pact;

use PhpPact\Plugin\Driver\Pact\AbstractPluginPactDriver;

class ProtobufPactDriver extends AbstractPluginPactDriver
{
protected function getPluginName(): string
{
return 'protobuf';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PhpPact\Plugins\Protobuf\Factory;

use PhpPact\Config\PactConfigInterface;
use PhpPact\Consumer\Driver\Interaction\MessageDriver;
use PhpPact\Consumer\Driver\Interaction\MessageDriverInterface;
use PhpPact\Consumer\Factory\MessageDriverFactoryInterface;
use PhpPact\Consumer\Registry\Pact\PactRegistry;
use PhpPact\FFI\Client;
use PhpPact\Plugins\Protobuf\Driver\Pact\ProtobufPactDriver;
use PhpPact\Plugins\Protobuf\Registry\Interaction\ProtobufMessageRegistry;

class ProtobufMessageDriverFactory implements MessageDriverFactoryInterface
{
public function create(PactConfigInterface $config): MessageDriverInterface
{
$client = new Client();
$pactRegistry = new PactRegistry($client);
$pactDriver = new ProtobufPactDriver($client, $config, $pactRegistry);
$messageRegistry = new ProtobufMessageRegistry($client, $pactRegistry);

return new MessageDriver($client, $pactDriver, $messageRegistry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PhpPact\Plugins\Protobuf\Factory;

use PhpPact\Consumer\Registry\Pact\PactRegistry;
use PhpPact\FFI\Client;
use PhpPact\Plugins\Protobuf\Driver\Pact\ProtobufPactDriver;
use PhpPact\Plugins\Protobuf\Registry\Interaction\ProtobufSyncMessageRegistry;
use PhpPact\Plugins\Protobuf\Service\GrpcMockServer;
use PhpPact\Standalone\MockService\MockServerConfigInterface;
use PhpPact\SyncMessage\Driver\Interaction\SyncMessageDriver;
use PhpPact\SyncMessage\Driver\Interaction\SyncMessageDriverInterface;
use PhpPact\SyncMessage\Factory\SyncMessageDriverFactoryInterface;

class ProtobufSyncMessageDriverFactory implements SyncMessageDriverFactoryInterface
{
public function create(MockServerConfigInterface $config): SyncMessageDriverInterface
{
$client = new Client();
$pactRegistry = new PactRegistry($client);
$pactDriver = new ProtobufPactDriver($client, $config, $pactRegistry);
$grpcMockServer = new GrpcMockServer($client, $pactRegistry, $config);
$syncMessageRegistry = new ProtobufSyncMessageRegistry($client, $pactRegistry);

return new SyncMessageDriver($pactDriver, $syncMessageRegistry, $grpcMockServer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PhpPact\Plugins\Protobuf\Registry\Interaction\Body;

use PhpPact\Consumer\Registry\Interaction\MessageRegistryInterface;
use PhpPact\Consumer\Registry\Interaction\Part\RequestPartTrait;
use PhpPact\FFI\ClientInterface;
use PhpPact\Plugin\Registry\Interaction\Body\AbstractPluginBodyRegistry;

class ProtobufMessageContentsRegistry extends AbstractPluginBodyRegistry
{
use RequestPartTrait;

public function __construct(
protected ClientInterface $client,
private MessageRegistryInterface $messageRegistry
) {
parent::__construct($client);
}

protected function getInteractionId(): int
{
return $this->messageRegistry->getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PhpPact\Plugins\Protobuf\Registry\Interaction;

use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface;
use PhpPact\Consumer\Registry\Interaction\MessageRegistry;
use PhpPact\Consumer\Registry\Pact\PactRegistryInterface;
use PhpPact\FFI\ClientInterface;
use PhpPact\Plugins\Protobuf\Registry\Interaction\Body\ProtobufMessageContentsRegistry;

class ProtobufMessageRegistry extends MessageRegistry
{
public function __construct(
ClientInterface $client,
PactRegistryInterface $pactRegistry,
?BodyRegistryInterface $messageContentsRegistry = null
) {
parent::__construct($client, $pactRegistry, $messageContentsRegistry ?? new ProtobufMessageContentsRegistry($client, $this));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PhpPact\Plugins\Protobuf\Registry\Interaction;

use PhpPact\Consumer\Registry\Interaction\Body\BodyRegistryInterface;
use PhpPact\Consumer\Registry\Pact\PactRegistryInterface;
use PhpPact\FFI\ClientInterface;
use PhpPact\Plugins\Protobuf\Registry\Interaction\Body\ProtobufMessageContentsRegistry;
use PhpPact\SyncMessage\Registry\Interaction\SyncMessageRegistry;

class ProtobufSyncMessageRegistry extends SyncMessageRegistry
{
public function __construct(
ClientInterface $client,
PactRegistryInterface $pactRegistry,
?BodyRegistryInterface $messageContentsRegistry = null
) {
parent::__construct($client, $pactRegistry, $messageContentsRegistry ?? new ProtobufMessageContentsRegistry($client, $this));
}
}
13 changes: 13 additions & 0 deletions src/PhpPact/Plugins/Protobuf/Service/GrpcMockServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace PhpPact\Plugins\Protobuf\Service;

use PhpPact\Consumer\Service\MockServer;

class GrpcMockServer extends MockServer
{
protected function getTransport(): string
{
return 'grpc';
}
}

0 comments on commit be1a80f

Please sign in to comment.