2017 © Pedro PelΓ‘ez
 

symfony-bundle offline-locker-bundle

Symfony OfflineLockerBundle

image

text-media/offline-locker-bundle

Symfony OfflineLockerBundle

  • Tuesday, July 24, 2018
  • by text-media
  • Repository
  • 0 Watchers
  • 0 Stars
  • 54 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Offline Locker Bundle

Packagist Packagist, (*1)

ΠœΠΎΠ΄ΡƒΠ»ΡŒ Symfony для Π΄Π»ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΎΠΊ ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Π°ΠΌ, (*2)

Установка

ΠœΠΎΠ΄ΡƒΠ»ΡŒ устанавливаСтся Ρ‡Π΅Ρ€Π΅Π· Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ зависимости Π² composer, (*3)

composer require text-media/offline-locker-bundle

Π’ ΠΊΠΎΠ½Ρ„ΠΈΠ³ Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΡΠ΅ΠΊΡ†ΠΈΡŽ offline_locker ΠΈ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΊΠΎΠ½Ρ„ΠΈΠ³ doctrine для ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½ΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ, (*4)

offline_locker:
    # Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΡ‹ΠΉ ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ€
    entity_manager: offline_locker

doctrine:
    # ΠžΡ‚Π΄Π΅Π»ΡŒΠ½ΠΎΠ΅ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ΠΊ Π‘Π”
    dbal:
        connections:
            offline_locker:
                driver: pdo_sqlite
                path: "%kernel.cache_dir%/offline_locker.db"
                charset: UTF8
    orm:
        entity_managers:
            offline_locker:
                connection: offline_locker
                naming_strategy: doctrine.orm.naming_strategy.underscore
                mappings:
                    OfflineLockerBundle: ~

Π’Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΌΠΈΠ³Ρ€Π°Ρ†ΠΈΡŽ автоматичСски ΠΈΠ»ΠΈ Π²Ρ€ΡƒΡ‡Π½ΡƒΡŽ, (*5)

php ./bin/console doctrine:schema:update --em=offline_locker --dump-sql

ИспользованиС

Π’ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Π΅ зарСгистрирован сСрвис \TextMedia\OfflineLockerBundle\OfflineLockerInterface (ΠΈΠ»ΠΈ offline_locker) Ρ€Π΅Π°Π»ΠΈΠ·ΡƒΡŽΡ‰ΠΈΠΉ ΠΎΠ΄Π½ΠΎΠΈΠΌΠ΅Π½Π½Ρ‹ΠΉ интСрфСйс, (*6)

<?php
    use TextMedia\OfflineLockerBundle\ConcurrencyException;
    use TextMedia\OfflineLockerBundle\OfflineLockerInterface;

    class MyService
    {
        public function __construct(OfflineLockerInterface $locker)
        {
            $lockKey = 'order::1000';
            $lockOwner = __CLASS__;
            try {
                # Π—Π°Ρ…Π²Π°Ρ‚ΠΈΡ‚ΡŒ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΡƒ
                $locker->acquireLock($lockKey, $lockOwner);
            } catch (ConcurrencyException $e) {
                # Π‘Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠ° Π½Π΅ Π·Π°Ρ…Π²Π°Ρ‡Π΅Π½Π°
            }

            try {
                $this->doSomething();
            } finally {
                # ΠžΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡ‚ΡŒ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΡƒ
                $locker->releaseLock($lockKey, $lockOwner);
            }
        }
    }

The Versions