Skip to content

Commit 106f08a

Browse files
committed
Move management of compressors to Environment
1 parent b5d6833 commit 106f08a

File tree

5 files changed

+95
-19
lines changed

5 files changed

+95
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ The `write` method takes an array of options:
143143
### Enabling Compression
144144

145145
You can turn on compression by setting the `js_compressor` and
146-
`css_compressor` config keys.
146+
`css_compressor` config keys, or by calling `setJsCompressor` or
147+
`setCssCompressor` on an Environment instance.
147148

148149
Supported Javascript Compressors:
149150

lib/Pipe/Config.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
class Config
88
{
99
public
10-
# Maps compressor names (available as js_compressor/css_compressor)
11-
# to template classes.
12-
$compressors = array(
13-
"uglify_js" => "\\Pipe\\Compressor\\UglifyJs",
14-
"yuglify_css" => "\\Pipe\\Compressor\\YuglifyCss",
15-
"yuglify_js" => "\\Pipe\\Compressor\\YuglifyJs",
16-
),
17-
1810
$filename,
1911
$precompile,
2012
$precompilePrefix,
@@ -65,19 +57,11 @@ function createEnvironment()
6557

6658
if (!$this->debug) {
6759
if ($jsCompressor = $this->jsCompressor) {
68-
if ($compressor = @$this->compressors[$jsCompressor]) {
69-
$env->registerBundleProcessor('application/javascript', $compressor);
70-
} else {
71-
throw new \UnexpectedValueException("JS compressor '$jsCompressor' not found.");
72-
}
60+
$env->setJsCompressor($jsCompressor);
7361
}
7462

7563
if ($cssCompressor = $this->cssCompressor) {
76-
if ($compressor = @$this->compressors[$cssCompressor]) {
77-
$env->registerBundleProcessor('text/css', $compressor);
78-
} else {
79-
throw new \UnexpectedValueException("CSS compressor '$cssCompressor' not found.");
80-
}
64+
$env->setCssCompressor($cssCompressor);
8165
}
8266
}
8367

lib/Pipe/Environment.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ class Environment implements \ArrayAccess
3333
public $postProcessors;
3434
public $bundleProcessors;
3535

36+
public $compressors = array(
37+
"uglify_js" => "\\Pipe\\Compressor\\UglifyJs",
38+
"yuglify_css" => "\\Pipe\\Compressor\\YuglifyCss",
39+
"yuglify_js" => "\\Pipe\\Compressor\\YuglifyJs",
40+
);
41+
42+
protected $jsCompressor;
43+
protected $cssCompressor;
44+
3645
function __construct($root = null)
3746
{
3847
$this->root = $root;
@@ -121,6 +130,43 @@ function find($logicalPath, $options = array())
121130
return $asset;
122131
}
123132

133+
function setJsCompressor($compressor)
134+
{
135+
if (!isset($this->compressors[$compressor])) {
136+
throw new \InvalidArgumentException(sprintf('Undefined compressor "%s"', $compressor));
137+
}
138+
139+
$js = $this->contentType('.js');
140+
141+
if ($this->jsCompressor !== null) {
142+
$this->bundleProcessors->unregister($js, $this->jsCompressor);
143+
}
144+
145+
$this->jsCompressor = $compressor;
146+
$this->bundleProcessors->register($js, $this->compressors[$compressor]);
147+
}
148+
149+
function setCssCompressor($compressor)
150+
{
151+
if (!isset($this->compressors[$compressor])) {
152+
throw new \InvalidArgumentException(sprintf('Undefined compressor "%s"', $compressor));
153+
}
154+
155+
$css = $this->contentType('.css');
156+
157+
if ($this->cssCompressor !== null) {
158+
$this->bundleProcessors->unregister($css, $this->cssCompressor);
159+
}
160+
161+
$this->cssCompressor = $compressor;
162+
$this->bundleProcessors->register($css, $this->compressors[$compressor]);
163+
}
164+
165+
function contentType($extension)
166+
{
167+
return @$this->contentTypes[Path::normalizeExtension($extension)];
168+
}
169+
124170
# Sugar for find().
125171
#
126172
# logicalPath - The path relative to the virtual file system.

lib/Pipe/Util/ProcessorRegistry.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ function register($mimeType, $processor)
3434
return $this;
3535
}
3636

37+
function isRegistered($mimeType, $processor)
38+
{
39+
if (empty($this->processors[$mimeType])) {
40+
return false;
41+
}
42+
43+
$index = array_search($processor, $this->processors[$mimeType]);
44+
45+
return $index !== false;
46+
}
47+
48+
function unregister($mimeType, $processor)
49+
{
50+
if ($this->isRegistered($mimeType, $processor)) {
51+
$index = array_search($processor, $this->processors[$mimeType]);
52+
unset($this->processors[$mimeType][$index]);
53+
}
54+
}
55+
3756
function clear()
3857
{
3958
$this->processors = array();

tests/Pipe/Test/ConfigTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Pipe\Test;
4+
5+
use Pipe\Config;
6+
7+
class ConfigTest extends \PHPUnit_Framework_TestCase
8+
{
9+
function testCreateEnvironmentWithCompressors()
10+
{
11+
$config = new Config(array(
12+
'js_compressor' => 'uglify_js',
13+
'css_compressor' => 'yuglify_css'
14+
));
15+
16+
$env = $config->createEnvironment();
17+
18+
$this->assertTrue($env->bundleProcessors->isRegistered(
19+
$env->contentType('.js'), $env->compressors['uglify_js']
20+
));
21+
22+
$this->assertTrue($env->bundleProcessors->isRegistered(
23+
$env->contentType('.css'), $env->compressors['yuglify_css']
24+
));
25+
}
26+
}

0 commit comments

Comments
 (0)