PHP code example of tourze / json-rpc-lock-bundle

1. Go to this page and download the library: Download tourze/json-rpc-lock-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / json-rpc-lock-bundle example snippets


// config/bundles.php
return [
    // ...
    Tourze\JsonRPCLockBundle\JsonRPCLockBundle::class => ['all' => true],
];



use Tourze\JsonRPCLockBundle\Procedure\LockableProcedure;
use Tourze\JsonRPC\Core\Model\JsonRpcParams;

class MySecureProcedure extends LockableProcedure
{
    public function execute(): array
    {
        // Your business logic here
        $params = $this->getParams();
        $userId = $params->get('user_id');
        $amount = $params->get('amount');
        
        // This will be automatically locked based on user identity
        return ['result' => $this->processPayment($userId, $amount)];
    }
    
    private function processPayment(int $userId, float $amount): string
    {
        // Your payment processing logic
        return 'payment_processed';
    }
}

public function getLockResource(JsonRpcParams $params): ?array
{
    // Custom lock based on specific parameters
    $accountId = $params->get('account_id');
    return ['account_lock_' . $accountId];
}

// To skip locking entirely, return null
public function getLockResource(JsonRpcParams $params): ?array
{
    return null; // No locking applied
}

protected function getIdempotentCacheKey(JsonRpcRequest $request): ?string
{
    $params = $request->getParams();
    return 'payment_' . $params->get('user_id') . '_' . $params->get('transaction_id');
}

public function fallbackRetry(): bool
{
    return true; // Enable fallback retry on lock failures
}
bash
php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/json-rpc-lock-bundle