From faa26a91b70a890acf45879efd04d28cf1bb8922 Mon Sep 17 00:00:00 2001 From: Ed Gifford Date: Mon, 11 May 2015 20:36:45 -0400 Subject: [PATCH] Starting to add the ability to pass parameters into Assetic classes. In this commit, adding the ability to setup addImportPath for the ScssphpFilter. --- src/AssetsMinify.php | 18 ++++++++++++++++-- src/AssetsMinify/Assets/Css.php | 6 ++++-- src/AssetsMinify/Assets/Css/Css.php | 3 ++- src/AssetsMinify/Assets/Css/Less.php | 3 ++- src/AssetsMinify/Assets/Css/Sass.php | 3 ++- src/AssetsMinify/Assets/Css/Scss.php | 24 ++++++++++++++++++++++-- src/AssetsMinify/Assets/Js.php | 5 +++-- src/AssetsMinify/Assets/Js/Coffee.php | 3 ++- src/AssetsMinify/Assets/Js/Js.php | 3 ++- src/AssetsMinify/Init.php | 18 +++++++++++++++--- 10 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/AssetsMinify.php b/src/AssetsMinify.php index 48acdd1..6dee11e 100644 --- a/src/AssetsMinify.php +++ b/src/AssetsMinify.php @@ -9,12 +9,19 @@ class AssetsMinify extends AssetsMinify\Pattern\Singleton { + /** + * @var \AssetsMinify\Init + */ + private $initObject = null; + /** * Constructor */ protected function __construct() { - if ( !is_admin() ) - return new AssetsMinify\Init; + if ( !is_admin() ) { + $this->initObject = new AssetsMinify\Init; + return; + } add_action( 'plugins_loaded', array( $this, 'loadAdmin' ) ); } @@ -25,4 +32,11 @@ protected function __construct() { public function loadAdmin() { new AssetsMinify\Admin; } + + /** + * Provides access to the central Init object. + */ + public function getInitObject() { + return $this->initObject; + } } \ No newline at end of file diff --git a/src/AssetsMinify/Assets/Css.php b/src/AssetsMinify/Assets/Css.php index fa4401e..26245d5 100644 --- a/src/AssetsMinify/Assets/Css.php +++ b/src/AssetsMinify/Assets/Css.php @@ -81,8 +81,10 @@ public function extract() { /** * Takes all the stylesheets and manages their queue to compress them + * + * @param array $param Keyed array with settings for various Assetic classes. */ - public function generate() { + public function generate($params = null) { $profiler = array( time() ); foreach ( $this->assets as $media => $assets ) { @@ -92,7 +94,7 @@ public function generate() { if ( !$this->cache->fs->has( $cachefile ) ) { $class = "AssetsMinify\\Assets\\Css\\" . ucfirst($ext); - new $class( $content['files'], $cachefile, $this ); + new $class( $content['files'], $cachefile, $this, $params ); } $key = "$media-$ext-am-generated"; diff --git a/src/AssetsMinify/Assets/Css/Css.php b/src/AssetsMinify/Assets/Css/Css.php index cda9bdd..5e41e76 100644 --- a/src/AssetsMinify/Assets/Css/Css.php +++ b/src/AssetsMinify/Assets/Css/Css.php @@ -13,8 +13,9 @@ class Css { * @param array $content The files to save to cache * @param string $cachefile The cache file name * @param object $manager The Factory object + * @param array $param Keyed array with settings for various Assetic classes. */ - public function __construct($content, $cachefile, $manager) { + public function __construct($content, $cachefile, $manager, $params = null) { $manager->cache->fs->set( $cachefile, $manager->createAsset( $content, array( 'CssRewrite' ) )->dump() ); } } \ No newline at end of file diff --git a/src/AssetsMinify/Assets/Css/Less.php b/src/AssetsMinify/Assets/Css/Less.php index ca9db91..6128cc5 100644 --- a/src/AssetsMinify/Assets/Css/Less.php +++ b/src/AssetsMinify/Assets/Css/Less.php @@ -16,8 +16,9 @@ class Less extends \AssetsMinify\Assets\Factory { * @param array $content The files to save to cache * @param string $cachefile The cache file name * @param object $manager The Factory object + * @param array $param Keyed array with settings for various Assetic classes. */ - public function __construct($content, $cachefile, $manager) { + public function __construct($content, $cachefile, $manager, $params = null) { parent::__construct( $this ); $manager->cache->fs->set( $cachefile, $this->createAsset( $content, $this->getFilters() )->dump() ); } diff --git a/src/AssetsMinify/Assets/Css/Sass.php b/src/AssetsMinify/Assets/Css/Sass.php index 9fa836d..dfd4caa 100644 --- a/src/AssetsMinify/Assets/Css/Sass.php +++ b/src/AssetsMinify/Assets/Css/Sass.php @@ -16,8 +16,9 @@ class Sass extends \AssetsMinify\Assets\Factory { * @param array $content The files to save to cache * @param string $cachefile The cache file name * @param object $manager The Factory object + * @param array $param Keyed array with settings for various Assetic classes. */ - public function __construct($content, $cachefile, $manager) { + public function __construct($content, $cachefile, $manager, $params = null) { parent::__construct( $this ); $manager->cache->fs->set( $cachefile, $this->createAsset( $content, $this->getFilters() )->dump() ); } diff --git a/src/AssetsMinify/Assets/Css/Scss.php b/src/AssetsMinify/Assets/Css/Scss.php index 01839b9..a859076 100644 --- a/src/AssetsMinify/Assets/Css/Scss.php +++ b/src/AssetsMinify/Assets/Css/Scss.php @@ -11,6 +11,11 @@ * @author Alessandro Carbone */ class Scss extends \AssetsMinify\Assets\Factory { + /** + * @var array + */ + private $params = null; + /** * Constructor * @@ -18,8 +23,9 @@ class Scss extends \AssetsMinify\Assets\Factory { * @param string $cachefile The cache file name * @param object $manager The Factory object */ - public function __construct($content, $cachefile, $manager) { + public function __construct($content, $cachefile, $manager, $params = null) { $this->manager = $manager; + $this->params = $params; parent::__construct( $this ); $manager->cache->fs->set( $cachefile, $this->createAsset( $content, $this->getFilters() )->dump() ); } @@ -34,7 +40,21 @@ public function setFilters() { $this->setFilter('Compass', $compassInstance); $filter = 'Compass'; } else { - $this->setFilter('Scssphp', new ScssphpFilter); + $scssphpFilter = new ScssphpFilter(); + + if ( is_array($this->params) ) { + if ( ! empty($this->params['addImportPath']) ) { + if ( is_array($this->params['addImportPath'] ) ) { + foreach ( $this->params['addImportPath'] as $value ) { + $scssphpFilter->addImportPath($value); + } + } else { + $scssphpFilter->addImportPath($this->params['addImportPath']); + } + } + } + + $this->setFilter('Scssphp', $scssphpFilter); $filter = 'Scssphp'; } diff --git a/src/AssetsMinify/Assets/Js.php b/src/AssetsMinify/Assets/Js.php index 1dd8cc6..b497c28 100644 --- a/src/AssetsMinify/Assets/Js.php +++ b/src/AssetsMinify/Assets/Js.php @@ -85,8 +85,9 @@ public function extract() { * Takes all the JavaScript and manages the queue to compress them * * @param string $where The page's place to dump the scripts in (header or footer) + * @param array $param Keyed array with settings for various Assetic classes. */ - public function generate($where) { + public function generate($where, $params = null) { $profiler = array( time() ); if ( !isset($this->assets[$where]) ) @@ -98,7 +99,7 @@ public function generate($where) { if ( !$this->cache->fs->has( $cachefile ) ) { $class = "AssetsMinify\\Assets\\Js\\" . ucfirst($ext); - new $class( $content['files'], $cachefile, $this ); + new $class( $content['files'], $cachefile, $this, $params ); } $key = "$ext-am-generated"; diff --git a/src/AssetsMinify/Assets/Js/Coffee.php b/src/AssetsMinify/Assets/Js/Coffee.php index 53b963b..7d282c8 100644 --- a/src/AssetsMinify/Assets/Js/Coffee.php +++ b/src/AssetsMinify/Assets/Js/Coffee.php @@ -15,8 +15,9 @@ class Coffee extends \AssetsMinify\Assets\Factory { * @param array $content The files to save to cache * @param string $cachefile The cache file name * @param object $manager The Factory object + * @param array $param Keyed array with settings for various Assetic classes. */ - public function __construct($content, $cachefile, $manager) { + public function __construct($content, $cachefile, $manager, $params = null) { parent::__construct( $this ); $manager->cache->fs->set( $cachefile, $this->createAsset( $content, $this->getFilters() )->dump() ); } diff --git a/src/AssetsMinify/Assets/Js/Js.php b/src/AssetsMinify/Assets/Js/Js.php index f7c8664..ec021c0 100644 --- a/src/AssetsMinify/Assets/Js/Js.php +++ b/src/AssetsMinify/Assets/Js/Js.php @@ -13,8 +13,9 @@ class Js { * @param array $content The files to save to cache * @param string $cachefile The cache file name * @param object $manager The Factory object + * @param array $param Keyed array with settings for various Assetic classes. */ - public function __construct($content, $cachefile, $manager) { + public function __construct($content, $cachefile, $manager, $params = null) { $manager->cache->fs->set( $cachefile, $manager->createAsset( $content, $manager->getFilters() )->dump() ); } } \ No newline at end of file diff --git a/src/AssetsMinify/Init.php b/src/AssetsMinify/Init.php index 9a5c7c2..8e8d605 100644 --- a/src/AssetsMinify/Init.php +++ b/src/AssetsMinify/Init.php @@ -17,6 +17,11 @@ class Init { protected $exclusions; + /** + * @var array + */ + private $params = null; + /** * Constructor */ @@ -47,6 +52,13 @@ public function __construct() { add_action( 'wp_footer', array( $this, 'footer' ) ); } + /** + * @param array $param Keyed array with settings for various Assetic classes. + */ + public function setParams($params) { + $this->params = $params; + } + /** * Checks if a file is within the list of resources to exclude * @@ -65,15 +77,15 @@ public function isFileExcluded( $path ) { * Returns header's inclusion for CSS and JS */ public function header() { - $this->css->generate(); - $this->js->generate('header'); + $this->css->generate($this->params); + $this->js->generate('header', $this->params); } /** * Returns footer's inclusion for JS */ public function footer() { - $this->js->generate('footer'); + $this->js->generate('footer', $this->params); if ( $this->cache->isUpdated() ) Log::getInstance()->dumpStorage(); }