<commit_before><?php namespace Main\Classes;

use App;
use File;
use Igniter\Flame\Exception\ApplicationException;
use Igniter\Flame\Traits\Singleton;
use Lang;
use System\Libraries\Assets;
use SystemException;
use ZipArchive;

/**
 * Theme Manager Class
 * @package Main
 */
class ThemeManager
{
    use Singleton;

    protected $themeModel = 'System\Models\Themes_model';

    /**
     * @var array of disabled themes.
     */
    public $installedThemes = [];

    /**
     * @var array used for storing theme information objects.
     */
    public $themes = [];

    public $activeTheme;

    /**
     * @var array of themes and their directory paths.
     */
    protected $paths = [];

    protected $config = [
        'allowedImageExt' => ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff'],
        'allowedFileExt' => ['html', 'txt', 'xml', 'js', 'css', 'php', 'json'],
    ];

    protected $loadedConfig;

    protected $loadedCustomizerConfig;

    protected $booted = FALSE;

    public function initialize()
    {
        // This prevents reading settings from the database before its been created
        if (App::hasDatabase()) {
            $this->loadInstalled();
            $this->loadThemes();
        }
    }

    public static function addAssetsFromActiveThemeManifest(Assets $manager)
    {
        $instance = self::instance();
        $theme = $instance->getActiveTheme();
        $manager->addFromManifest($theme->publicPath.'/_meta/assets.json');

        if ($theme->hasParent()) {
            $parentTheme = $instance->findTheme($theme->getParentName());
            $manager->addFromManifest($parentTheme->publicPath.'/_meta/assets.json');
        }
    }

    public static function applyAssetVariablesOnCombinerFilters(array $filters)
    {
        $theme = self::instance()->getActiveTheme();

        if (!$theme OR !$theme->hasCustomData())
            return;

        $assetVars = $theme->getAssetVariables();
        foreach ($filters as $filter) {
            if (method_exists($filter, 'setVariables')) {
                $filter->setVariables($assetVars);
            }
        }
    }

    //
    // Registration Methods
    //

    /**
     * Returns a list of all themes in the system.
     * @return array A list of all themes in the system.
     */
    public function listThemes()
    {
        $themes = [];
        foreach ($this->paths() as $theme => $path) {
            $themes[] = $theme;
        }

        return $themes;
    }

    /**
     * Loads all installed theme from application config.
     */
    public function loadInstalled()
    {
        if (($installedThemes = setting('installed_themes')) AND is_array($installedThemes)) {
            $this->installedThemes = $installedThemes;
        }
    }

    /**
     * Finds all available themes and loads them in to the $themes array.
     * @return array
     * @throws \SystemException
     */
    public function loadThemes()
    {
        foreach ($this->paths() as $theme => $path) {
            $this->loadTheme($theme, $path);
        }

        return $this->themes;
    }

    /**
     * Loads a single theme in to the manager.
     *
     * @param string $themeCode Eg: directory_name
     * @param string $path Ex: base_path().'directory_name';
     *
     * @return bool|object
     * @throws \SystemException
     */
    public function loadTheme($themeCode, $path)
    {
        if (!$this->checkName($themeCode)) return FALSE;

        if (isset($this->themes[$themeCode])) {
            return $this->themes[$themeCode];
        }

        $config = $this->getMetaFromFile($themeCode);
        $themeObject = new Theme($path, $config);

        $themeObject->active = $this->isActive($themeCode);

        $this->themes[$themeCode] = $themeObject;
        $this->paths[$themeCode] = $path;

        return $themeObject;
    }

    public function bootThemes()
    {
        if ($this->booted)
            return;

        foreach ($this->themes as $theme) {
            $theme->boot();
        }

        $this->booted = TRUE;
    }

    //
    // Management Methods
    //

    public function getActiveTheme()
    {
        return ($activeTheme = $this->findTheme($this->getActiveThemeCode()))
            ? $activeTheme : null;
    }

    public function getActiveThemeCode()
    {
        return trim(params('default_themes.main', config('system.defaultTheme')), '/');
    }

    /**
     * Returns a theme object based on its name.<commit_msg>New theme.getActiveTheme event<commit_after><?php namespace Main\Classes;

use App;
use File;
use Igniter\Flame\Exception\ApplicationException;
use Igniter\Flame\Traits\Singleton;
use Illuminate\Support\Facades\Event;
use Lang;
use System\Libraries\Assets;
use SystemException;
use ZipArchive;

/**
 * Theme Manager Class
 * @package Main
 */
class ThemeManager
{
    use Singleton;

