Created
August 15, 2014 02:19
-
-
Save jm42/d66ce79428dd7ffd5992 to your computer and use it in GitHub Desktop.
PHP Stream Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use php_user_filter as StreamFilter; | |
use Composer\Autoload\ClassLoader; | |
require __DIR__ . '/vendor/autoload.php'; | |
class MyFilter extends StreamFilter | |
{ | |
const FILTER_ID = 'my_filter'; | |
private $data; | |
static public function register() | |
{ | |
\stream_filter_register(self::FILTER_ID, __CLASS__); | |
} | |
public function filter($in, $out, &$consumed, $closing) | |
{ | |
while ($bucket = stream_bucket_make_writeable($in)) { | |
$this->bucket = $bucket; | |
$this->data .= $bucket->data; | |
} | |
if ($closing) { | |
$this->bucket->data = $this->data; | |
$this->bucket->datalen = \strlen($this->data); | |
$consumed += $this->bucket->datalen; | |
if (!empty($this->bucket->data)) { | |
\stream_bucket_append($out, $this->bucket); | |
} | |
return PSFS_PASS_ON; | |
} | |
$consumed = 0; | |
return PSFS_FEED_ME; | |
} | |
} | |
class MyClassLoader | |
{ | |
protected $loader; | |
public function __construct(ClassLoader $loader) | |
{ | |
$this->loader = $loader; | |
} | |
public function loadClass($class) | |
{ | |
if ($file = $this->loader->findFile($class)) { | |
include 'php://filter/read=' . MyFilter::FILTER_ID . '/resource=' . $file; | |
return true; | |
} | |
} | |
static public function register() | |
{ | |
$loaders = \spl_autoload_functions(); | |
if ($loaders) { | |
foreach ($loaders as $loader) { | |
if (is_array($loader) && $loader[0] instanceof ClassLoader) { | |
$loader[0]->unregister(); | |
$new = new self($loader[0]); | |
\spl_autoload_register(array($new, 'loadClass'), true, false); | |
} | |
} | |
} | |
} | |
} | |
MyFilter::register(); | |
MyClassLoader::register(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment