PHPackages                             texthtml/php-lock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. texthtml/php-lock

Abandoned → [symfony/lock](/?search=symfony%2Flock)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

texthtml/php-lock
=================

ressource lock

v2.2.1(8y ago)29234.2k↓44.7%4[1 issues](https://github.com/texthtml/php-lock/issues)[2 PRs](https://github.com/texthtml/php-lock/pulls)2aGPLv3PHPCI passing

Since Sep 13Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/texthtml/php-lock)[ Packagist](https://packagist.org/packages/texthtml/php-lock)[ RSS](/packages/texthtml-php-lock/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (3)Dependencies (3)Versions (14)Used By (2)

php-lock
========

[](#php-lock)

[![Build Status](https://camo.githubusercontent.com/bfe1f5070d6c724c33b27ef07e9650f8ebf1aff76e764f2bded1b4dba33263e7/68747470733a2f2f7472617669732d63692e6f72672f7465787468746d6c2f7068702d6c6f636b2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/texthtml/php-lock)[![Latest Stable Version](https://camo.githubusercontent.com/0b3bb61522670421651d6e26c704bb3a9b28d4d6d96411cacfc94c09331254b5/68747470733a2f2f706f7365722e707567782e6f72672f7465787468746d6c2f7068702d6c6f636b2f762f737461626c652e737667)](https://packagist.org/packages/texthtml/php-lock)[![License](https://camo.githubusercontent.com/208a6ccafbd0d0657b4351f2092f11fb3e80888ffdd0b7dcb3c89663f8a09ab6/68747470733a2f2f706f7365722e707567782e6f72672f7465787468746d6c2f7068702d6c6f636b2f6c6963656e73652e737667)](http://www.gnu.org/licenses/agpl-3.0.html)[![Total Downloads](https://camo.githubusercontent.com/ff1f4a880c1da5c5134cba2b6b1e99d704d8b2bc2557c2e18734bc2a6beb52c8/68747470733a2f2f706f7365722e707567782e6f72672f7465787468746d6c2f7068702d6c6f636b2f646f776e6c6f6164732e737667)](https://packagist.org/packages/texthtml/php-lock)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/dee9c279c0df6407e680c5ecd6643c8952316929520bc30ee641db2364de1571/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7465787468746d6c2f7068702d6c6f636b2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/texthtml/php-lock/?branch=master)

[php-lock](https://packagist.org/packages/texthtml/php-lock) is a library that makes locking on resources easy. It can be used to avoid access to a file during write operations or to prevent crontabs to overlap. And it's designed to integrate well with Dependancy Injection (eg Symfony Container or Pimple).

Installation
------------

[](#installation)

With Composer :

```
composer require texthtml/php-lock
```

Usage
-----

[](#usage)

You can create an object that represent a lock on a file. You can then try to acquire that lock by calling `$lock->acquire()`. If the lock fail it will throw a `\TH\Lock\Exception` (useful for CLI tools built with [Symfony Console Components documentation](http://symfony.com/doc/current/components/console/introduction.html)). If the lock is acquired the program can continue.

### Locking a file exclusively

[](#locking-a-file-exclusively)

```
use TH\Lock\FileLock;

$lock = new FileLock('/path/to/file');

$lock->acquire();

// other processes that try to acquire a lock on the file will fail

// edit /path/to/file

$lock->release();

// other processes can now acquire a lock on the file
```

### Sharing a lock on a file

[](#sharing-a-lock-on-a-file)

```
use TH\Lock\FileLock;

$lock = new FileLock('/path/to/file', FileLock::SHARED);

$lock->acquire();

// other processes that try to acquire an exclusive lock on the file will fail,
// processes that try to acquire an shared lock on the file will succeed

// read /path/to/file

$lock->release();

// other processes can now acquire an exclusive lock on the file if no other shared lock remains.
```

### Auto release

[](#auto-release)

`$lock->release()` is called automatically when the lock is destroyed so you don't need to manually release it at the end of a script or if it got out of scope.

```
use TH\Lock\FileLock;

function batch() {
    $lock = new FileLock('/some/file');
    $lock->acquire();

    // lot of editing on file
}

batch();

// the lock will be released here even if $lock->release() is not called in batch()
```

### Using lock for crontabs

[](#using-lock-for-crontabs)

When you don't want some crontabs to overlap you can make a lock on the same file in each crontab. The `TH\Lock\LockFactory` can ease the process and provide more helpful message in case of overlap.

```
$lock = $factory->create('protected resource', 'process 1');

$lock->acquire();

// process 1 does stuff
```

```
$lock = $factory->create('protected resource', 'process 2');

$lock->acquire();

// process 2 does stuff
```

When process 1 is running and we start process 2, an Exception will be thrown: "Could not acquire exclusive lock on protected resource" and if the factory was configured with a `\Psr\Log\LoggerInterface`, messages explaining what happend would be logged:

```
process 1: exclusive lock acquired on protected resource
process 2: could not acquire exclusive lock on protected resource
process 2: lock released on protected resource

```

The only `LockFactory` available at the moment is the `TH\Lock\FileFactory`. This factory autmatically create lock files for your resources in the specified folder.

```
use TH\Lock\FileFactory;

$factory = new FileFactory('/path/to/lock_dir/');
$lock = $factory->create('resource identifier');
```

### Aggregating locks

[](#aggregating-locks)

If you want to simplify acquiring multiple locks at once, you can use the `\TH\Lock\LockSet`:

```
use TH\Lock\LockSet;

$superLock = new LockSet([$lock1, $lock2, $lock3]);
// You can make a set with any types of locks (eg: FileLock, RedisSimpleLock or another nested LockSet)

$superLock->acquire();

// all locks will be released when $superLock is destroyed or when `$superLock->release()` is called
```

It will try to acquire all locks, if it fails it will release the lock that have been acquired to avoid locking other processes.

note: `Lock` put inside a `LockSet` should not be used manually anymore

Notes
-----

[](#notes)

### Distributed system

[](#distributed-system)

On a distributed system, file based locking does not work, You can use the [php-lock redis extension](https://github.com/texthtml/php-lock-redis) to have a safe lock instead.

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance44

Moderate activity, may be stable

Popularity42

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~147 days

Recently: every ~232 days

Total

8

Last Release

3282d ago

Major Versions

v1.1.2 → v2.0.02015-10-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/3943b5c35797d1ede53aaed3d6812e4cf5b2dba7619a00dc7a381480a7b3d330?d=identicon)[mathroc](/maintainers/mathroc)

---

Top Contributors

[![mathroc](https://avatars.githubusercontent.com/u/291531?v=4)](https://github.com/mathroc "mathroc (45 commits)")[![text-html-renovate[bot]](https://avatars.githubusercontent.com/u/6465918?v=4)](https://github.com/text-html-renovate[bot] "text-html-renovate[bot] (6 commits)")[![denpamusic](https://avatars.githubusercontent.com/u/16575433?v=4)](https://github.com/denpamusic "denpamusic (2 commits)")[![ayrtonvwf](https://avatars.githubusercontent.com/u/22180887?v=4)](https://github.com/ayrtonvwf "ayrtonvwf (1 commits)")

---

Tags

cronlockphp

### Embed Badge

![Health badge](/badges/texthtml-php-lock/health.svg)

```
[![Health](https://phpackages.com/badges/texthtml-php-lock/health.svg)](https://phpackages.com/packages/texthtml-php-lock)
```

###  Alternatives

[symfony/lock

Creates and manages locks, a mechanism to provide exclusive access to a shared resource

514139.2M693](/packages/symfony-lock)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k53](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)[illuminate/broadcasting

The Illuminate Broadcasting package.

7127.2M209](/packages/illuminate-broadcasting)[logiscape/mcp-sdk-php

Model Context Protocol SDK for PHP

368116.8k12](/packages/logiscape-mcp-sdk-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
