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

dev-main 2025-11-05 23:18 UTC

This package is auto-updated.

Last update: 2025-11-05 23:19:05 UTC


README

Current version Packagist PHP Version Support Monthly Downloads Total Downloads License

πŸ“¦ 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: &lt;script&gt;alert(1)&lt;/script&gt;

🧱 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

MIT license Β© Maatify.dev

You’re free to use, modify, and distribute this library with attribution.

πŸ‘€ Author

Maatify.dev https://www.Maatify.dev