Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Rpc Common Laravel Package

scaytrase/rpc-common

Common PHP RPC interfaces and helpers with batch-style request support. Includes client decorators (lazy, logging, caching) plus test utilities like a mock client with queued responses and acceptance filters for predictable RPC testing.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require scaytrase/rpc-common
    
    • Verify the package loads in config/app.php under providers.
  2. First Use Case: Basic RPC Client

    use Scaytrase\RpcCommon\Client\RpcClient;
    use Scaytrase\RpcCommon\Message\RpcRequest;
    use Scaytrase\RpcCommon\Message\RpcResponse;
    
    $client = new RpcClient('http://example.com/rpc');
    $request = new RpcRequest('methodName', ['param1', 'param2']);
    $response = $client->send($request);
    
    if ($response->isSuccess()) {
        echo $response->getResult();
    } else {
        echo $response->getError();
    }
    
  3. Key Files to Explore

    • src/Client/RpcClient.php – Core client logic.
    • src/Message/RpcRequest.php & src/Message/RpcResponse.php – Request/response structures.
    • src/Exception/RpcException.php – Error handling.

Implementation Patterns

1. Request/Response Workflow

  • Structured Requests: Use RpcRequest to define method names and parameters.
    $request = new RpcRequest('user.get', ['id' => 1]);
    
  • Response Handling: Check isSuccess() before accessing data.
    if ($response->isSuccess()) {
        $data = $response->getResult();
    }
    

2. Integration with Laravel

  • Service Providers: Bind interfaces to implementations in AppServiceProvider.
    $this->app->bind(
        \Scaytrase\RpcCommon\Client\RpcClientInterface::class,
        \Scaytrase\RpcCommon\Client\RpcClient::class
    );
    
  • HTTP Middleware: Extend RpcClient to add auth headers or logging.
    $client = new RpcClient('http://api.example.com', [
        'headers' => ['Authorization' => 'Bearer ' . auth()->token()]
    ]);
    

3. Batch Requests

  • Chain multiple requests for efficiency.
    $client->send(new RpcRequest('method1', []))
            ->then(function ($response) {
                return $client->send(new RpcRequest('method2', []));
            });
    

4. Error Handling

  • Catch RpcException for malformed responses.
    try {
        $response = $client->send($request);
    } catch (RpcException $e) {
        Log::error($e->getMessage());
    }
    

5. Testing

  • Mock RpcClientInterface in unit tests.
    $mock = Mockery::mock(RpcClientInterface::class);
    $mock->shouldReceive('send')
         ->once()
         ->andReturn(new RpcResponse(true, ['data' => 'test']));
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package

    • Last release in 2017; verify compatibility with modern PHP/Laravel.
    • Check for breaking changes in HTTP clients (e.g., Guzzle 7+).
  2. No Built-in Retries

    • Implement retry logic manually for transient failures.
      $attempts = 0;
      while ($attempts < 3) {
          try {
              $response = $client->send($request);
              break;
          } catch (Exception $e) {
              $attempts++;
              sleep(1);
          }
      }
      
  3. No JSON Schema Validation

    • Manually validate RPC responses if strict contracts are needed.

Debugging Tips

  • Enable Guzzle Logging (if used internally):
    $client = new RpcClient('http://example.com', [
        'debug' => true,
        'handler' => HandlerStack::create(new \GuzzleHttp\Handler\CurlHandler())
    ]);
    
  • Inspect Raw Responses:
    $rawResponse = $client->getLastRawResponse();
    

Extension Points

  1. Custom Serializers

    • Extend RpcRequest/RpcResponse to support custom data formats (e.g., XML).
    class CustomRpcRequest extends RpcRequest {
        public function serialize(): string {
            return json_encode(['custom' => $this->getParams()]);
        }
    }
    
  2. Middleware Support

    • Add pre/post-processing hooks:
    $client = new RpcClient('http://example.com');
    $client->addMiddleware(function ($request) {
        $request->addHeader('X-Custom', 'value');
    });
    
  3. Event Dispatching

    • Trigger events before/after requests (requires Laravel Events):
    event(new RpcRequestEvent($request));
    $response = $client->send($request);
    event(new RpcResponseEvent($response));
    

Configuration Quirks

  • No Default Config File: Package expects runtime configuration (e.g., base URI, headers).
  • HTTP Client Dependency: Assumes Guzzle-like interface; replace if needed.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky