1. Go to this page and download the library: Download enjoys/assets-collector library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
enjoys / assets-collector example snippets
use Enjoys\AssetsCollector\Assets;
use Enjoys\AssetsCollector\Environment;
//project directory
$projectDir = __DIR__;
//compile path relative project directory
$assetsDir = $projectDir .'/assets';
//or relative project directory
//$assetsDir = 'assets';
$environment = new Environment($assetsDir, $projectDir);
//Base URL to compile path for Web
$environment->setBaseUrl("/assets-collector/example/assets");
//Set strategy, default STRATEGY_MANY_FILES
$environment->setStrategy(Assets::STRATEGY_ONE_FILE); //Assets::STRATEGY_MANY_FILES
//Cache time for files in strategy STRATEGY_ONE_FILE
$environment->setCacheTime(0); //cache time in seconds
//Adds the output version, for example //example.php/style.css?v=123
$environment->setVersion(123);
//You can change the parameter for the version
$environment->setParamVersion('?ver=');
/**
* YYou can add a logger that implements \Psr\Log\LoggerInterface, for example, Monolog
* @var \Psr\Log\LoggerInterface $logger
*/
$environment->setLogger($logger);
/** @var \Enjoys\AssetsCollector\Environment $environment */
$assets = new \Enjoys\AssetsCollector\Assets($environment);
/** @var \Enjoys\AssetsCollector\Assets $assets */
$assets->add('css', [
'style/style.css', //относительный путь, относительно текущей рабочей директории
__DIR__ . '/style/style.css', //полный путь
'//example.com/style.css', //сокращенная URL ссылка
'https://example.com/style.css', //URL ссылка
'url:/assets/css/style.css', // URL ссылка
'local:/assets/css/style.css', // URL ссылка (local: и url: идентичны)
['style.css', \Enjoys\AssetsCollector\AssetOption::MINIFY => false], //попускает минификацию конкретного файла
['goods/style.css', \Enjoys\AssetsCollector\AssetOption::REPLACE_RELATIVE_URLS => false], //не заменяет относительные ссылки - оставляет так как есть
]);
use Enjoys\AssetsCollector\AssetOption;
/** @var \Enjoys\AssetsCollector\Assets $assets */
$assets->add('css', [
[
__DIR__.'/style.css',
// По-умолчанию все ресурсы минифицируются, если указать явно false, этот ресурс пропустит минификацию
AssetOption::MINIFY => false,
// Если нужно создать дополнительные симлинки, то можно указать их в этом параметре, в качества массива,
// где ключ - сама ссылка, а значение - исходный файл или директория (цель)
// Это бывает необходимо если в ресурсе есть относительные ссылки, и чтобы был к ним доступ нужно прописать
// явно все символические ссылки
AssetOption::SYMLINKS => [
__DIR__.'/symlink' => __DIR__.'/../../../target',
//...
],
// При STRATEGY_MANY_FILES будут добавлены html-аттрибуты,
// примерно это будет выглядеть так
// <script attribute-key='attribute-value' attribute-without-value attribute-without-value src='...'>
AssetOption::ATTRIBUTES => [
'attribute-key' => 'attribute-value',
'attribute-without-value' => null,
'attribute-without-value-another-method',
//...
],
// При STRATEGY_ONE_FILE если будет установлена эта опция в true, то именно этот asset в сборку не попадет,
// а выведется отдельно
AssetOption::NOT_COLLECT => true,
// При false - не заменяет относительные ссылки - оставляет так как есть.
// По-умолчанию true, все относительные ссылки заменяются на абсолютные
AssetOption::REPLACE_RELATIVE_URLS => false
],
//...
]);
use Enjoys\AssetsCollector\Content\Minify\Adapters\CssMinify;
/** @var \Enjoys\AssetsCollector\Environment $environment */
//необязательно передавать все параметры, можно только выборочно
$environment->setMinifyCSS(
new CssMinify([
'keepSourceMapComment' => false, //bool
'removeImportantComments' => true, //bool
'setLineBreakPosition' => 1000, //int
'setMaxExecutionTime' => 60, //int
'setMemoryLimit' => '128M',
'setPcreBacktrackLimit' => 1000000, //int
'setPcreRecursionLimit' => 500000, //int
])
);
use Enjoys\AssetsCollector\Content\Minify\Adapters\JsMinify;
/** @var \Enjoys\AssetsCollector\Environment $environment */
// Необязательно передавать все параметры, можно только выборочно
$environment->setJsMinifyOptions(
new JsMinify([
'flaggedComments' => false
])
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.