    protected $themeModel = 'System\Models\Themes_model';

    /**
     * @var array of disabled themes.
     */
    public $installedThemes = [];

    /**
     * @var array used for storing theme information objects.
     */
    public $themes = [];

    public $activeTheme;

    /**
     * @var array of themes and their directory paths.
     */
    protected $paths = [];

    protected $config = [
        'allowedImageExt' => ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff'],
        'allowedFileExt' => ['html', 'txt', 'xml', 'js', 'css', 'php', 'json'],
    ];

    protected $loadedConfig;

    protected $loadedCustomizerConfig;

    protected $booted = FALSE;

    public function initialize()
    {
        // This prevents reading settings from the database before its been created
        if (App::hasDatabase()) {
            $this->loadInstalled();
            $this->loadThemes();
        }
    }

    public static function addAssetsFromActiveThemeManifest(Assets $manager)
    {
        $instance = self::instance();
        $theme = $instance->getActiveTheme();
        $manager->addFromManifest($theme->publicPath.'/_meta/assets.json');

        if ($theme->hasParent()) {
            $parentTheme = $instance->findTheme($theme->getParentName());
            $manager->addFromManifest($parentTheme->publicPath.'/_meta/assets.json');
        }
    }

    public static function applyAssetVariablesOnCombinerFilters(array $filters)
    {
        $theme = self::instance()->getActiveTheme();

        if (!$theme OR !$theme->hasCustomData())
            return;

        $assetVars = $theme->getAssetVariables();
        foreach ($filters as $filter) {
            if (method_exists($filter, 'setVariables')) {
                $filter->setVariables($assetVars);
            }
        }
    }

    //
    // Registration Methods
    //

    /**
     * Returns a list of all themes in the system.
     * @return array A list of all themes in the system.
     */
    public function listThemes()
    {
        $themes = [];
        foreach ($this->paths() as $theme => $path) {
            $themes[] = $theme;
        }

        return $themes;
    }

    /**
     * Loads all installed theme from application config.
     */
    public function loadInstalled()
    {
        if (($installedThemes = setting('installed_themes')) AND is_array($installedThemes)) {
            $this->installedThemes = $installedThemes;
        }
    }

    /**
     * Finds all available themes and loads them in to the $themes array.
     * @return array
     * @throws \SystemException
     */
    public function loadThemes()
    {
        foreach ($this->paths() as $theme => $path) {
            $this->loadTheme($theme, $path);
        }

        return $this->themes;
    }

    /**
     * Loads a single theme in to the manager.
     *
     * @param string $themeCode Eg: directory_name
     * @param string $path Ex: base_path().'directory_name';
     *
     * @return bool|object
     * @throws \SystemException
     */
    public function loadTheme($themeCode, $path)
    {
        if (!$this->checkName($themeCode)) return FALSE;

        if (isset($this->themes[$themeCode])) {
            return $this->themes[$themeCode];
        }

        $config = $this->getMetaFromFile($themeCode);
        $themeObject = new Theme($path, $config);

        $themeObject->active = $this->isActive($themeCode);

        $this->themes[$themeCode] = $themeObject;
        $this->paths[$themeCode] = $path;

        return $themeObject;
    }

    public function bootThemes()
    {
        if ($this->booted)
            return;

        foreach ($this->themes as $theme) {
            $theme->boot();
        }

        $this->booted = TRUE;
    }

    //
    // Management Methods
    //

    public function getActiveTheme()
    {
        return ($activeTheme = $this->findTheme($this->getActiveThemeCode()))
            ? $activeTheme : null;
    }

    public function getActiveThemeCode()
    {
        $activeTheme = trim(params('default_themes.main', config('system.defaultTheme')), '/');

        if (!is_null($apiResult = Event::fire('theme.getActiveTheme', [], TRUE)))
            $activeTheme = $apiResult;

        return $activeTheme;
    }

    /**
     * Returns a theme object based on its name.
{'fraction_non_alphanumeric': 0.1382734912146677, 'fraction_numerical': 0.0, 'mean_word_length': 2.231311706629055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '501385', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2540, 'n_words': 617}