<commit_before><?php

/**
 * This file is, guess what, part of WebHelper.
 *
 * (c) James <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JamesRezo\WebHelper;

use Symfony\Component\Finder\Finder;
use Composer\Semver\VersionParser;
use \Twig_Loader_Filesystem;
use \Twig_Environment;

/**
 * WebHelper Repository.
 */
class WebHelperRepository
{
    /** @var Finder a Finder instance */
    private $finder;

    /** @var VersionParser a VersionParser instance */
    private $versionParser;

    /** @var array a structured array of a directives repository */
    private $memoize = [];

    /** @var Twig_Environment a Twig_Environment instance */
    private $twig;

    /**
     * Base constructor.
     *
     * @param string $resDir the Path of a Directives Repository
     */
    public function __construct($resDir)
    {
        $this->finder = new Finder();
        $this->versionParser = new VersionParser();
        $this->memoize = $this->memoize($resDir);
        $this->twig = $this->initialize($resDir);
    }

    /**
     * Initialize the Twig Environment.
     *
     * @param  string           $resDir the Path of a Directives Repository
     * @return Twig_Environment         the Twig Environment
     */
    private function initialize($resDir)
    {
        try {
            $loader = new Twig_Loader_Filesystem($resDir);
        } catch (\Twig_Error_Loader $e) {
            return null;
        }

        $twig = new Twig_Environment($loader, array(
            'cache' => __DIR__ . '/../var/cache',
        ));

        return $twig;
    }

    /**
     * Initialize the structured array of a directives repository.
     *
     * @param  string $resDir the Path of a Directives Repository
     * @return array          the structured array of a directives repository
     */
    private function memoize($resDir)
    {
        try {
            $this->finder->files()->name('*.twig')->in($resDir);
        } catch (\RuntimeException $e) {
            return [];
        } catch (\InvalidArgumentException $e) {
            return [];
        }

        $memoize = [];
        foreach ($this->finder as $file) {
            $parsedPath = explode('/', $file->getRelativePathname());
            if (count($parsedPath) == 2) {
                $parsedPath[2] = $parsedPath[1];
                $parsedPath[1] = 0;
            }
            $parsedPath[2] = str_replace('.twig', '', $parsedPath[2]);
            $memoize[$parsedPath[0]]
                [$this->versionParser->normalize($parsedPath[1])]<commit_msg>Manage Finder RunTime Exception 2<commit_after><?php

/**
 * This file is, guess what, part of WebHelper.
 *
 * (c) James <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace JamesRezo\WebHelper;

use Symfony\Component\Finder\Finder;
use Composer\Semver\VersionParser;
use \Twig_Loader_Filesystem;
use \Twig_Environment;

/**
 * WebHelper Repository.
 */
class WebHelperRepository
{
    /** @var Finder a Finder instance */
    private $finder;

    /** @var VersionParser a VersionParser instance */
    private $versionParser;

    /** @var array a structured array of a directives repository */
    private $memoize = [];

    /** @var Twig_Environment a Twig_Environment instance */
    private $twig = null;

    /**
     * Base constructor.
     *
     * @param string $resDir the Path of a Directives Repository
     */
    public function __construct($resDir)
    {
        $this->finder = new Finder();
        $this->versionParser = new VersionParser();
        if ($resDir !== '') {
            $this->memoize = $this->memoize($resDir);
            $this->twig = $this->initialize($resDir);            
        }
    }

    /**
     * Initialize the Twig Environment.
     *
     * @param  string           $resDir the Path of a Directives Repository
     * @return Twig_Environment         the Twig Environment
     */
    private function initialize($resDir)
    {
        try {
            $loader = new Twig_Loader_Filesystem($resDir);
        } catch (\Twig_Error_Loader $e) {
            return null;
        }

        $twig = new Twig_Environment($loader, array(
            'cache' => __DIR__ . '/../var/cache',
        ));

        return $twig;
    }

    /**
     * Initialize the structured array of a directives repository.
     *
     * @param  string $resDir the Path of a Directives Repository
     * @return array          the structured array of a directives repository
     */
    private function memoize($resDir)
    {
        try {
            $this->finder->files()->name('*.twig')->in($resDir);
        } catch (\InvalidArgumentException $e) {
            return [];
        }

        $memoize = [];
        foreach ($this->finder as $file) {
            $parsedPath = explode('/', $file->getRelativePathname());
            if (count($parsedPath) == 2) {
                $parsedPath[2] = $parsedPath[1];
                $parsedPath[1] = 0;
            }
            $parsedPath[2] = str_replace('.twig', '', $parsedPath[2]);
            $memoize[$parsedPath[0]]
                [$this->versionParser->normalize($parsedPath[1])]
{'fraction_non_alphanumeric': 0.1257766898889098, 'fraction_numerical': 0.0035774807004330636, 'mean_word_length': 2.133923303834808, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, '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': '940484', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1496, 'n_words': 400}