Skip to content

Commit

Permalink
Some new minor functions, readme updates
Browse files Browse the repository at this point in the history
  • Loading branch information
BelleNottelling committed Apr 7, 2024
1 parent 34efac6 commit eb94e17
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
# CompressionBuffer

CompressionBuffer provides easy access to zstd, brotli, and gzip output buffering with PHP on **any** webserver. You can even get zstd [output compression with the PHP development server](https://www.daniel.priv.no/tools/zstd-browser-test/).
CompressionBuffer provides easy access to `zstd`, `brotli`, and `gzip` output buffering with PHP on **any** web server. You can even get zstd [output compression with the PHP development server](https://github.com/HostByBelle/CompressionBuffer/blob/main/misc/zstd-php-dev-server.png).

## Features

- Respects the `Accept-Encoding` header sent by the client, including the specified priority for each compression method.
- Allows `zstd`, `brotli`, `gzip` and `deflate` compression methods to be used on any web server.
- All included compression methods have been benchmarked with compression levels carefully selected for the ideal balance between speed and size reduction.
- Auto-selects the compression method based on client headers and available PHP extensions.
- Automatically sends the appropriate headers.

## Requirements

- A PHP application using output buffering.
- `PHP 8.0` or greater.
- `ext-brotli` if you want brotli compression.
- `ext-zstd`if you want zstd compression.
- `ext-zlib` if you want gzip / deflate compression.

CompressionBuffer will automatically pick the best possible compression method based on the client's `Accept-Encoding` header and installed extensions with no extra configuration required.

## Installation & Usage

Install via composer:
_Note_: CompressionBuffer isn't quite yet released, you'll have to use the `main` dev branch specifically.

```bash
composer install hostbybelle/compressionbuffer
Expand All @@ -37,6 +45,14 @@ echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit,";
ob_end_flush();
```

Toggling CompressionBuffer's status:

```PHP
CompressionBuffer::enable(); // Enable output compression (enabled by default)
CompressionBuffer::disable(); // Disable output compression
CompressionBuffer::isEnabled(); // Check if it's currently enabled or not
```

## Compression Method Tests & Results

### Compression Methods, Ranked
Expand Down
33 changes: 33 additions & 0 deletions src/HostByBelle/CompressionBuffer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

/**
* Copyright 2024 HostByBelle
*/

declare(strict_types=1);

namespace HostByBelle;
Expand All @@ -18,8 +22,33 @@ class CompressionBuffer

private static bool $attemptMultiple = false;
private static bool $respectPreferred = true;
private static bool $doCompression = true;
private static array $tryOrder = [];

/**
* Enables output compression
*/
public static function enable(): void
{
self::$doCompression = true;
}

/**
* Disables output compression
*/
public static function disable(): void
{
self::$doCompression = false;
}

/**
* Used to check if output compression is enabled or disabled
*/
public static function isEnabled(): bool
{
return self::$doCompression;
}

/**
* Must be called so CompressionBuffer can perform the needed checks and setup.
* Once called, it will automatically find compatible compression methods for the client & server before then sorting them in a logical try order.
Expand All @@ -40,6 +69,10 @@ public static function setUp(bool $respectPreferred = true, bool $attemptMultipl
public static function handler(string $buffer, int $phase): string
{
if ($phase & PHP_OUTPUT_HANDLER_FINAL || $phase & PHP_OUTPUT_HANDLER_END) {
if (!self::$doCompression) {
return $buffer;
}

foreach (self::$tryOrder as $encoding) {
try {
$compressed = self::doCompression($encoding, $buffer);
Expand Down

0 comments on commit eb94e17

Please sign in to comment.