Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed code for project environment #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .scrutinizer.yml
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
Empty file modified Exception.php
100644 → 100755
Empty file.
4 changes: 3 additions & 1 deletion HtmlCompressor.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ private function htmlCompress($data, $options = null)

// Remove HTML comments...
if (array_key_exists('c', $options) || array_key_exists('no-comments', $options)) {
$out = preg_replace('/(<!--.*?-->)/ms', '', $out);
// $out = preg_replace('/(<!--.*?-->)/ms', '', $out);
// adding browser's condition's exceptions
$out = preg_replace('#\\s*<!--[^\\[].*?(?<!!)-->\\s*#s', '', $out);
$out = str_replace('<!>', '', $out);
}

Expand Down
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
48 changes: 2 additions & 46 deletions View.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -190,85 +190,41 @@ class View extends \yii\web\View
*/
public $excludeFiles = [];

/*
* Updated params by Jaimin MosLake to give more functionality
*/

/**
* @var boolean
* whether you want to use S3Bucket or not
* By default it will be false
*/
public $S3Upload = false ;


/**
* @var boolean
* Name of awsBucket
*/
public $awsBucket = null ;

/**
* @var boolean
* It is for linking Resource folder to asset files
* if Resources like images above one folder it should be "../" if two folders above "../../"
* You want to load all the images from s3 now you have images folder in root and you have dev , qa , prod folder
* and css and js inside those folder now you have to link images in those css file.
* You have to use this option to do that.
*/
public $assetsFolderPathPatch = null ;


/*
* boolean
* backend checke will help take asset from root/minify folder for backedn instead of root/backend/minifiy
* If backend and frontend has same assets and you want to use same location to store asset
* you can make thi true. By this it will use root/minify other then root/backedn/minify
* to copy files to S3
*/
public $backendCheck = false ;

/*
* Folder name where minified files will be kept
* Here i have devided it will be used when $backendCheck is true
* as if we are doing from web from backend / frontend to have same file hashes
* as I am using same assets folder in root for backend / frontend as to have files from same assets folder
*/
public $folderName = 'minify' ;

/*
* will be used at _getSummaryFilesHash will fix path to have same hash value as frontend or backend when files generated from console.
* At console level this will be used as when generating from console path will be different so some adjustment path should be decalared t make path same as
* of running in web browser as console has path from console folder script
*
* so when you generate from console make modifyPath true
* and modifyPathData regarding your assets folder to console folder
*/
public $modifyPath = false ;
public $modifyPathData = "" ;


/*
* It helps if you want to add prefix to any file as it will mostly create file name
* as {prefix}-all-in-one-{HASH}.{js/css}
* so if you want to give a prefix for certain layout
* then you can do it by this option.
* Pass layout name as array key and pass prefix name as array value
* ex : for main layout if you want newmain prefix
* you have to pass array like ["main"=>"newmain"]
* if you do not wont prefix do not do anything just live it a blank array
*/
public $layoutPrefixArray = [] ;

/*
* Use layoutPrefixArray option for css true/false
*/
public $layoutPrefixCss = false ;

/*
* Use layoutPrefixArray option for Js true/false
*/
public $layoutPrefixJS = false ;


/**
* @throws \processfast\yii\minify\Exception
*/
Expand Down
97 changes: 9 additions & 88 deletions components/CSS.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@
/**
* CSS.php
* @author Revin Roman
* @link https://processfast.com
* @link https://rmrevin.com
*/

namespace processfast\yii\minify\components;

use tubalmartin\CssMin\Minifier as CSSmin;
use yii\helpers\Html;

