Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Inpassor committed Jul 29, 2016
1 parent d51e633 commit 18ae892
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 24 deletions.
100 changes: 77 additions & 23 deletions EAssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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'.
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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__);
Expand All @@ -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);
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
...
Expand All @@ -29,6 +38,7 @@ INSTALLATION
...
),
```

See code of EAssetManager.php to read description of public properties.

Expand All @@ -42,17 +52,23 @@ USAGE

Just publish .less file with assetManager like that:

```
$css = CHtml::asset(Yii::app()->basePath.'/vendors/bootstrap/less/bootstrap.less');
```

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');
```

0 comments on commit 18ae892

Please sign in to comment.