PHP Classes

File: src/Generics/Streams/Interceptor/CachedStreamInterceptor.php

Recommend this page to a friend!
  Classes of Maik Greubel   PHP Generics   src/Generics/Streams/Interceptor/CachedStreamInterceptor.php   Download  
File: src/Generics/Streams/Interceptor/CachedStreamInterceptor.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Generics
Framework for accessing streams, sockets and logs
Author: By
Last change: Update of src/Generics/Streams/Interceptor/CachedStreamInterceptor.php
Date: 7 months ago
Size: 1,675 bytes
 

Contents

Class file image Download
<?php
/**
 * This file is part of the PHP Generics package.
 *
 * @package Generics
 */
namespace Generics\Streams\Interceptor;

/**
 * This class provides an caching stream interceptor
 *
 * @author Maik Greubel <greubel@nkey.de>
 */
class CachedStreamInterceptor extends AbstractStreamInterceptor
{

   
/**
     *
     * @var string
     */
   
private static $cache = "";

   
/**
     * Create a new instance of CachedStreamInterceptor
     */
   
public function __construct()
    {
       
stream_filter_register($this->getFilterName(), CachedStreamInterceptor::class);
    }

   
/**
     *
     * {@inheritdoc}
     * @see \Generics\Streams\Interceptor\StreamInterceptor::filter()
     */
   
public function filter($in, $out, int &$consumed, bool $closing): int
   
{
        if (
$closing) {
            return
PSFS_FEED_ME;
        }
       
        while (
$bucket = stream_bucket_make_writeable($in)) {
           
self::$cache .= $bucket->data;
           
$consumed += $bucket->datalen;
           
stream_bucket_append($out, $bucket);
        }
        return
PSFS_PASS_ON;
    }

   
/**
     * Retrieve the cache buffer
     *
     * @return string
     */
   
public function getCache(): string
   
{
        return
self::$cache;
    }

   
/**
     *
     * {@inheritdoc}
     * @see \Generics\Streams\Interceptor\StreamInterceptor::getFilterName()
     */
   
public function getFilterName(): string
   
{
        return
strtolower(CachedStreamInterceptor::class);
    }

   
/**
     *
     * {@inheritdoc}
     * @see \Generics\Streams\Interceptor\StreamInterceptor::reset()
     */
   
public function reset()
    {
       
self::$cache = "";
    }
}