diff --git a/composer.json b/composer.json index 01ae6f5..739fdeb 100644 --- a/composer.json +++ b/composer.json @@ -21,11 +21,8 @@ "satooshi/php-coveralls": "dev-master" }, "require": { - "php": ">=5.4", - "devize/closure-compiler": "dev-master", - "joshtronic/php-ayah": "dev-master", - "leafo/lessphp": "dev-master", - "leafo/scssphp": "dev-master" + "php": ">=5.4", + "joshtronic/php-ayah": "dev-master" }, "autoload": { "classmap": ["src/"] diff --git a/composer.lock b/composer.lock index 9b1a001..7940dbf 100644 --- a/composer.lock +++ b/composer.lock @@ -3,45 +3,8 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "b9d7347bc0da996e7ed52a308b336d36", + "hash": "640eef08d54f04334875c0743919a751", "packages": [ - { - "name": "devize/closure-compiler", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/devize/closure-compiler.git", - "reference": "adf9a5bdef3c404e3066ab095bd0fae0368aa814" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/devize/closure-compiler/zipball/adf9a5bdef3c404e3066ab095bd0fae0368aa814", - "reference": "adf9a5bdef3c404e3066ab095bd0fae0368aa814", - "shasum": "" - }, - "require-dev": { - "mikey179/vfsstream": "*", - "phpunit/phpunit": "3.7.*" - }, - "type": "library", - "autoload": { - "classmap": [ - "src" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Peter Breuls", - "email": "breuls@devize.nl" - } - ], - "description": "Google's Closure Compiler for use in PHP-based projects", - "time": "2013-08-14 11:53:34" - }, { "name": "joshtronic/php-ayah", "version": "dev-master", @@ -80,102 +43,6 @@ "source": "https://github.com/joshtronic/php-ayah/tree/master" }, "time": "2014-07-27 22:09:32" - }, - { - "name": "leafo/lessphp", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/leafo/lessphp.git", - "reference": "011afcca8e6f1000a6e789921ba805fa578271a3" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/leafo/lessphp/zipball/011afcca8e6f1000a6e789921ba805fa578271a3", - "reference": "011afcca8e6f1000a6e789921ba805fa578271a3", - "shasum": "" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "0.4.x-dev" - } - }, - "autoload": { - "classmap": [ - "lessc.inc.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT", - "GPL-3.0" - ], - "authors": [ - { - "name": "Leaf Corcoran", - "email": "leafot@gmail.com", - "homepage": "http://leafo.net" - } - ], - "description": "lessphp is a compiler for LESS written in PHP.", - "homepage": "http://leafo.net/lessphp/", - "time": "2014-04-27 02:56:30" - }, - { - "name": "leafo/scssphp", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/leafo/scssphp.git", - "reference": "8c08da585537e97efd528c7d278463d2b9396371" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/leafo/scssphp/zipball/8c08da585537e97efd528c7d278463d2b9396371", - "reference": "8c08da585537e97efd528c7d278463d2b9396371", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "require-dev": { - "phpunit/phpunit": "3.7.*" - }, - "bin": [ - "bin/pscss" - ], - "type": "library", - "autoload": { - "classmap": [ - "classmap.php" - ], - "psr-4": { - "Leafo\\ScssPhp\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT", - "GPL-3.0" - ], - "authors": [ - { - "name": "Leaf Corcoran", - "email": "leafot@gmail.com", - "homepage": "http://leafo.net" - } - ], - "description": "scssphp is a compiler for SCSS written in PHP.", - "homepage": "http://leafo.net/scssphp/", - "keywords": [ - "css", - "less", - "sass", - "scss", - "stylesheet" - ], - "time": "2014-08-12 22:41:53" } ], "packages-dev": [ @@ -727,10 +594,7 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": { - "devize/closure-compiler": 20, "joshtronic/php-ayah": 20, - "leafo/lessphp": 20, - "leafo/scssphp": 20, "satooshi/php-coveralls": 20 }, "platform": { diff --git a/src/classes/Dynamic.php b/src/classes/Dynamic.php index fa974d6..2880abb 100644 --- a/src/classes/Dynamic.php +++ b/src/classes/Dynamic.php @@ -95,172 +95,5 @@ class Dynamic extends Object return $reference; } - - /** - * Generate Stylesheet Reference - * - * Attempts to minify the stylesheet and then returns the reference URI for - * the file, minified or not. Supports LESS and SASS, pass it a .less file - * or a .scss file instead and it will be compiled before minification. - * - * @param string $reference URI reference of the Stylesheet - * @return string URI reference reference with dynamic content - * @url http://lesscss.org - * @url http://sass-lang.com - */ - public function css($original_reference) - { - $less = false; - $sass = false; - - // Injects .min into the filename - $parts = explode('.', $original_reference); - - if (count($parts) == 1) - { - throw new Exception('Filename must have an extension (e.g. /path/to/file.css)'); - } - else - { - end($parts); - - switch (current($parts)) - { - case 'less': - $less = true; - $parts[key($parts)] = 'css'; - break; - - case 'scss': - $sass = true; - $parts[key($parts)] = 'css'; - break; - } - - $parts[key($parts)] = 'min.' . current($parts); - $minified_reference = implode('.', $parts); - } - - $original_filename = '.' . $original_reference; - $minified_filename = '.' . $minified_reference; - - $path = dirname($original_filename); - - if (file_exists($original_filename)) - { - $reference = $original_reference; - - if ($this->config->pickles['minify'] === true) - { - // Compiles LESS & SASS to CSS before minifying - if ($less || $sass) - { - $compiled_filename = str_replace('.min', '', $minified_filename); - - if ($less) - { - $less = new lessc(); - $less->compileFile($original_filename, $compiled_filename); - } - elseif ($sass) - { - $scss = new Leafo\ScssPhp\Compiler(); - - file_put_contents( - $compiled_filename, - $scss->compile(file_get_contents($original_filename)) - ); - } - - $original_filename = $compiled_filename; - } - - // Minifies CSS with a few basic character replacements. - $stylesheet = file_get_contents($original_filename); - $stylesheet = str_replace( - ["\t", "\n", ', ', ' {', ': ', ';}', '{ ', '; '], - ['', '', ',', '{', ':', '}', '{', ';'], - $stylesheet - ); - $stylesheet = preg_replace('/\/\*.+?\*\//', '', $stylesheet); - file_put_contents($minified_filename, $stylesheet); - - $reference = $minified_reference; - } - elseif (file_exists($minified_filename)) - { - $reference = $minified_reference; - } - - $reference = $this->reference($reference); - } - else - { - throw new Exception('Supplied reference does not exist'); - } - - return $reference; - } - - /** - * Generate Javascript Reference - * - * Attempts to minify the source with Google's Closure compiler, and then - * returns the reference URI for the file, minified or not. - * - * @link http://code.google.com/closure/compiler/ - * @param string $reference URI reference of the Javascript file - * @return string URI reference reference with dynamic content - */ - public function js($original_reference) - { - // Injects .min into the filename - $parts = explode('.', $original_reference); - - if (count($parts) == 1) - { - throw new Exception('Filename must have an extension (e.g. /path/to/file.js)'); - } - else - { - end($parts); - $parts[key($parts)] = 'min.' . current($parts); - $minified_reference = implode('.', $parts); - } - - $original_filename = '.' . $original_reference; - $minified_filename = '.' . $minified_reference; - - $path = dirname($original_filename); - - if (file_exists($original_filename)) - { - $reference = $original_reference; - - if ($this->config->pickles['minify'] === true) - { - $compiler = new Devize\ClosureCompiler\ClosureCompiler(); - $compiler->setSourceBaseDir(dirname($original_filename)); - $compiler->setTargetBaseDir(dirname($minified_filename)); - $compiler->addSourceFile(basename($original_filename)); - $compiler->setTargetFile(basename($minified_filename)); - $compiler->compile(); - - $reference = $minified_reference; - } - elseif (file_exists($minified_filename)) - { - $reference = $minified_reference; - } - - $reference = $this->reference($reference); - } - else - { - throw new Exception('Supplied reference does not exist'); - } - - return $reference; - } } diff --git a/tests/classes/DynamicTest.php b/tests/classes/DynamicTest.php index e749277..52c8ff9 100644 --- a/tests/classes/DynamicTest.php +++ b/tests/classes/DynamicTest.php @@ -116,121 +116,5 @@ JS; $this->dynamic->reference('/images/image.png?foo=bar' )); } - - /** - * @expectedException Exception - * @expectedExceptionMessage Filename must have an extension (e.g. /path/to/file.css) - */ - public function testCSSMissingExtension() - { - $this->dynamic->css('/css/invalid'); - } - - public function testCSSWithoutMinify() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = false; - - $this->assertRegExp('/^\/css\/stylesheet\.\d{10}\.css$/', $this->dynamic->css('/css/stylesheet.css')); - } - - public function testCSSWithoutMinifyFileMinifiedFileExists() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = false; - - touch('/tmp/pickles-fs/public/css/stylesheet.min.css'); - - $this->assertRegExp('/^\/css\/stylesheet\.min\.\d{10}\.css$/', $this->dynamic->css('/css/stylesheet.css')); - - unlink('/tmp/pickles-fs/public/css/stylesheet.min.css'); - } - - public function testCSSWithMinify() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = true; - - $this->assertRegExp('/^\/css\/stylesheet\.min\.\d{10}\.css$/', $this->dynamic->css('/css/stylesheet.css')); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Supplied reference does not exist - */ - public function testCSSReferenceDoesNotExist() - { - $this->dynamic->css('/css/missing.css'); - } - - public function testLESSWithoutMinify() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = false; - - $this->assertRegExp('/^\/css\/stylesheet\.\d{10}\.less$/', $this->dynamic->css('/css/stylesheet.less')); - } - - public function testLESSWithMinify() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = true; - - $this->assertRegExp('/^\/css\/stylesheet\.min\.\d{10}\.css$/', $this->dynamic->css('/css/stylesheet.less')); - } - - public function testSCSSWithoutMinify() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = false; - - $this->assertRegExp('/^\/css\/stylesheet\.\d{10}\.scss$/', $this->dynamic->css('/css/stylesheet.scss')); - } - - public function testSCSSWithMinify() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = true; - - $this->assertRegExp('/^\/css\/stylesheet\.min\.\d{10}\.css$/', $this->dynamic->css('/css/stylesheet.scss')); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Filename must have an extension (e.g. /path/to/file.js) - */ - public function testJSMissingExtension() - { - $this->dynamic->js('/js/invalid'); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Supplied reference does not exist - */ - public function testJSReferenceDoesNotExist() - { - $this->dynamic->js('/js/missing.js'); - } - - public function testJS() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = true; - - $this->assertRegExp('/^\/js\/script\.min\.\d{10}\.js$/', $this->dynamic->js('/js/script.js')); - } - - public function testJSWithoutMinifyFileMinifiedFileExists() - { - $config = Config::getInstance(); - $config->data['pickles']['minify'] = false; - - touch('/tmp/pickles-fs/public/js/script.min.css'); - - $this->assertRegExp('/^\/js\/script\.min\.\d{10}\.js$/', $this->dynamic->js('/js/script.js')); - - unlink('/tmp/pickles-fs/public/js/script.min.css'); - } }