定制 nimp/link-loom 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nimp/link-loom

最新稳定版本:v1.0.0

Composer 安装命令:

composer require nimp/link-loom

包简介

This package provides a PHP URL Shortener Library

README 文档

README

Tests PHPStan Code Coverage PHP Version

A flexible, extensible URL shortener library for PHP applications with multiple storage backends and event-driven architecture.

Features

  • 🚀 Multiple Storage Backends - Redis, File, or bring your own
  • 📝 PSR-compliant Logging - Built-in Monolog integration
  • 🎯 Event-Driven Architecture - Hook into URL processing lifecycle
  • 🔧 Extensible Design - Easy to customize validators, code generators, and more
  • 💪 Type-Safe - Full PHP type hints and exceptions

Installation

composer require nimp/link-loom

Quick Start

Basic Usage with Redis

<?php

use Nimp\LinkLoom\UrlShortenerBuilder;
use Redis;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$shortener = (new UrlShortenerBuilder())
    ->withRedisRepository($redis)
    ->build();

// Shorten URL
$code = $shortener->encode('https://example.com/long-url');
echo "Short code: " . $code;

// Expand URL
$url = $shortener->decode($code);
echo "Original URL: " . $url;

With Logging

$shortener = (new UrlShortenerBuilder())
    ->withRedisRepository($redis)
    ->withLogger('/path/to/urls.log')
    ->build();

Advanced Configuration

$shortener = (new UrlShortenerBuilder())
    ->withCustomRepository(new MySQLRepository())
    ->withCodeGenerator(new MyCustomCodeGenerator())
    ->withCustomValidator(new MyCustomValidator())
    ->build();

Multiple Event Listeners

$shortener = (new UrlShortenerBuilder())
    ->withRedisRepository($redis)
    ->withLogger('/path/to/urls.log')
    ->addListener(new AnalyticsListener())
    ->addListener(new CacheListener())
    ->build();

Custom Event Dispatcher

$shortener = (new UrlShortenerBuilder())
    ->withRedisRepository($redis)
    ->withEventDispatcher($myCustomDispatcher)
    ->build();

Event System

LinkLoom provides a comprehensive event system for monitoring the URL shortening lifecycle using the Observer pattern.

Available Events

All events extend BaseShortenerEvent and provide access to the UrlShortener instance:

$event->context; // UrlShortener instance
Event Trigger Data
EncodeStartEvent Before URL encoding starts url, context
EncodeSuccessEvent After successful URL encoding url, code, context
DecodeStartEvent Before code decoding starts code , context
DecodeSuccessEvent After successful code decoding code, url, context
ValidateErrorEvent When URL validation fails url, message, context
GetFromStorageErrorEvent When storage read fails code, message, context
SaveErrorEvent When storage save fails message, context

UrlShortener Methods

Method Description

encode(string $url): string	Shorten URL and return code
decode(string $code): string	Expand code to original URL

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-03