PHPackages                             digidip/url-rewriter - 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. digidip/url-rewriter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

digidip/url-rewriter
====================

digidip URL Rewriter with a circuit breaker.

v0.3.0(6y ago)03MITPHP &gt;=7.2

Since Jan 23Compare

[ Source](https://github.com/digidip/url-rewriter)[ Packagist](https://packagist.org/packages/digidip/url-rewriter)[ RSS](/packages/digidip-url-rewriter/feed)WikiDiscussions Synced 3w ago

READMEChangelogDependencies (4)Versions (7)Used By (0)

URL Rewriter for digidp GmbH visit links
========================================

[](#url-rewriter-for-digidp-gmbh-visit-links)

- [Key Concepts](#key-concepts)
    - [Url Writer](#url-writer)
    - [Circuit Breaker](#circuit-breaker)
    - [Url Writer Strategies](#url-writer-strategies)
    - [Circuit Breaker Adapters](#circuit-breaker-adapters)
- [Setup / Installation](#installation)
- [Examples](#examples)

This library was created to allow digidip customers to rewrite their affiliate URLs into digidip affiliate URLs for tracking purchases etc. The library takes advantage of a circuit breaker methodology to help reduce the number of users being redirected via digidip's servers when a server or network issue exists. When a server outage is detected, the circuit will become open and all rewritten URLs will be returned as the original URL provided to the library.

If the circuit is open, it means that the transaction can not be tracked by digidip, although a majority of users will experience a positive redirection to the merchant's page instead of experiencing a error page.

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

[](#installation)

You may install UrlRewriter via Composer:

```
composer require digidip/url-rewriter

```

### Dependancies

[](#dependancies)

If you wish to use the Memcached or Redis adapter you must install either PECL extensions:

- Redis (`pecl install redis`)
- Memcached (`pecl install memcached`)

Examples
--------

[](#examples)

```
// Basic example of the implementation.

$redis = new \Redis();
$redis->connect('....');

$adapter = new RedisCircuitBreakerAdapter($redis);
$circuit = new CircuitBreaker($adapter);
$rewriter = new UrlRewriter($circuit, new DigidipSubdomainRewriterStrategy('mydomain'));

$url = $rewriter->getUrl('http://www.merchant.com');

// ... $url is used elsewhere as part of your implementation...
```

For more check the examples directory.

Key Concepts
------------

[](#key-concepts)

- An explanation of the [circuit breaker pattern](https://martinfowler.com/bliki/CircuitBreaker.html).
- When the failure count is zero, then the circuit is considered `Closed`.
- When the failure count is greater than zero, but less than the failure threshold, the circuit is considered `Half-Open`.
- When the failure count is greater than zero and equal to the failure threshold, the circuit is considered `Open`.
- When the circuit is `Closed` or `Half-Open`, the UrlRewriter theoretically will convert `https://mymerchant.com` to `http://visit.digidip.net/visit?url=http%3A%2F%2Fmymerchant.com`.
- When the circuit is `Open`, the UrlRewriter will simply return `https://mymerchant.com` with no rewriter strategy being used.

### Url Writer

[](#url-writer)

This class is used to bind the Circuit Breaker and URL Writer strageies together, ideally you should only need to interact with this class in your existing code base. Reference **Url Writer Strategies** below for available strategies.

```
use digidip\UrlRewriter;

$urlRewriter = new UrlRewriter(
    // ... CircuitBreaker $circuitBreaker,
    // ... RewriterStrategy $strategy,
    // ... LoggerInterface $logger = null       (PSR Logger Instance)
);

$url = $urlRewriter->getUrl('https://www.a-merchant.com/'); // Return a URL whiuch is defined by the RewriterStrategy.
```

### Circuit Breaker

[](#circuit-breaker)

The circuit breaker class is the responsible on whether the URL Rewriter should perform a URL rewrite or not. It minimal requirements are an adapter which provides the Circuit Breaker a mechanism to communicate with an external storage resource, such as a file, Redis, Memcache or any bespoke method which can be implemented with the `CircuitBreakerAdapter` interface. Reference **Circuit Breaker Adapters** below for available adapters.

```
use digidip\CircuitBreaker;
use digidip\Adapters\FilePathCircuitBreakerAdapter;

$adapter = new FilePathCircuitBreakerAdapter('/path/to/file.json');
$circuit = new CircuitBreaker(
    $adapter,
    // ... ?array $options = [],
    // ... ?string $url = null,                 For testing purposes
    // ... ?Client $client = null,              For testing purposes
    // ... ?LoggerInterface $logger = null      (PSR Logger Instance)
);
```

#### Circuit Breaker Options (`?array $options = []`)

[](#circuit-breaker-options-array-options--)

- `CircuitBreaker::OPTION_TIMEOUT` - Number of milliseconds before the HTTP request will timeout and be considered a service failure. *Default: 1000*
- `CircuitBreaker::OPTION_FAILURE_THRESHOLD` - The amount of failures which must occur before opening the circuit. *Default: 5*
- `CircuitBreaker::OPTION_OPENED_SAMPLE_RATE` - The number of seconds between polling the service's availablity, once a single failure has been detected, this sample rate will be used. *Default: 1*
- `CircuitBreaker::OPTION_CLOSED_SAMPLE_RATE` - The number of seconds between polling the service's availablity while no failures are detected. *Default: 5*

### Url Writer Strategies

[](#url-writer-strategies)

Currently available stategies include:

- Standard Digidip Rewriter Strategy - `DigidipRewriterStrategy(pid)`
- Subdomain based Digidip Rewriter Strategy - `DigidipSubdomainRewriterStrategy(subdomain)`
- Template Rewriter - `TemplateRewriterStrategy(url template)`

### Circuit Breaker Adapters

[](#circuit-breaker-adapters)

Currently available adapters include:

- File Path - `FilePathCircuitBreakerAdapter(filepath)`
- File reader and writer interfaces - `FileCircuitBreakerAdapter(Reader, Writer)`, reference `src/Modules/Filesystem` for available Readers and Writers.
- Redis - `RedisCircuitBreakerAdapter(\Redis instance)`
- Memcached - `MemcachedCircuitBreakerAdapter(\Memcached instance)`

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

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 ~0 days

Total

4

Last Release

2353d ago

PHP version history (2 changes)v0.1.0PHP &gt;=7.1

v0.2.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5e1a9ef783e9b7a87ef16fd7dd7e9bf8e1705ce729027db4f647f838276fe427?d=identicon)[gil@digidip](/maintainers/gil@digidip)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/digidip-url-rewriter/health.svg)

```
[![Health](https://phpackages.com/badges/digidip-url-rewriter/health.svg)](https://phpackages.com/packages/digidip-url-rewriter)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M20.1k](/packages/laravel-framework)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

23.9k69.5k](/packages/grumpydictator-firefly-iii)[civicrm/civicrm-core

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

751291.4k43](/packages/civicrm-civicrm-core)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

21866.0M1.7k](/packages/drupal-core)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6942.5M421](/packages/drupal-core-recommended)

PHPackages © 2026

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