This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57d1231
commit 8b1726d
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* @copyright 2017 Hostnet B.V. | ||
*/ | ||
declare(strict_types=1); | ||
namespace Hostnet\Component\Resolver\Plugin; | ||
|
||
use Hostnet\Component\Resolver\Event\FileEvent; | ||
use Hostnet\Component\Resolver\Event\FileEvents; | ||
use Hostnet\Component\Resolver\File; | ||
use Hostnet\Component\Resolver\FileSystem\FileWriter; | ||
|
||
/** | ||
* This plugin outputs a gzipped file next to the output file. Enabling this with the correct web server settings | ||
* will serve a static gzipped file if the browser accepts gzip encoding. | ||
* For example in Apache this can be done with a simple .htaccess file. | ||
*/ | ||
class GzipPlugin implements PluginInterface | ||
{ | ||
public function activate(PluginApi $plugin_api): void | ||
{ | ||
$config = $plugin_api->getConfig(); | ||
$dispatcher = $config->getEventDispatcher(); | ||
$dispatcher->addListener(FileEvents::POST_WRITE, function (FileEvent $ev) use ($config, $dispatcher) { | ||
$content = $ev->getContent(); | ||
$gzip_content = gzencode($content, 9); | ||
if (strlen($gzip_content) < strlen($content)) { | ||
$writer = new FileWriter($dispatcher, $config->getProjectRoot()); | ||
$writer->write(new File($ev->getFile()->path . '.gz'), $gzip_content); | ||
} | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* @copyright 2018 Hostnet B.V. | ||
*/ | ||
declare(strict_types=1); | ||
namespace Hostnet\Component\Resolver\Plugin; | ||
|
||
use Hostnet\Component\Resolver\Config\ConfigInterface; | ||
use Hostnet\Component\Resolver\Event\FileEvent; | ||
use Hostnet\Component\Resolver\Event\FileEvents; | ||
use Hostnet\Component\Resolver\File; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
|
||
/** | ||
* @covers \Hostnet\Component\Resolver\Plugin\GzipPlugin | ||
*/ | ||
class GzipPluginTest extends TestCase | ||
{ | ||
public function testActivate() | ||
{ | ||
$plugin = new GzipPlugin(); | ||
|
||
$dispatcher = new EventDispatcher(); | ||
|
||
$config = $this->prophesize(ConfigInterface::class); | ||
$config->getEventDispatcher()->willReturn($dispatcher); | ||
$config->getProjectRoot()->willReturn(''); | ||
|
||
$api = $this->prophesize(PluginApi::class); | ||
$api->getConfig()->willReturn($config->reveal()); | ||
|
||
$plugin->activate($api->reveal()); | ||
|
||
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(); | ||
|
||
try { | ||
$contents = str_repeat('file contents', 5); | ||
|
||
$event = new FileEvent(new File($file), $contents); | ||
|
||
$dispatcher->dispatch(FileEvents::POST_WRITE, $event); | ||
|
||
self::assertTrue(file_exists($file . '.gz')); | ||
self::assertEquals($contents, gzdecode(file_get_contents($file . '.gz'))); | ||
} finally { | ||
@unlink($file); | ||
@unlink($file . '.gz'); | ||
} | ||
} | ||
|
||
public function testActivateNoGzipImprovement() | ||
{ | ||
$plugin = new GzipPlugin(); | ||
|
||
$dispatcher = new EventDispatcher(); | ||
|
||
$config = $this->prophesize(ConfigInterface::class); | ||
$config->getEventDispatcher()->willReturn($dispatcher); | ||
|
||
$api = $this->prophesize(PluginApi::class); | ||
$api->getConfig()->willReturn($config->reveal()); | ||
|
||
$plugin->activate($api->reveal()); | ||
|
||
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(); | ||
|
||
try { | ||
$event = new FileEvent(new File($file), ''); | ||
|
||
$dispatcher->dispatch(FileEvents::POST_WRITE, $event); | ||
|
||
self::assertFalse(file_exists($file . '.gz')); | ||
} finally { | ||
@unlink($file); | ||
@unlink($file . '.gz'); | ||
} | ||
} | ||
} |