Asset parser (AssertConverter) for Yii2
PHP
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Converter.php
Less.php
Parser.php
Sass.php
composer.json
readme.md

readme.md

Only for YII2 with the new Asset Manager, convert Less and Sass files to CSS without external tools and executable. The sass and less files are converted with PHP librairies It replace the yii\web\AssetConverter who use external tools. The Less and Sass file are converted with time source files dependency.

##Requirements

YII 2.0

##Installation and configuration

installation

This package is install by composer, in your composer.json add :

   "require": {
       ....
       "athos99/assetparser": "*"
       .....
       }

configuration

Modify assetManager components definition in your YII configuration file {app}/config/web.php

[php]
        'assetManager' => array(
//            'bundles' => require(__DIR__ . '/assets.php'),
            'converter'=>array(
                'class'=>'athos99\assetparser\Converter',
                'force'=>false
            )
        ),

'Force'=>true : If you want convert your sass each time without time dependency

The sass files with extension .sass are converted to a .css file The less files with extension .less are converted to a .css file The scss file with extension .scss are converted to a .css file

Example of assets config file /protected/config/assets.php

[php]
<?php

return array(
	'app' => array(
		'basePath' => '@wwwroot',
		'baseUrl' => '@www',
		'css' => array(
			'css/bootstrap.min.css',
			'css/bootstrap-responsive.min.css',
			'css/site.css',
            'css/less_style.less',
            'css/sass_style.sass',
		),
		'js' => array(

		),
		'depends' => array(
			'yii',
		),
	),
);

##Resources

##Q & A It's possible to chose a another folder that {app}/protected/extensions/assetparser ? Yes It Is ! It's possible to adapt assetparser to your spcific environement, it's little bit complex (You need version 1.0.1)

For exeample : Yii Framework : C:\www\xxx\vendor\yiisoft\yii2\framework Web application: C:\www\xxx\web\index.php assertparser extension: C:\www\xxx\vendor\yii-ext\assetparser Config file : C:\www\xxx\app\config\main.php

  1. Adapt the configuration file (main.php) In my example : C:\www\xxx\app\config\main.php
[php]
<?php

'components' => array(

....

		'assetManager' => array(
            'bundles' => require(__DIR__ . '/assets.php'),
            'converter'=>array(
                'class'=>'app\extensions\assetparser\Converter',
                'force'=>false,
                 'parsers' => array(
                    'sass' => array( // file extension to parse
                        'class' => 'app\extensions\assetparser\Sass',
                        'output' => 'css', // parsed output file type
                        'options' => array(
                            'cachePath' => '@app/runtime/cache/sass-parser' // optional options
                        ),
                    ),
                    'scss' => array( // file extension to parse
                        'class' => 'app\extensions\assetparser\Sass',
                        'output' => 'css', // parsed output file type
                        'options' => array() // optional options
                    ),
                    'less' => array( // file extension to parse
                        'class' => 'app\extensions\assetparser\Less',
                        'output' => 'css', // parsed output file type
                        'options' => array(
                            'auto' => true, // optional options
                            'lessParserPath' => __DIR__ . '/../../vendor/yii-ext/assetparser/vendors/lessphp/lessc.inc.php'
                        )
                    )
                )
            )
        ),
	),

.....

You need to update lessParserPath defintion to you specific environement 'lessParserPath' => ' DIR . '/../../vendor/yii-ext/assetparser/vendors/lessphp/lessc.inc.php'

  1. You need declare some classMap to YII, because your folder filepath are not standart with YII2 namespace

There are two possibilities 2a) Modifie your index.php ( C:\www\xxx\web\index.php)

defined('YII_DEBUG') or define('YII_DEBUG', true);

[php]
<?php
require(__DIR__ . '/../vendor/yiisoft/yii2/framework/yii.php');

Yii::$classMap['app\extensions\assetparser\Converter'] =__DIR__ . '/../vendor/yii-ext/assetparser/Converter.php';
Yii::$classMap['app\extensions\assetparser\Parser'] =__DIR__ . '/../vendor/yii-ext/assetparser/Parser.php';
Yii::$classMap['app\extensions\assetparser\Less'] =__DIR__ . '/../vendor/yii-ext/assetparser/Less.php';
Yii::$classMap['app\extensions\assetparser\Sass'] =__DIR__ . '/../vendor/yii-ext/assetparser/Sass.php';

$config = require(__DIR__ . '/../app/config/main.php');
$application = new yii\web\Application($config);
$application->run();

2b) Modifie your main.php ( C:\www\xxx\app\config\main.php)

[php]
<?php
Yii::$classMap['app\extensions\assetparser\Converter'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Converter.php';
Yii::$classMap['app\extensions\assetparser\Parser'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Parser.php';
Yii::$classMap['app\extensions\assetparser\Less'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Less.php';
Yii::$classMap['app\extensions\assetparser\Sass'] =__DIR__ . '/../../vendor/yii-ext/assetparser/Sass.php';

return array(
    'id' => 'hello',
    'basePath' => dirname(__DIR__),
    'components' => array(
.....