PHP code example of alkinbg / optimistic-concurrency-bundle
1. Go to this page and download the library: Download alkinbg/optimistic-concurrency-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/ */
alkinbg / optimistic-concurrency-bundle example snippets
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Document
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180)]
private string $title;
#[ORM\Version]
#[ORM\Column(type: 'integer')]
private int $version;
// ...
}
use OptimisticConcurrency\Bundle\Attribute\EntityTag;
use Symfony\Component\HttpFoundation\JsonResponse;
#[EntityTag('document', scope: 'document-detail-v1')]
public function show(Document $document): JsonResponse
{
return new JsonResponse([
'id' => $document->getId(),
'title' => $document->getTitle(),
]);
}
use OptimisticConcurrency\Bundle\Attribute\RequireIfMatch;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
#[RequireIfMatch('document', scope: 'document-detail-v1')]
public function update(
Document $document,
EntityManagerInterface $entityManager,
): JsonResponse {
$document->rename('New title');
$entityManager->flush();
return new JsonResponse([
'id' => $document->getId(),
'title' => $document->getTitle(),
]);
}
use OptimisticConcurrency\Bundle\Context\EntityTagContext;
use OptimisticConcurrency\Bundle\Contract\EntityTagProviderInterface;
use App\Entity\Document;
final readonly class ApplicationEntityTagProvider implements EntityTagProviderInterface
{
public function __construct(private EntityTagProviderInterface $inner)
{
}
public function generate(object $entity, EntityTagContext $context): string
{
$baseTag = $this->inner->generate($entity, $context);
if (!$entity instanceof Document || 'document-detail-v1' !== $context->scope) {
return $baseTag;
}
$fingerprint = implode("\0", [
$baseTag,
$context->request->getLocale(),
$entity->relatedStateFingerprint(),
]);
return '"app1-'.hash('sha256', $fingerprint).'"';
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.