diff --git a/EAssetManager.php b/EAssetManager.php index 4c47ec9..4aecabe 100644 --- a/EAssetManager.php +++ b/EAssetManager.php @@ -12,9 +12,25 @@ */ /* +EAssetManager class file. + +Extended Asset Manager +Compiles .less file(s) on-the-fly and publishes output .css file + +Author: Inpassor . +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,8 +219,12 @@ 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); @@ -181,8 +232,11 @@ private function _cacheSet($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); diff --git a/README.md b/README.md index 6dd2cfb..ac91153 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,18 @@ 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( ... @@ -29,6 +38,7 @@ INSTALLATION ... ), +``` See code of EAssetManager.php to read description of public properties. @@ -42,7 +52,9 @@ USAGE Just publish .less file with assetManager like that: +``` $css = CHtml::asset(Yii::app()->basePath.'/vendors/bootstrap/less/bootstrap.less'); +``` That's all :) @@ -50,9 +62,13 @@ That's all :) Also it might be useful to pre-compile .less files. For example, to make command which compiles .less files in background. In this case you can use "lessCompile" method: +``` Yii::app()->assetManager->lessCompile(Yii::app()->basePath.'/vendors/bootstrap/less/bootstrap.less'); +``` Output .css file will be stored under 'lessCompiledPath' directory. And then add already compiled file in your application: +``` $css = CHtml::asset(Yii::app()->assetManager->lessCompiledPath.'/bootstrap.css'); +```