Yii2 Asset Convertor - php

I'm trying to use Less with my Yii2 application.
I use the advanced application and would like to convert my .less files in the frontend/web/css using the asset convertor build in with yii2.
'assetManager' => [
'bundles' => [
'yii\bootstrap\BootstrapAsset' => [
'css' => []
],
],
'converter' => [
'class' => 'yii\web\AssetConverter',
'commands' => [
'less' => ['css', 'lessc {from} {to} --no-color'],
],
],
],
The above is in my main.php config file.
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.less',
'css/superhero.less',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
The above is the appAsset file.
but how can I install the less tool in my yii2 installation? I put less-1.7.5.js in the root folder where my yii console bootstrap file is but where do I have to adjust the configuration to convert the less files?
Thx in advance!

What I've just did in my project:
main.php config file:
'assetManager' => [
'converter' => [
'class' => 'yii\web\AssetConverter',
'commands' => [
'less' => ['css', 'nodejs "' . PROTECTED_BASE_PATH .
'/node_modules/less/bin/lessc" {from} {to} --no-color'],
],
],
],
Command line (in the subdirectory, represented by PROTECTED_BASE_PATH above):
$ npm install less
That's all. It works fine after git pull on another machine.
Of course, nodejs itself must be installed globally.
Important note. This is unusual way to keep the nodejs packges in VCS (though I have a reason to do so). Consider use of package.json instead.

To install less compiler (lessc) to your project use composer command:
composer require bower-asset/less
Then add to config:
'assetManager' => [
'converter' => [
'class' => 'yii\web\AssetConverter',
'commands' => [
'less' => ['css', '#bower/less/packages/less/bin/lessc {from} {to} --no-color'],
],
],
],
This config is helpful for shared hosting, when you have no root privileges and can't install lessc globally.

install less converter: Server-Side and Command Line Usage
http://lesscss.org/usage/
and make sure command available as lessc from anywhere i.e. added to PATH

Related

Laravel log file specific to a package

