<commit_before><?php

/**
 * This file is part of TwigView.
 *
 ** (c) 2014 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace WyriHaximus\TwigView\Shell;

use Cake\Console\ConsoleIo;
use Cake\Console\Shell;
use Cake\Core\Plugin;
use WyriHaximus\TwigView\View\TwigView;

/**
 * Class CompileTemplatesShell
 * @package WyriHaximus\TwigView\Console\Command
 */
// @codingStandardsIgnoreStart
class CompileTemplatesShell extends Shell
// @codingStandardsIgnoreEnd
{

    /**
     * Instance of TwigView to be used to compile templates.
     *
     * @var TwigView
     */
    protected $twigView;

    /**
     * Constructor.
     *
     * @param ConsoleIo $consoleIo An IO instance.
     */
    public function __construct(ConsoleIo $consoleIo = null)
    {
        parent::__construct($consoleIo);

        $this->twigView = new TwigView();
    }

    /**
     * Set TwigView.
     *
     * @param TwigView $twigView TwigView instance.
     *
     * @return void
     */
    public function setTwigview(TwigView $twigView)
    {
        $this->twigView = $twigView;
    }

    /**
     * Compile all templates.
     *
     * @return void
     */
    // @codingStandardsIgnoreStart
    public function all()
    {
        $this->out('<info>Compiling all templates</info>');

        $plugins = Plugin::loaded();
        if (is_array($plugins)) {
            foreach ($plugins as $plugin) {
                $this->processPlugin($plugin);
            }
        }
    }
    // @codingStandardsIgnoreEnd

    /**
     * Compile only this plugin.
     *
     * @param string $plugin Plugin name.
     *
     * @return void
     */
    public function plugin($plugin)
    {
        $this->out('<info>Compiling one plugin\'s templates</info>');
        $this->processPlugin($plugin);
    }

    /**
     * Only compile one file.
     *
     * @param string $fileName File to compile.
     *
     * @return void
     */
    public function file($fileName)
    {
        $this->out('<info>Compiling one template</info>');
        $this->compileTemplate($fileName);
    }

    /**
     * Process plugin.
     *
     * @param string $plugin Plugin to process.
     *
     * @return void
     */
    protected function processPlugin($plugin)
    {
        if (!is_dir(Plugin::path($plugin) . 'Template' . DIRECTORY_SEPARATOR)) {
            return;
        }

        $this->walkIterator($this->setupIterator($plugin));
    }

    /**
     * Setup iterator for plugin.
     *
     * @param string $plugin Plugin to setup iterator.
     *
     * @return \RegexIterator
     */
    protected function setupIterator($plugin)
    {
        return new \RegexIterator(new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator(
                Plugin::path($plugin) . 'Template' . DIRECTORY_SEPARATOR,
                \FilesystemIterator::KEY_AS_PATHNAME |
                \FilesystemIterator::CURRENT_AS_FILEINFO |
                \FilesystemIterator::SKIP_DOTS
            ),
            \RecursiveIteratorIterator::CHILD_FIRST,
            \RecursiveIteratorIterator::CATCH_GET_CHILD
        ), '/.*?.tpl$/', \RegexIterator::GET_MATCH);
    }

    /**
     * Walk over the iterator and compile all templates.
     *
     * @param \Iterator $iterator Iterator to walk.
     *<commit_msg>Added missing app template compilation and simplefied some things<commit_after><?php

/**
 * This file is part of TwigView.
 *
 ** (c) 2014 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace WyriHaximus\TwigView\Shell;

use Cake\Console\ConsoleIo;
use Cake\Console\Shell;
use Cake\Core\Plugin;
use WyriHaximus\TwigView\View\TwigView;

/**
 * Class CompileTemplatesShell
 * @package WyriHaximus\TwigView\Console\Command
 */
// @codingStandardsIgnoreStart
class CompileTemplatesShell extends Shell
// @codingStandardsIgnoreEnd
{

    /**
     * Instance of TwigView to be used to compile templates.
     *
     * @var TwigView
     */
    protected $twigView;

    /**
     * Constructor.
     *
     * @param ConsoleIo $consoleIo An IO instance.
     */
    public function __construct(ConsoleIo $consoleIo = null)
    {
        parent::__construct($consoleIo);

        $this->twigView = new TwigView();
    }

    /**
     * Set TwigView.
     *
     * @param TwigView $twigView TwigView instance.
     *
     * @return void
     */
    public function setTwigview(TwigView $twigView)
    {
        $this->twigView = $twigView;
    }

    /**
     * Compile all templates.
     *
     * @return void
     */
    // @codingStandardsIgnoreStart
    public function all()
    {
        $this->out('<info>Compiling all templates</info>');

        $plugins = Plugin::loaded();
        if (is_array($plugins)) {
            foreach ($plugins as $plugin) {
                $this->out('<info>Compiling ' . $plugin . ' templates</info>');
                $this->processPlugin($plugin);
            }
        }

        if (is_dir(APP . 'Template' . DIRECTORY_SEPARATOR)) {
            $this->out('<info>Compiling app templates</info>');
            $this->walkIterator($this->setupIterator(APP . 'Template' . DIRECTORY_SEPARATOR));
        }
    }
    // @codingStandardsIgnoreEnd

    /**
     * Compile only this plugin.
     *
     * @param string $plugin Plugin name.
     *
     * @return void
     */
    public function plugin($plugin)
    {
        $this->out('<info>Compiling one plugin\'s templates</info>');
        $this->processPlugin($plugin);
    }

    /**
     * Only compile one file.
     *
     * @param string $fileName File to compile.
     *
     * @return void
     */
    public function file($fileName)
    {
        $this->out('<info>Compiling one template</info>');
        $this->compileTemplate($fileName);
    }

    /**
     * Process plugin.
     *
     * @param string $plugin Plugin to process.
     *
     * @return void
     */
    protected function processPlugin($plugin)
    {
        $path = Plugin::classPath($plugin) . 'Template' . DIRECTORY_SEPARATOR;
        if (!is_dir($path)) {
            return;
        }

        $this->walkIterator($this->setupIterator($path));
    }

    /**
     * Setup iterator for plugin.
     *
     * @param string $plugin Plugin to setup iterator.
     *
     * @return \RegexIterator
     */
    protected function setupIterator($path)
    {
        return new \RegexIterator(new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator(
                $path,
                \FilesystemIterator::KEY_AS_PATHNAME |
                \FilesystemIterator::CURRENT_AS_FILEINFO |
                \FilesystemIterator::SKIP_DOTS
            ),
            \RecursiveIteratorIterator::CHILD_FIRST,
            \RecursiveIteratorIterator::CATCH_GET_CHILD
        ), '/.*?.tpl$/', \RegexIterator::GET_MATCH);
    }

    /**
     * Walk over the iterator and compile all templates.
     *
     * @param \Iterator $iterator Iterator to walk.
     *
{'fraction_non_alphanumeric': 0.12097341398227598, 'fraction_numerical': 0.0011253340835560557, 'mean_word_length': 2.099389712292938, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 45, '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': '2214466', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2059, 'n_words': 457}