/**
* Class CSS
* @package processfast\yii\minify\components
* @package rmrevin\yii\minify\components
*/
class CSS extends MinifyComponent
{

public function export()
{
$cssFiles = $this->view->cssFiles;

$this->view->cssFiles = [];

$toMinify = [];

if (!empty($cssFiles)) {
foreach ($cssFiles as $file => $html) {
if ($this->thisFileNeedMinify($file, $html)) {
Expand All @@ -35,127 +29,73 @@ public function export()
} else {
if (!empty($toMinify)) {
$this->process($toMinify);

$toMinify = [];
}

$this->view->cssFiles[$file] = $html;
}
}
}

if (!empty($toMinify)) {
$this->process($toMinify);
}

unset($toMinify);
}

/**
* @param array $files
*/
protected function process(array $files)
{
$hash = $this->_getSummaryFilesHash($files) ;
$resultFile = $this->view->minifyPath . DIRECTORY_SEPARATOR . $hash . '.css';

if( $this->view->S3Upload && $this->doesObjectExist( $resultFile , "CSS" , $hash ) )
{
// It exist on s3 just do not do any processing
$resultFile = $this->getS3Path( $resultFile , "CSS" , $hash );
}
else if (!file_exists($resultFile))
{
$resultFile = $this->view->minifyPath . DIRECTORY_SEPARATOR . $this->_getSummaryFilesHash($files) . '.css';
if (!file_exists($resultFile)) {
$css = '';

foreach ($files as $file => $html) {
$path = dirname($file);

if( $this->view->S3Upload )
{
$assetsFolderPathPatch = $this->view->assetsFolderPathPatch ;
$pathArray = explode('assets', $path, 2);
$newPath = $pathArray[1] ;
$path = $assetsFolderPathPatch."assets".$newPath ;
}

$file = $this->getAbsoluteFilePath($file);

$content = '';

if (!file_exists($file)) {
\Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__);
} elseif (!is_readable($file)) {
\Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__);
} else {
$content = file_get_contents($file);
}

$result = [];

preg_match_all('|url\(([^)]+)\)|is', $content, $m);
if (!empty($m[0])) {
foreach ($m[0] as $k => $v) {
if (in_array(strpos($m[1][$k], 'data:'), [0, 1], true)) {
continue;
}

$url = str_replace(['\'', '"'], '', $m[1][$k]);

if ($this->isUrl($url)) {
$result[$m[1][$k]] = $url;
} else {
$result[$m[1][$k]] = $path . '/' . $url;
}
}

$content = strtr($content, $result);
}

$css .= $content;
}

$this->expandImports($css);

$this->removeCssComments($css);

if ($this->view->minifyCss) {
$css = (new \CSSmin())
$css = (new CSSmin())
->run($css, $this->view->cssLinebreakPos);
}

$charsets = false !== $this->view->forceCharset
? ('@charset "' . (string)$this->view->forceCharset . '";' . "\n")
: $this->collectCharsets($css);

$imports = $this->collectImports($css);
$fonts = $this->collectFonts($css);

$content = gzencode( $charsets . $imports . $fonts . $css , 9);
file_put_contents($resultFile, $content );

file_put_contents($resultFile, $charsets . $imports . $fonts . $css);
if (false !== $this->view->fileMode) {
@chmod($resultFile, $this->view->fileMode);
}

if( $this->view->S3Upload )
{
$resultFile = $this->uploadToS3( $resultFile , "CSS" , $hash );
}
}
else
{
if( $this->view->S3Upload )
{
$resultFile = $this->uploadToS3( $resultFile , "CSS" , $hash );
}
}

$file = $this->prepareResultFile($resultFile);

$this->view->cssFiles[$file] = Html::cssFile($file);
}

/**
* @param string $code
*/
Expand All @@ -165,22 +105,18 @@ protected function removeCssComments(&$code)
$code = preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#', '', $code);
}
}

/**
* @param string $code
*/
protected function expandImports(&$code)
{
if (true === $this->view->expandImports) {
preg_match_all('|\@import\s([^;]+);|is', str_replace('&amp;', '&', $code), $m);

if (!empty($m[0])) {
foreach ($m[0] as $k => $v) {
$import_url = $m[1][$k];

if (!empty($import_url)) {
$import_content = $this->_getImportContent($import_url);

if (!empty($import_content)) {
$code = str_replace($m[0][$k], $import_content, $code);
}
Expand All @@ -189,7 +125,6 @@ protected function expandImports(&$code)
}
}
}

/**
* @param string $code
* @return string
Expand All @@ -200,7 +135,6 @@ protected function collectCharsets(&$code)
return $string . ';';
});
}

/**
* @param string $code
* @return string
Expand All @@ -211,7 +145,6 @@ protected function collectImports(&$code)
return $string . ';';
});
}

/**
* @param string $code
* @return string
Expand All @@ -222,7 +155,6 @@ protected function collectFonts(&$code)
return $string;
});
}

/**
* @param string $code
* @param string $pattern
Expand All @@ -232,50 +164,39 @@ protected function collectFonts(&$code)
protected function _collect(&$code, $pattern, $handler)
{
$result = '';

preg_match_all($pattern, $code, $m);

foreach ($m[0] as $string) {
$string = $handler($string);
$code = str_replace($string, '', $code);

$result .= $string . PHP_EOL;
}

return $result;
}

/**
* @param string $url
* @return null|string
*/
protected function _getImportContent($url)
{
$result = null;

if ('url(' === mb_substr($url, 0, 4)) {
$url = str_replace(['url(\'', 'url("', 'url(', '\')', '")', ')'], '', $url);

if (mb_substr($url, 0, 2) === '//') {
$url = preg_replace('|^//|', 'http://', $url, 1);
}

if (!empty($url)) {
if (!in_array(mb_substr($url, 0, 4), ['http', 'ftp:'], true)) {
$url = \Yii::getAlias($this->view->basePath . $url);
}

$context = [
'ssl' => [
'verify_peer' => false,
'verify_peer' => false,
'verify_peer_name' => false,
],
];

$result = file_get_contents($url, null, stream_context_create($context));
}
}

return $result;
}
}
}
Loading