<commit_before><?php
/**
 * View.php
 * @author <NAME> http://phptime.ru
 */

namespace rmrevin\yii\minify;

use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\helpers\StringHelper;

/**
 * Class View
 * @package rmrevin\yii\minify
 */
class View extends \yii\web\View
{

    /**
     * @var string path alias to web base
     */
    public $base_path = '@app/web';

    /**
     * @var string path alias to save minify result
     */
    public $minify_path = '@app/web/minify';

    /**
     * @var array positions of js files to be minified
     */
    public $js_position = [ self::POS_END ];

    /**
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
     */
    public $force_charset = false;

    /**
     * @var bool whether to change @import on content
     */
    public $expand_imports = true;

    /**
     * @var int
     */
    public $css_linebreak_pos = 2048;

    /**
     * @var int chmod of minified file
     */
    public $file_mode = 0664;

    /**
     * @var array schemes that will be ignored during normalization url
     */
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];

    public function init()
    {
        parent::init();

        $minify_path = $this->minify_path = \Yii::getAlias($this->minify_path);
        if (!file_exists($minify_path)) {
            FileHelper::createDirectory($minify_path);
        }

        if (!is_readable($minify_path)) {
            throw new \RuntimeException(\Yii::t('app', 'Directory for compressed assets is not readable.'));
        }

        if (!is_writable($minify_path)) {
            throw new \RuntimeException(\Yii::t('app', 'Directory for compressed assets is not writable.'));
        }
    }

    public function endPage($ajaxMode = false)
    {
        $this->trigger(self::EVENT_END_PAGE);

        $content = ob_get_clean();
        foreach (array_keys($this->assetBundles) as $bundle) {
            $this->registerAssetFiles($bundle);
        }

        $this->minify();

        echo strtr(
            $content,
            [
                self::PH_HEAD => $this->renderHeadHtml(),
                self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
                self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
            ]
        );

        $this->clear();
    }

    protected function registerAssetFiles($name)
    {
        if (!isset($this->assetBundles[$name])) {
            return;
        }

        $bundle = $this->assetBundles[$name];
        if ($bundle) {
            foreach ($bundle->depends as $dep) {
                $this->registerAssetFiles($dep);
            }

            $bundle->registerAssetFiles($this);
        }

        unset($this->assetBundles[$name]);
    }

    private function minify()
    {
        $this
            ->minifyCSS()
            ->minifyJS();
    }

    /**
     * @return self
     */
    protected function minifyCSS()
    {
        if (!empty($this->cssFiles)) {
            $css_files = array_keys($this->cssFiles);

            $long_hash = '';
            foreach ($css_files as $file) {
                $file = \Yii::getAlias($this->base_path) . $file;
                $hash = sha1_file($file);
                $long_hash .= $hash;
            }

            $css_minify_file = $this->minify_path . DIRECTORY_SEPARATOR . sha1($long_hash) . '.css';
            if (!file_exists($css_minify_file)) {
                $css = '';
                $charsets = '';
                $imports = '';
                $fonts = '';

                foreach ($css_files as $file) {
                    $content = file_get_contents(\Yii::getAlias($this->base_path) . $file);

                    preg_match_all('|url\(([^)]+)\)|is', $content, $m);
                    if (!empty($m[0])) {
                        $path = dirname($file);
                        $result = [];
                        foreach ($m[0] as $k => $v) {
                            $url = str_replace(['\'', '"'], '', $m[1][$k]);
                            if (preg_match('#^(' . implode('|', $this->schemas) . ')#is', $url)) {
                                $result[$m[1][$k]] = '\'' . $url . '\'';
                            } else {
                                $result[$m[1][$k]] = '\'' . $path . DIRECTORY_SEPARATOR . $url . '\'';
                            }
                        }
                        $content = str_replace(array_keys($result), array_values($result), $content);
                    }

                    $css .= $content;<commit_msg>Wrong URL about base64 encoded data<commit_after><?php
/**
 * View.php
 * @author <NAME> http://phptime.ru
 */

namespace rmrevin\yii\minify;

use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\helpers\StringHelper;

/**
 * Class View
 * @package rmrevin\yii\minify
 */
class View extends \yii\web\View
{

    /**
     * @var string path alias to web base
     */
    public $base_path = '@app/web';

    /**
     * @var string path alias to save minify result
     */
    public $minify_path = '@app/web/minify';

    /**
     * @var array positions of js files to be minified
     */
    public $js_position = [ self::POS_END ];

    /**
     * @var bool|string charset forcibly assign, otherwise will use all of the files found charset
     */
    public $force_charset = false;

    /**
     * @var bool whether to change @import on content
     */
    public $expand_imports = true;

    /**
     * @var int
     */
    public $css_linebreak_pos = 2048;

    /**
     * @var int chmod of minified file
     */
    public $file_mode = 0664;

    /**
     * @var array schemes that will be ignored during normalization url
     */
    public $schemas = ['//', 'http://', 'https://', 'ftp://'];

    public function init()
    {
        parent::init();

        $minify_path = $this->minify_path = \Yii::getAlias($this->minify_path);
        if (!file_exists($minify_path)) {
            FileHelper::createDirectory($minify_path);
        }

        if (!is_readable($minify_path)) {
            throw new \RuntimeException(\Yii::t('app', 'Directory for compressed assets is not readable.'));
        }

        if (!is_writable($minify_path)) {
            throw new \RuntimeException(\Yii::t('app', 'Directory for compressed assets is not writable.'));
        }
    }

    public function endPage($ajaxMode = false)
    {
        $this->trigger(self::EVENT_END_PAGE);

        $content = ob_get_clean();
        foreach (array_keys($this->assetBundles) as $bundle) {
            $this->registerAssetFiles($bundle);
        }

        $this->minify();

        echo strtr(
            $content,
            [
                self::PH_HEAD => $this->renderHeadHtml(),
                self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
                self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
            ]
        );

        $this->clear();
    }

    protected function registerAssetFiles($name)
    {
        if (!isset($this->assetBundles[$name])) {
            return;
        }

        $bundle = $this->assetBundles[$name];
        if ($bundle) {
            foreach ($bundle->depends as $dep) {
                $this->registerAssetFiles($dep);
            }

            $bundle->registerAssetFiles($this);
        }

        unset($this->assetBundles[$name]);
    }

    private function minify()
    {
        $this
            ->minifyCSS()
            ->minifyJS();
    }

    /**
     * @return self
     */
    protected function minifyCSS()
    {
        if (!empty($this->cssFiles)) {
            $css_files = array_keys($this->cssFiles);

            $long_hash = '';
            foreach ($css_files as $file) {
                $file = \Yii::getAlias($this->base_path) . $file;
                $hash = sha1_file($file);
                $long_hash .= $hash;
            }

            $css_minify_file = $this->minify_path . DIRECTORY_SEPARATOR . sha1($long_hash) . '.css';
            if (!file_exists($css_minify_file)) {
                $css = '';
                $charsets = '';
                $imports = '';
                $fonts = '';

                foreach ($css_files as $file) {
                    $content = file_get_contents(\Yii::getAlias($this->base_path) . $file);

                    preg_match_all('|url\(([^)]+)\)|is', $content, $m);
                    if (!empty($m[0])) {
                        $path = dirname($file);
                        $result = [];
                        foreach ($m[0] as $k => $v) {
                            if ( 0 === strpos( $m[1][$k], 'data:' ) )
                                continue;
                            $url = str_replace(['\'', '"'], '', $m[1][$k]);
                            if (preg_match('#^(' . implode('|', $this->schemas) . ')#is', $url)) {
                                $result[$m[1][$k]] = '\'' . $url . '\'';
                            } else {
                                $result[$m[1][$k]] = '\'' . $path . DIRECTORY_SEPARATOR . $url . '\'';
                            }
                        }
                        $content = str_replace(array_keys($result), array_values($result), $content);
                    }

                    $css .= $content;
{'fraction_non_alphanumeric': 0.15580492015537334, 'fraction_numerical': 0.0036685369011653, 'mean_word_length': 1.567590027700831, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3444899', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2680, 'n_words': 540}