maatify / common
Common DTOs and helpers for all maatify libraries
Installs: 5
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/maatify/common
Requires
- php: >=8.4
- ext-intl: *
- ext-mbstring: *
- ezyang/htmlpurifier: ^4.19
- maatify/psr-logger: dev-main
This package is auto-updated.
Last update: 2025-11-05 23:19:05 UTC
README
π¦ maatify/common
Common Data Transfer Objects (DTOs) and helper utilities shared across all maatify libraries.
βοΈ Installation
composer require maatify/common
π§© Overview
This library provides common DTOs and helper classes that are reused across all maatify packages,
such as maatify/mongo-activity, maatify/psr-logger, and future maatify projects.
Current modules:
PaginationHelperβ simple array pagination logic.PaginationDTOβ unified structure for pagination metadata.
π Example Usage
πΉ Paginate Array Data
use Maatify\Common\Pagination\Helpers\PaginationHelper; $items = range(1, 100); $result = PaginationHelper::paginate($items, page: 2, perPage: 10); print_r($result);
Output:
[
'data' => [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
'pagination' => Maatify\Common\DTO\PaginationDTO {
page: 2,
perPage: 10,
total: 100,
totalPages: 10,
hasNext: true,
hasPrev: true
}
]
πΉ Working with PaginationDTO
use Maatify\Common\Pagination\DTO\PaginationDTO; $pagination = new PaginationDTO( page: 1, perPage: 25, total: 200, totalPages: 8, hasNext: true, hasPrev: false ); print_r($pagination->toArray());
π Lock System (New)
Advanced locking utilities to prevent concurrent executions in Cron jobs, queue workers, or API-critical flows.
πΉ Available Managers
| Class | Type | Description |
|---|---|---|
FileLockManager |
Local | File-based lock stored in /tmp or any directory |
RedisLockManager |
Distributed | Uses Redis or Predis client for network-safe locking |
HybridLockManager |
Smart | Automatically chooses Redis if available, otherwise falls back to file lock |
LockCleaner |
Utility | Cleans up stale .lock files after timeouts |
LockModeEnum |
Enum | Defines whether lock should EXECUTION (non-blocking) or QUEUE (waits until free) |
π§ Example 1 β File Lock
use Maatify\Common\Lock\FileLockManager; $lock = new FileLockManager('/tmp/maatify/cron/report.lock', 600); if (! $lock->acquire()) { exit("Another job is running.\n"); } echo "Running safely...\n"; $lock->release();
βοΈ Example 2 β Redis Lock
use Maatify\Common\Lock\RedisLockManager; $lock = new RedisLockManager('cleanup_task', ttl: 600); if ($lock->acquire()) { echo "Cleaning...\n"; $lock->release(); }
β
Works automatically with both phpredis and predis.
If Redis is down, it logs an error via maatify/psr-logger.
π Example 3 β Hybrid Lock (Recommended)
use Maatify\Common\Lock\HybridLockManager; use Maatify\Common\Lock\LockModeEnum; $lock = new HybridLockManager( key: 'daily_summary', mode: LockModeEnum::QUEUE, ttl: 600 ); $lock->run(function () { echo "Generating daily summary...\n"; });
Automatically uses Redis if available, otherwise falls back to file lock.
π§Ή Example 4 β Clean Old Locks
use Maatify\Common\Lock\LockCleaner; LockCleaner::cleanOldLocks(sys_get_temp_dir() . '/maatify/locks', 900);
π§Ύ Notes
- All lock operations are fully logged (via
maatify/psr-logger). - Default lock expiration (TTL) is 300 seconds (5 minutes).
- Hybrid mode retries every 0.5 seconds when using queue mode.
π Directory (Lock Module)
src/Lock/
βββ LockInterface.php
βββ LockModeEnum.php
βββ FileLockManager.php
βββ RedisLockManager.php
βββ HybridLockManager.php
βββ LockCleaner.php
π Cron Lock System (Legacy Section)
This module provides simple yet powerful locking mechanisms to prevent concurrent cron executions.
Available implementations :
FileCronLockβ lightweight local lock for single-host environments.RedisCronLockβ distributed lock using Redis or Predis, automatically disabled if Redis is unavailable.
Interface:
use Maatify\Common\Lock\LockInterface;
Example:
use Maatify\Common\Lock\FileLockManager; $lock = new FileLockManager('/var/locks/daily_job.lock', 300); if (! $lock->acquire()) { exit("Cron already running...\n"); } echo "Running job...\n"; // ... job logic ... $lock->release();
β If Redis or Predis is installed, you can use:
use Maatify\Common\Lock\RedisLockManager; $lock = new RedisLockManager('daily_job'); if ($lock->acquire()) { // do work $lock->release(); }
Redis version automatically logs a warning (and safely disables itself) if Redis isnβt available.
π§Ό Input Sanitization
Use Maatify\Common\Security\InputSanitizer to clean any user or system input safely.
use Maatify\Common\Security\InputSanitizer; echo InputSanitizer::sanitize('<script>alert(1)</script>', 'output'); // Output: <script>alert(1)</script>
π§± Directory Structure
src/
βββ Pagination/
β βββ DTO/
β β βββ PaginationDTO.php
β βββ Helpers/
β β βββ PaginationHelper.php
β β βββ PaginationResultDTO.php
βββ Lock/
βββ LockInterface.php
βββ LockModeEnum.php
βββ FileLockManager.php
βββ RedisLockManager.php
βββ HybridLockManager.php
βββ LockCleaner.php
βββ Security/
βββ InputSanitizer.php
βββ Traits/
βββ SanitizesInputTrait.php
πͺͺ License
Youβre free to use, modify, and distribute this library with attribution.
π€ Author
Maatify.dev https://www.Maatify.dev