<commit_before><?php
namespace Karlomikus\Theme;

use Karlomikus\Theme\Exceptions\ThemeNotFoundException;
use Illuminate\Contracts\Container\Container;
use Karlomikus\Theme\Exceptions\ThemeInfoAttributeException;

/**
 * Theme
 *
 * @author <NAME>
 * @version 0.0.1
 * @package Karlomikus\Theme
 */
class Theme {

    /**
     * Scanned themes
     * @var array|ThemeInfo[]
     */
    private $themes;

    /**
     * View finder
     * @var \Illuminate\View\Factory
     */
    private $view;

    /**
     * Base theme folder
     * @var string
     */
    private $basePath;

    /**
     * Current active theme
     * @var string|null
     */
    private $activeTheme = null;

    /**
     * Setup default view finder and view paths.
     *
     * @param Container $app
     */
    public function __construct(Container $app)
    {
        // Default themes path
        $this->basePath = config('theme.path');
        
        // Config view finder
        $paths = $app['config']['view.paths'];
        $this->view = $app->make('view');
        $this->view->setFinder(new ThemeViewFinder($app['files'], $paths));

        // Scan themes
        $this->scanThemes();
    }

    /**
     * Set current active theme
     *
     * @param string $theme Theme namespace
     * @throws ThemeNotFoundException
     */
    public function setTheme($theme)
    {
        if (!$this->themeExists($theme)) {
            throw new ThemeNotFoundException($theme);
        }

        $this->loadTheme($theme);
    }

    /**
     * Get all found themes
     *
     * @return array|ThemeInfo[]
     */
    public function getThemes()
    {
        return $this->themes;
    }

    /**
     * Return currently active theme
     *
     * @return null|ThemeInfo
     */
    public function get()
    {
        return $this->themes[$this->activeTheme];
    }

    /**
     * Check if theme exists
     *
     * @param $theme
     * @return bool
     */
    public function themeExists($theme)
    {
        return array_key_exists($theme, $this->themes);
    }

    /**
     * Set base themes folder path
     *
     * @param $path
     */
    public function setDefaultThemePath($path)
    {
        $this->basePath = $path;
        $this->scanThemes();
    }

    /**
     * Load a theme
     *
     * @param string $theme
     * @throws \Exception
     */
    private function loadTheme($theme)
    {
        if (!isset($theme))
            return;

        $th = $this->findThemeByNamespace($theme);

        if (isset($th)) {
            $viewFinder = $this->view->getFinder();

            $viewFinder->prependPath($th->getPath());
            if (!is_null($th->getParent()))
                $this->loadTheme($th->getParent());

            $this->activeTheme = $theme;
        }
    }

    /**
     * Find a theme from all scanned themes
     *
     * @param string $namespace
     * @return null|ThemeInfo
     */
    private function findThemeByNamespace($namespace)
    {
        if (isset($this->themes[$namespace]))
            return $this->themes[$namespace];

        return null;
    }

    /**
     * Scan for all available themes
     *
     * @throws ThemeInfoAttributeException
     */
    private function scanThemes()
    {
        $themeDirectories = glob($this->basePath . '/*', GLOB_ONLYDIR);

        $themes = [];
        foreach ($themeDirectories as $themePath) {
            $json = $themePath . '/theme.json';

            if (file_exists($json)) {
                $th = $this->parseThemeInfo(json_decode(file_get_contents($json), true));
                $themes[$th->getNamespace()] = $th;
            }
        }

        $this->themes = $themes;
    }

    /**
     * Find theme views path
     *
     * @param $namespace
     * @return string
     */
    private function findPath($namespace)
    {
        $path = [];
        $path[] = $this->basePath;
        $path[] = $namespace;
        $path[] = 'views';

        return implode(DIRECTORY_SEPARATOR, $path);
    }

    /**
     * Parse theme json file
     *
     * @param array $info
     * @return ThemeInfo
     * @throws ThemeInfoAttributeException
     */<commit_msg>Added console debug<commit_after><?php
namespace Karlomikus\Theme;

use Karlomikus\Theme\Exceptions\ThemeNotFoundException;
use Illuminate\Contracts\Container\Container;
use Karlomikus\Theme\Exceptions\ThemeInfoAttributeException;

/**
 * Theme
 *
 * @author <NAME>
 * @version 0.0.1
 * @package Karlomikus\Theme
 */
class Theme {

