-
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
Showing
2 changed files
with
94 additions
and
24 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 |
---|---|---|
|
@@ -12,9 +12,25 @@ | |
*/ | ||
/* | ||
EAssetManager class file. | ||
Extended Asset Manager | ||
Compiles .less file(s) on-the-fly and publishes output .css file | ||
Author: Inpassor <[email protected]> . | ||
Link: https://github.com/Inpassor/yii-EAssetManager . | ||
Version: 0.3 (2013.10.24) . | ||
INSTALLATION | ||
1. Copy EAssetManager.php to /protected/extensions/ directory | ||
Install with composer: | ||
composer require inpassor/yii-eassetmanager | ||
Manual install: | ||
1. Copy EAssetManager.php to /protected/vendor/ directory | ||
2. Add or replace the assetManager component in /protected/config/main.php like that: | ||
'components'=>array( | ||
|
@@ -35,7 +51,7 @@ | |
See code of EAssetManager.php to read description of public properties. | ||
3. CHMOD 'lessCompiledPath' directory to 777 in order to create new files there by EAssetManager. | ||
3. CHMOD 'lessCompiledPath' directory to 0777 in order to create new files there by EAssetManager. | ||
4. Optional: enable Yii caching. Otherwise, EAssetManager will create (or use existing) directory /protected/runtime/cache/ and store cache data there. | ||
You can override this path by setting public property 'cachePath'. | ||
|
@@ -61,33 +77,45 @@ | |
*/ | ||
|
||
|
||
class EAssetManager extends CAssetManager | ||
{ | ||
|
||
// default cache path for EAssetManager. It will be used if Yii caching is not enabled. | ||
/** | ||
* @var string default cache path for EAssetManager. It will be used if Yii caching is not enabled. | ||
*/ | ||
public $cachePath = null; | ||
|
||
// path to store compiled css files | ||
// defaults to 'application.assets.css' | ||
// note that this path must be writtable by script (CHMOD 777) | ||
/** | ||
* @var string path to store compiled css files. Defaults to 'application.assets.css'. | ||
* Note that this path must be writtable by script (CHMOD 777) | ||
*/ | ||
public $lessCompiledPath = null; | ||
|
||
// compiled output formatter | ||
// accepted values: 'lessjs' , 'compressed' , 'classic' | ||
// defaults to 'lessjs' | ||
// read http://leafo.net/lessphp/docs/#output_formatting for details | ||
/** | ||
* @var string compiled output formatter. Accepted values: 'lessjs' , 'compressed' , 'classic' . Defaults to 'lessjs'. | ||
* Read http://leafo.net/lessphp/docs/#output_formatting for details. | ||
*/ | ||
public $lessFormatter = 'lessjs'; | ||
|
||
// passing in true will cause the input to always be recompiled | ||
/** | ||
* @var bool passing in true will cause the input to always be recompiled. | ||
*/ | ||
public $lessForceCompile = false; | ||
|
||
// if set to false, .less to .css compilation will be done ONLY if output .css file not found | ||
// otherwise existing .css file will be used | ||
/** | ||
* @var bool if set to false, .less to .css compilation will be done ONLY if output .css file not found. | ||
* Otherwise existing .css file will be used | ||
*/ | ||
public $lessCompile = true; | ||
|
||
/** | ||
* @var lessc | ||
*/ | ||
protected $_lessc = null; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
if (!Yii::app()->cache) { | ||
|
@@ -99,7 +127,13 @@ public function init() | |
parent::init(); | ||
} | ||
|
||
|
||
/** | ||
* @param string $path | ||
* @param bool $hashByName | ||
* @param int $level | ||
* @param null $forceCopy | ||
* @return mixed | ||
*/ | ||
public function publish($path, $hashByName = false, $level = -1, $forceCopy = null) | ||
{ | ||
if (($src = realpath($path)) !== false) { | ||
|
@@ -112,7 +146,10 @@ public function publish($path, $hashByName = false, $level = -1, $forceCopy = nu | |
return parent::publish($path, $hashByName, $level, $forceCopy); | ||
} | ||
|
||
|
||
/** | ||
* @param string $src | ||
* @return string | ||
*/ | ||
public function lessCompile($src) | ||
{ | ||
$path = $this->lessCompiledPath . DIRECTORY_SEPARATOR . basename($src, '.less') . '.css'; | ||
|
@@ -143,8 +180,12 @@ public function lessCompile($src) | |
return $path; | ||
} | ||
|
||
|
||
private function _chkDir($dir, $create) | ||
/** | ||
* @param string $dir | ||
* @param bool $create | ||
* @return bool | ||
*/ | ||
protected function _chkDir($dir, $create) | ||
{ | ||
if (($alias = Yii::getPathOfAlias($dir))) { | ||
return $alias; | ||
|
@@ -158,7 +199,13 @@ private function _chkDir($dir, $create) | |
return false; | ||
} | ||
|
||
private function _getPath($path, $default = null, $createDir = false) | ||
/** | ||
* @param string $path | ||
* @param null $default | ||
* @param bool $createDir | ||
* @return bool | ||
*/ | ||
protected function _getPath($path, $default = null, $createDir = false) | ||
{ | ||
if ($default === null) { | ||
$default = dirname(__FILE__); | ||
|
@@ -172,17 +219,24 @@ private function _getPath($path, $default = null, $createDir = false) | |
return $ret; | ||
} | ||
|
||
|
||
private function _cacheSet($name, $value) | ||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
* @return int | ||
*/ | ||
protected function _cacheSet($name, $value) | ||
{ | ||
if (Yii::app()->cache) { | ||
return Yii::app()->cache->set($name, $value); | ||
} | ||
return file_put_contents($this->cachePath . DIRECTORY_SEPARATOR . md5($name) . '.bin', serialize($value), LOCK_EX); | ||
} | ||
|
||
|
||
private function _cacheGet($name) | ||
/** | ||
* @param string $name | ||
* @return bool|mixed | ||
*/ | ||
protected function _cacheGet($name) | ||
{ | ||
if (Yii::app()->cache) { | ||
return Yii::app()->cache->get($name); | ||
|
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