<commit_before><?php
/*---------------------------------------------------------------------------
 * @Project: Alto CMS
 * @Project URI: http://altocms.com
 * @Description: Advanced Community Engine
 * @Copyright: Alto CMS Team
 * @License: GNU GPL v2 & MIT
 *----------------------------------------------------------------------------
 */

/**
 * @package engine.modules
 * @since   1.0
 */

class ModuleViewerAsset_EntityPackage extends Entity {

    protected $sOutType = '*';
    protected $sAssetType = '';

    protected $bMerge = false;
    protected $bCompress = false;

    protected $aFiles = array();

    protected $aAssetNames = array();
    protected $aAssets = array();

    protected $aLinks = array();
    protected $aHtmlLinkParams = array();

    protected $oCompressor;

    protected $aMapDir = array();

    protected $sMarker;

    public function __construct($aParams = array()) {

        if (isset($aParams['out_type'])) {
            $this->sOutType = $aParams['out_type'];
        }
        if (isset($aParams['asset_type'])) {
            $this->sAssetType = $aParams['asset_type'];
        }
        if ($this->sOutType) {
            $this->bMerge = (bool)Config::Get('compress.' . $this->sOutType . '.merge');
            $this->bCompress = (bool)Config::Get('compress.' . $this->sOutType . '.use');
        }
        $this->sMarker = uniqid('alto-asset-marker-', true);
    }

    /**
     * @param string $sDir
     *
     * @return string
     */
    protected function _makeSubdir($sDir) {

        if (!isset($this->aMapDir[$sDir])) {
            $s=F::Crc32($sDir, true);
            $this->aMapDir[$sDir] = F::Crc32($sDir, true);
        }
        return $this->aMapDir[$sDir];
    }

    /**
     * Initialization
     */
    public function Init() {

        $this->aHtmlLinkParams = array();
    }

    /**
     * @return string
     */
    public function GetHash() {

        return $this->sAssetType . '-' . md5(serialize($this->aFiles));
    }

    /**
     * Добавляет ссылку в набор
     *
     * @param string $sOutType
     * @param string $sLink
     * @param array  $aParams
     */
    public function AddLink($sOutType, $sLink, $aParams = array()) {

        if ($sOutType != $this->sOutType) {
            E::ModuleViewerAsset()->AddLinksToAssets('*', array($sLink => $aParams));
        } else {
            $this->aLinks[] = array_merge($aParams, array('link' => $sLink));
        }
    }

    /**
     * Сжатие контента
     *
     * @param string $sContents
     *
     * @return string
     */
    public function Compress($sContents) {

        return $sContents;
    }

    /**
     * Обработка файла
     *
     * @param string $sFile
     * @param string $sDestination
     *
     * @return string
     */
    public function PrepareFile($sFile, $sDestination) {

        return F::File_Copy($sFile, $sDestination);
    }

    /**
     * Обработка контента
     *
     * @param string $sContents
     * @param string $sSource
     *
     * @return string
     */
    public function PrepareContents($sContents, $sSource) {

        return $sContents;
    }

    /**
     * Создание ресурса из одиночного файла
     *
     * @param string $sAsset
     * @param array  $aFileParams
     *
     * @return bool
     */
    public function MakeSingle($sAsset, $aFileParams) {

        $sFile = $aFileParams['file'];
        if (isset($aFileParams['dir_from'])) {
            $sLocalPath = F::File_LocalPath(dirname($sFile), $aFileParams['dir_from']);
            $sDir = $aFileParams['dir_from'];
        } else {
            $sLocalPath = '';
            $sDir = dirname($sFile);
        }
        if ($aFileParams['merge']) {
            $sSubdir = $this->_makeSubdir($sAsset . $sDir);
        } else {
            $sSubdir = $this->_makeSubdir($sDir);
        }
        if ($sLocalPath) {
            $sSubdir .= '/' . $sLocalPath;
        }
        $sDestination = F::File_GetAssetDir() . $sSubdir . '/' . basename($sFile);
        $aFileParams['asset_file'] = $sDestination;
        if (!$this->CheckDestination($sDestination)) {
            if ($sDestination = $this->PrepareFile($sFile, $sDestination)) {
                $this->AddLink($aFileParams['info']['extension'], E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aFileParams);
            } else {
                F::SysWarning('Can not prepare asset file "' . $sFile . '"');
                return false;
            }
        } else {
            $this->AddLink($aFileParams['info']['extension'], E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aFileParams);
        }
        return true;
    }