I'm writing a couple of laravel packages and I'm wondering if it is possible to have the package write to a specific log file but only for messages related to the package?
I tried making a logging.php file in the packages/myorg/mypackage/config (below) but it doesn't seem to do anything.
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
]
];
I am using "jeroen-g/laravel-packager" to set up the packages. It appears to manually load the mypackage.config in the ServiceProvider bootForConsole
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
mypackage.'/../config/mypackage.php' => config_path('mypackage.php'),
], 'mypackage.config');
}
I'm not sure how to add custom logging to that though. I'm still learning Laravel and I'm not quite sure what or how the main applications config/logging.php is read so I'm not quite sure how to inject a custom version for an add-on package.
EDIT:
I found a post that suggested using the following in the ServiceManager boot() method:
$this->app->make('config')->set('logging.channels.mychannel', [
/* settings */
]);
I used the package config to set a 'logging' => [ 'channels' => [ 'mychannel' => [ /* settings */ ] ] ] and could then do the same thing as above with:
$this->app->make('config')->set('logging.channels.mychannel', config('mypackage.logging.channels.mychannel');
But that still required something in the code. The next best thing I have found thus far is to change my config/logging.php to config/logging.channels.php and include something like:
return [
'mychannel' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
];
Then in the service provider register() method add:
$this->mergeConfigFrom(__DIR__ . '/../config/logging.channels.php', 'logging.channels');
I tried doing it from the original 'logging.php' with channels array nested in a 'logging' key, but array_merge doesn't appear to merge the nested elements so my channel never showed up in logging.channels.
I'm not sure if this is ideal, however. I'd still like to know if there is a 'better' or best practices way of adding custom package logging parameters and whether there is a need to publish it in any way (and how).

how to use or import ffmpeg in a laravel controller

i have already installed the laravel-ffmpeg via composer in my project
composer require pbmedia/laravel-ffmpeg
then i added class to config/app.php file as documentation says
// config/app.php
'providers' => [
...
ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider::class,
...
];
'aliases' => [
...
'FFMpeg' => ProtoneMedia\LaravelFFMpeg\Support\FFMpeg::class
...
];
then publish this config file
php artisan vendor:publish --provider="ProtoneMedia\LaravelFFMpeg\Support\ServiceProvider"
now my question is to use this package in my project, how should i use this into my controller
i saw many post and ques regarding this and i get these
use FFMpeg;
use FFMpeg\FFMpeg;
use Pbmedia\LaravelFFMpeg\FFMpeg;
use ProtoneMedia\LaravelFFMpeg\FFMpeg;
.
.
.
which one is correct, my goal is to trim a video, but which one works for that i am so confused
//config/laravel-ffmpeg.php
<?php
return [
'ffmpeg' => [
'binaries' => env('FFMPEG_BINARIES', 'ffmpeg'),
'threads' => 12,
],
'ffprobe' => [
'binaries' => env('FFPROBE_BINARIES', 'ffprobe'),
],
'timeout' => 3600,
'enable_logging' => true,
'set_command_and_error_output_on_exception' => false,
];
please help me out, thanks in advance
Use below code in your controller above class
use FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
Open file using FFMpeg in your controller method
$media = FFMpeg::open($file_path);

How to Fix Cache JavaScript in Yii 2

In Yii 2, when I add a few code rows to my JavaScript file, that has a cache, it will not accept my additional code to the JavaScript file.
How can I fix this problem?
You need to add forceCopy option in asset manager under components :
'components' => [
'assetManager' => [
'class' => 'yii\web\AssetManager',
'forceCopy' => true,
],
],
In my case I did it by chanding version in asset file where i include this js:
public $js = [
'js/script.js?0.0.1',
];

yii2 assetBundle - all absolute links instead of relative

I am trying to figure out, how can I configure AssetBundle via /config/main.php in Yii2 . The reason is, that we need to use globally absolute links for all assets (CSS + JS bundles) instead of relative.
We have set absolute #web alias:
Yii::setAlias('#webabs', empty($_SERVER['SERVER_NAME']) ? '/' : '//'.$_SERVER['SERVER_NAME']);
So the only thing we need to change is the property baseUrl in class \yii\web\AssetBundle:
baseUrl = '#webabs'
Following did not work for me:
'assetBundle' => [
'baseUrl' => '#webabs',
],
because "assetBundle" is not core component.
'yii\web\AssetBundle' => [
'class' => 'yii\web\AssetBundle',
'baseUrl' => '#webabs',
],
because Object configurator will not configure property.
So is there any way to configure "baseUrl" property globally in "\yii\web\AssetBundle"?
Thank you.
Try in configuration:
// ...
'components' => [
// ...
'assetManager' => [
'baseUrl' => '#webabs/assets'
],
],

Yii2 combine assets into one css and js

I have a site based on the advanced app template, everything works fine except I want to combine all the css and js files into one.
So I read the official documentation in order to combine assets.
The problem:
the bootstrap's js file in unminified and is included separately but the css file is compressed and combined perfectly (with the others)?
how can I combine the assets included with a widget?
Here is my assets.php (which is used with the php yii asset command)
Yii::setAlias('#webroot', __DIR__ . '/frontend/web');
Yii::setAlias('#web', '/');
return [
// Adjust command/callback for JavaScript files compressing:
'jsCompressor' => 'java -jar compiler.jar --js {from} --js_output_file {to}',
// Adjust command/callback for CSS files compressing:
'cssCompressor' => 'yui-compressor --type css {from} -o {to}',
// The list of asset bundles to compress:
'bundles' => [
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
'frontend\assets\AppAsset',
'yii\web\YiiAsset',
'\rmrevin\yii\fontawesome\AssetBundle',
],
// Asset bundle for compression output:
'targets' => [
'all' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '#webroot',
'baseUrl' => '#web',
'js' => 'js/all-{hash}.js',
'css' => 'css/all-{hash}.css',
],
],
// Asset manager configuration:
'assetManager' => [
'basePath' => '#webroot/assets',
'baseUrl' => '#web/assets',
],
];

Categories