-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
0 parents
commit 811367e
Showing
15 changed files
with
424 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,14 @@ | ||
--- | ||
engines: | ||
duplication: | ||
enabled: true | ||
config: | ||
languages: | ||
- php | ||
fixme: | ||
enabled: true | ||
phpmd: | ||
enabled: true | ||
ratings: | ||
paths: | ||
- "**.php" |
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,4 @@ | ||
composer.phar | ||
composer.lock | ||
/dest/ | ||
/vendor/ |
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,7 @@ | ||
enabled: | ||
- concat_with_spaces | ||
|
||
disabled: | ||
- short_array_syntax | ||
- concat_without_spaces | ||
- single_quote |
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,26 @@ | ||
language: php | ||
|
||
php: | ||
- '5.3' | ||
- '5.4' | ||
- '5.5' | ||
- '5.6' | ||
- '7.0' | ||
- hhvm | ||
|
||
before_script: | ||
- travis_retry composer self-update | ||
- travis_retry composer install --no-interaction --prefer-source --dev | ||
|
||
script: | ||
- vendor/bin/phpunit --verbose --coverage-text --coverage-clover=coverage.xml | ||
|
||
after_script: | ||
- vendor/bin/test-reporter --coverage-report coverage.xml | ||
|
||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) | ||
|
||
addons: | ||
code_climate: | ||
repo_token: 6b766bad1c5501531244a78fbd1096a8192cab07ade1b12c1bd49c056e404bef |
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,47 @@ | ||
# uglify | ||
[](https://packagist.org/packages/nodejs-php-fallback/uglify) | ||
[](https://travis-ci.org/kylekatarnls/uglify) | ||
[](https://styleci.io/repos/64242033) | ||
[](https://codecov.io/github/kylekatarnls/uglify?branch=master) | ||
[](https://codeclimate.com/github/kylekatarnls/uglify) | ||
|
||
Simple PHP class to minify both your javascript and css the best existing way (uglify-js for JS, clean-css for CSS) and if node is not available, PHP fallbacks are used instead. | ||
|
||
## Usage | ||
|
||
First you need [composer](https://getcomposer.org/) if you have not already. Then get the package with ```composer require nodejs-php-fallback/uglify``` then require the composer autload in your PHP file if it's not already: | ||
```php | ||
<?php | ||
|
||
use NodejsPhpFallback\Uglify; | ||
|
||
// Require the composer autload in your PHP file if it's not already. | ||
// You do not need to if you use a framework with composer like Symfony, Laravel, etc. | ||
require 'vendor/autoload.php'; | ||
|
||
$uglify = new Uglify(array( | ||
'path/to/my-first-file.js', | ||
'path/to/my-second-file.js', | ||
)); | ||
$uglify->add('path/to/my-thrid-file.js'); | ||
|
||
// Output to a file: | ||
$uglify->write('path/to/destination.min.js'); | ||
|
||
// Output to the browser: | ||
header('Content-type: text/javascript'); | ||
echo $uglify; | ||
``` | ||
|
||
**Uglify** will use js minification by default. If the first source path end with *.css* or you use ```->write()``` with a path ending with *.css*, it will switch to CSS mode. Else you can switch manually or get explicitly JS/CSS minified: | ||
|
||
```php | ||
$uglify->jsMode(); | ||
echo $uglify; // display minified javascript | ||
$uglify->cssMode(); | ||
echo $uglify; // display minified css | ||
|
||
// or | ||
echo $uglify->getMinifiedJs(); // display minified javascript | ||
echo $uglify->getMinifiedCss(); // display minified css | ||
``` |
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,32 @@ | ||
{ | ||
"name": "nodejs-php-fallback/uglify", | ||
"description": "PHP wrapper to execute uglify-js/clean-css node package or fallback to PHP alternatives", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Kyle", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"nodejs-php-fallback/nodejs-php-fallback": "^1.2.3", | ||
"natxet/CssMin": "*", | ||
"gkralik/php-uglifyjs": "*" | ||
}, | ||
"extra": { | ||
"npm": [ | ||
"uglify-js", | ||
"clean-css" | ||
] | ||
}, | ||
"require-dev": { | ||
"composer/composer": "^1.2", | ||
"phpunit/phpunit": ">=4.0", | ||
"codeclimate/php-test-reporter": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"NodejsPhpFallback\\": "src/" | ||
} | ||
} | ||
} |
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,19 @@ | ||
<phpunit | ||
bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Features"> | ||
<file>tests/UglifyTest.php</file> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,113 @@ | ||
<?php | ||
|
||
namespace NodejsPhpFallback; | ||
|
||
use CoffeeScript\Compiler as PhpUglifyEngine; | ||
use GK\JavascriptPacker as Packer; | ||
|
||
class Uglify extends Wrapper | ||
{ | ||
protected $css = false; | ||
protected $concat = array(); | ||
|
||
protected $programs = array( | ||
'js' => 'uglify', | ||
'css' => 'clean', | ||
); | ||
|
||
public function __construct($file) | ||
{ | ||
$files = (array) $file; | ||
$this->concat = array_slice($files, 1); | ||
$this->setModeFromPath($files[0]); | ||
parent::__construct($files[0]); | ||
} | ||
|
||
public function setModeFromPath($path) | ||
{ | ||
$this->css = strtolower(substr($path, -4)) === '.css'; | ||
|
||
return $this; | ||
} | ||
|
||
public function add($file) | ||
{ | ||
$this->concat[] = $file; | ||
} | ||
|
||
public function getSource() | ||
{ | ||
$source = trim(parent::getSource()); | ||
foreach ($this->concat as $file) { | ||
if ($this->getMode() === 'js') { | ||
$source = rtrim($source, '; ') . ';'; | ||
} | ||
$source .= trim(file_get_contents($file)); | ||
} | ||
|
||
return $source; | ||
} | ||
|
||
public function jsMode() | ||
{ | ||
$this->css = false; | ||
|
||
return $this; | ||
} | ||
|
||
public function cssMode() | ||
{ | ||
$this->css = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function getMode() | ||
{ | ||
return $this->css ? 'css' : 'js'; | ||
} | ||
|
||
public function getMinifiedCss() | ||
{ | ||
return $this->cssMode()->getResult(); | ||
} | ||
|
||
public function getMinifiedJs() | ||
{ | ||
return $this->jsMode()->getResult(); | ||
} | ||
|
||
public function write($path) | ||
{ | ||
$this->setModeFromPath($path); | ||
|
||
return parent::write($path); | ||
} | ||
|
||
public function compile() | ||
{ | ||
$language = $this->getMode(); | ||
$program = $this->programs[$language]; | ||
$name = $this->path ? basename($this->path) : null; | ||
|
||
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $name; | ||
file_put_contents($path, $this->getSource()); | ||
|
||
return $this->execModuleScript( | ||
$program . '-' . $language, | ||
'bin/' . $program . $language, | ||
escapeshellarg($path) | ||
); | ||
} | ||
|
||
public function fallback() | ||
{ | ||
if ($this->getMode() === 'js') { | ||
$packer = new Packer($this->getSource()); | ||
|
||
return $packer->pack(); | ||
} | ||
|
||
return CssMin::minify($this->getSource()); | ||
} | ||
} |
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,102 @@ | ||
<?php | ||
|
||
use NodejsPhpFallback\Uglify; | ||
|
||
class UglifyTest extends PHPUnit_Framework_TestCase | ||
{ | ||
protected function getTempJs() | ||
{ | ||
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test.min.js'; | ||
} | ||
|
||
protected function getTempCss() | ||
{ | ||
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test.min.css'; | ||
} | ||
|
||
protected function writeJs($content) | ||
{ | ||
return file_put_contents($this->getTempJs(), $content); | ||
} | ||
|
||
protected function writeCss($content) | ||
{ | ||
return file_put_contents($this->getTempCss(), $content); | ||
} | ||
|
||
protected function getConsole() | ||
{ | ||
$file = $this->getTempJs(); | ||
$output = trim(str_replace("\r", '', shell_exec('node ' . escapeshellarg($file)))); | ||
unlink($file); | ||
|
||
return $output; | ||
} | ||
|
||
protected function getExpectedJs() | ||
{ | ||
return trim(str_replace("\r", '', file_get_contents(__DIR__ . '/test.log'))); | ||
} | ||
|
||
protected function getExpectedCss() | ||
{ | ||
return trim(str_replace("\r", '', file_get_contents(__DIR__ . '/test.min.css'))); | ||
} | ||
|
||
public function testMode() | ||
{ | ||
$uglify = new Uglify(__DIR__ . '/test.css'); | ||
$this->assertSame('css', $uglify->getMode()); | ||
$uglify = new Uglify(__DIR__ . '/test.js'); | ||
$this->assertSame('js', $uglify->getMode()); | ||
$uglify->cssMode(); | ||
$this->assertSame('css', $uglify->getMode()); | ||
$uglify->jsMode(); | ||
$this->assertSame('js', $uglify->getMode()); | ||
} | ||
|
||
public function testCompile() | ||
{ | ||
$expected = $this->getExpectedJs(); | ||
$uglify = new Uglify(array( | ||
__DIR__ . '/test.js', | ||
__DIR__ . '/test2.js', | ||
)); | ||
$this->writeJs($uglify); | ||
$log = $this->getConsole(); | ||
|
||
$this->assertSame($expected, $log, 'Uglify should render with node.'); | ||
} | ||
|
||
public function testWrite() | ||
{ | ||
$expected = $this->getExpectedJs(); | ||
$uglify = new Uglify(__DIR__ . '/test.js'); | ||
$uglify->add(__DIR__ . '/test2.js'); | ||
$this->writeJs($uglify->getMinifiedJs()); | ||
$log = $this->getConsole(); | ||
|
||
$this->assertSame($expected, $log, 'Uglify should render with node.'); | ||
} | ||
|
||
public function testCompileCss() | ||
{ | ||
$expected = $this->getExpectedCss(); | ||
$uglify = new Uglify(__DIR__ . '/test.css'); | ||
$uglify->add(__DIR__ . '/test2.css'); | ||
$css = $uglify->getMinifiedCss(); | ||
|
||
$this->assertSame($expected, $css, 'Uglify should render with node.'); | ||
} | ||
|
||
public function testFallback() | ||
{ | ||
$uglify = new Uglify(array(__DIR__ . '/test.js')); | ||
$uglify->add(__DIR__ . '/test2.js'); | ||
$expected = $this->getExpectedJs(); | ||
$this->writeJs($uglify->fallback()); | ||
$log = $this->getConsole(); | ||
|
||
$this->assertSame($expected, $log, 'Uglify should render without node.'); | ||
} | ||
} |
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,3 @@ | ||
body { | ||
color: #f6d; | ||
} |
Oops, something went wrong.