    /**
     * Создание ресурса из множества файлов
     *
     * @param string $sAsset
     * @param array  $aFiles
     *
     * @return bool
     */
    public function MakeMerge($sAsset, $aFiles) {

        $sDestination = F::File_GetAssetDir() . md5($sAsset . serialize($aFiles)) . '.' . $this->sOutType;
        if (!$this->CheckDestination($sDestination)) {
            $sContents = '';
            $bCompress = true;
            $bPrepare = null;
            foreach ($aFiles as $aFileParams) {
                $sFileContents = F::File_GetContents($aFileParams['file']);
                $sContents .= $this->PrepareContents($sFileContents, $aFileParams['file']) . PHP_EOL;
                if (isset($aFileParams['compress'])) {
                    $bCompress = $bCompress && (bool)$aFileParams['compress'];
                }
                // Если хотя бы один файл из набора нужно выводить, то весь набор выводится
                if ((is_null($bPrepare) || $bPrepare === true) && isset($aFileParams['prepare']) && !$aFileParams['prepare']) {
                    $bPrepare = false;
                }
            }
            if (F::File_PutContents($sDestination, $sContents)) {
                $aParams = array(
                    'file' => $sDestination,
                    'asset' => $sAsset,
                    'asset_file' => $sDestination,
                    'compress' => $bCompress,
                    'prepare' => is_null($bPrepare) ? false : $bPrepare,
                );
                $this->AddLink($this->sOutType, E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aParams);<commit_msg>viewerAsset - update<commit_after><?php
/*---------------------------------------------------------------------------
 * @Project: Alto CMS
 * @Project URI: http://altocms.com
 * @Description: Advanced Community Engine
 * @Copyright: Alto CMS Team
 * @License: GNU GPL v2 & MIT
 *----------------------------------------------------------------------------
 */

/**
 * @package engine.modules
 * @since   1.0
 */

class ModuleViewerAsset_EntityPackage extends Entity {

    protected $sOutType = '*';
    protected $sAssetType = '';

    protected $bMerge = false;
    protected $bCompress = false;

    protected $aFiles = array();

    protected $aAssetNames = array();
    protected $aAssets = array();

    protected $aLinks = array();
    protected $aHtmlLinkParams = array();

    protected $oCompressor;

    protected $aMapDir = array();

    protected $sMarker;

    public function __construct($aParams = array()) {

        if (isset($aParams['out_type'])) {
            $this->sOutType = $aParams['out_type'];
        }
        if (isset($aParams['asset_type'])) {
            $this->sAssetType = $aParams['asset_type'];
        }
        if ($this->sOutType) {
            $this->bMerge = (bool)Config::Get('compress.' . $this->sOutType . '.merge');
            $this->bCompress = (bool)Config::Get('compress.' . $this->sOutType . '.use');
        }
        $this->sMarker = uniqid('alto-asset-marker-', true);
    }

    /**
     * @param string $sDir
     *
     * @return string
     */
    protected function _makeSubdir($sDir) {

        if (!isset($this->aMapDir[$sDir])) {
            $s=F::Crc32($sDir, true);
            $this->aMapDir[$sDir] = F::Crc32($sDir, true);
        }
        return $this->aMapDir[$sDir];
    }

    /**
     * Initialization
     */
    public function Init() {

        $this->aHtmlLinkParams = array();
    }

    /**
     * @return string
     */
    public function GetHash() {

        return $this->sAssetType . '-' . md5(serialize($this->aFiles));
    }

    /**
     * Добавляет ссылку в набор
     *
     * @param string $sOutType
     * @param string $sLink
     * @param array  $aParams
     */
    public function AddLink($sOutType, $sLink, $aParams = array()) {

        if ($sOutType != $this->sOutType) {
            E::ModuleViewerAsset()->AddLinksToAssets('*', array($sLink => $aParams));
        } else {
            $this->aLinks[] = array_merge($aParams, array('link' => $sLink));
        }
    }

    /**
     * Сжатие контента
     *
     * @param string $sContents
     *
     * @return string
     */
    public function Compress($sContents) {

        return $sContents;
    }

    /**
     * Обработка файла
     *
     * @param string $sFile
     * @param string $sDestination
     *
     * @return string
     */
    public function PrepareFile($sFile, $sDestination) {

        return F::File_Copy($sFile, $sDestination);
    }

    /**
     * Обработка контента
     *
     * @param string $sContents
     * @param string $sSource
     *
     * @return string
     */
    public function PrepareContents($sContents, $sSource) {

        return $sContents;
    }

    /**
     * Создание ресурса из одиночного файла
     *
     * @param string $sAsset
     * @param array  $aFileParams
     *
     * @return bool
     */
    public function MakeSingle($sAsset, $aFileParams) {

        $sFile = $aFileParams['file'];
        if (isset($aFileParams['dir_from'])) {
            $sLocalPath = F::File_LocalPath(dirname($sFile), $aFileParams['dir_from']);
            $sDir = $aFileParams['dir_from'];
        } else {
            $sLocalPath = '';
            $sDir = dirname($sFile);
        }
        if ($aFileParams['merge']) {
            $sSubdir = $this->_makeSubdir($sAsset . $sDir);
        } else {
            $sSubdir = $this->_makeSubdir($sDir);
        }
        if ($sLocalPath) {
            $sSubdir .= '/' . $sLocalPath;
        }
        $sDestination = F::File_GetAssetDir() . $sSubdir . '/' . basename($sFile);
        $aFileParams['asset_file'] = $sDestination;
        if (!$this->CheckDestination($sDestination)) {
            if ($sDestination = $this->PrepareFile($sFile, $sDestination)) {
                $this->AddLink($aFileParams['info']['extension'], E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aFileParams);
            } else {
                F::SysWarning('Can not prepare asset file "' . $sFile . '"');
                return false;
            }
        } else {
            $this->AddLink($aFileParams['info']['extension'], E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aFileParams);
        }
        return true;
    }

    /**
     * Создание ресурса из множества файлов
     *
     * @param string $sAsset
     * @param array  $aFiles
     *
     * @return bool
     */
    public function MakeMerge($sAsset, $aFiles) {

        $sDestination = F::File_GetAssetDir() . md5($sAsset . serialize($aFiles)) . '.' . $this->sOutType;
        if (!$this->CheckDestination($sDestination)) {
            $sContents = '';
            $bCompress = $this->bCompress;
            $bPrepare = null;
            foreach ($aFiles as $aFileParams) {
                $sFileContents = F::File_GetContents($aFileParams['file']);
                $sContents .= $this->PrepareContents($sFileContents, $aFileParams['file']) . PHP_EOL;
                if (isset($aFileParams['compress'])) {
                    $bCompress = $bCompress && (bool)$aFileParams['compress'];
                }
                // Если хотя бы один файл из набора нужно выводить, то весь набор выводится
                if ((is_null($bPrepare) || $bPrepare === true) && isset($aFileParams['prepare']) && !$aFileParams['prepare']) {
                    $bPrepare = false;
                }
            }
            if (F::File_PutContents($sDestination, $sContents)) {
                $aParams = array(
                    'file' => $sDestination,
                    'asset' => $sAsset,
                    'asset_file' => $sDestination,
                    'compress' => $bCompress,
                    'prepare' => is_null($bPrepare) ? false : $bPrepare,
                );
                $this->AddLink($this->sOutType, E::ModuleViewerAsset()->AssetFileDir2Url($sDestination), $aParams);
{'fraction_non_alphanumeric': 0.17491039426523297, 'fraction_numerical': 0.0019115890083632018, 'mean_word_length': 1.8831228473019517, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '418079', 'n_tokens_mistral': 3838, 'n_tokens_neox': 3817, 'n_words': 696}