    /**
     * Scanned themes
     * @var array|ThemeInfo[]
     */
    private $themes;

    /**
     * View finder
     * @var \Illuminate\View\Factory
     */
    private $view;

    /**
     * Base theme folder
     * @var string
     */
    private $basePath;

    /**
     * Current active theme
     * @var string|null
     */
    private $activeTheme = null;

    /**
     * Setup default view finder and view paths.
     *
     * @param Container $app
     */
    public function __construct(Container $app)
    {
        // Default themes path
        $this->basePath = config('theme.path');
        
        // Config view finder
        $paths = $app['config']['view.paths'];
        $this->view = $app->make('view');
        $this->view->setFinder(new ThemeViewFinder($app['files'], $paths));

        // Scan themes
        $this->scanThemes();
    }

    /**
     * Set current active theme
     *
     * @param string $theme Theme namespace
     * @throws ThemeNotFoundException
     */
    public function setTheme($theme)
    {
        if (!$this->themeExists($theme)) {
            throw new ThemeNotFoundException($theme);
        }

        $this->loadTheme($theme);
    }

    /**
     * Get all found themes
     *
     * @return array|ThemeInfo[]
     */
    public function getThemes()
    {
        return $this->themes;
    }

    /**
     * Return currently active theme
     *
     * @return null|ThemeInfo
     */
    public function get()
    {
        return $this->themes[$this->activeTheme];
    }

    /**
     * Check if theme exists
     *
     * @param $theme
     * @return bool
     */
    public function themeExists($theme)
    {
        return array_key_exists($theme, $this->themes);
    }

    /**
     * Set base themes folder path
     *
     * @param $path
     */
    public function setDefaultThemePath($path)
    {
        $this->basePath = $path;
        $this->scanThemes();
    }

    /**
     * Load a theme
     *
     * @param string $theme
     * @throws \Exception
     */
    private function loadTheme($theme)
    {
        if (!isset($theme))
            return;

        $th = $this->findThemeByNamespace($theme);

        if (isset($th)) {
            $viewFinder = $this->view->getFinder();

            $viewFinder->prependPath($th->getPath());
            if (!is_null($th->getParent()))
                $this->loadTheme($th->getParent());

            $this->activeTheme = $theme;
        }
    }

    /**
     * Find a theme from all scanned themes
     *
     * @param string $namespace
     * @return null|ThemeInfo
     */
    private function findThemeByNamespace($namespace)
    {
        if (isset($this->themes[$namespace]))
            return $this->themes[$namespace];

        return null;
    }

    /**
     * Scan for all available themes
     *
     * @throws ThemeInfoAttributeException
     */
    private function scanThemes()
    {
        $themeDirectories = glob($this->basePath . '/*', GLOB_ONLYDIR);

        $themes = [];
        foreach ($themeDirectories as $themePath) {
            $json = $themePath . '/theme.json';

            if (file_exists($json)) {
                fwrite(STDERR, print_r($json, TRUE));
                $contents = file_get_contents($json);
                if (!$contents === false) {
                    $th = $this->parseThemeInfo(json_decode($contents, true));
                    $themes[$th->getNamespace()] = $th;
                }
            }
        }

        $this->themes = $themes;
    }

    /**
     * Find theme views path
     *
     * @param $namespace
     * @return string
     */
    private function findPath($namespace)
    {
        $path = [];
        $path[] = $this->basePath;
        $path[] = $namespace;
        $path[] = 'views';

        return implode(DIRECTORY_SEPARATOR, $path);
    }

    /**
     * Parse theme json file
     *
     * @param array $info
     * @return ThemeInfo
     * @throws ThemeInfoAttributeException
     */
{'fraction_non_alphanumeric': 0.13756051481875073, 'fraction_numerical': 0.0007084661707403471, 'mean_word_length': 1.8074245939675173, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, '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': '468482', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2428, 'n_words': 520}