<commit_before><?php

/*
 * This file is part of Mannequin.
 *
 * (c) 2017 Last Call Media, <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace LastCall\Mannequin\Drupal\Driver;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Template\TwigExtension;
use Drupal\Core\Theme\ThemeManagerInterface;
use LastCall\Mannequin\Core\Cache\NullCacheItemPool;
use LastCall\Mannequin\Drupal\Drupal\MannequinDateFormatter;
use LastCall\Mannequin\Drupal\Drupal\MannequinExtensionDiscovery;
use LastCall\Mannequin\Drupal\Drupal\MannequinRenderer;
use LastCall\Mannequin\Drupal\Drupal\MannequinThemeManager;
use LastCall\Mannequin\Drupal\Drupal\MannequinUrlGenerator;
use LastCall\Mannequin\Twig\Driver\SimpleTwigDriver;
use Psr\Cache\CacheItemPoolInterface;

class DrupalTwigDriver extends SimpleTwigDriver
{
    private $booted;
    private $drupalRoot;
    private $cache;
    private $twigOptions;

    public function __construct(string $drupalRoot, array $twigOptions = [], CacheItemPoolInterface $cache = null)
    {
        if (!is_dir($drupalRoot)) {
            throw new \InvalidArgumentException(sprintf('Drupal root %s does not exist', $drupalRoot));
        }
        if (!file_exists(sprintf('%s/autoload.php', $drupalRoot))) {
            throw new \InvalidArgumentException(sprintf('Directory %s does not look like a Drupal installation', $drupalRoot));
        }
        $this->drupalRoot = $drupalRoot;
        $this->twigOptions = $twigOptions;
        $this->cache = $cache ?: new NullCacheItemPool();
    }

    public function getTwigRoot(): string
    {
        return $this->drupalRoot;
    }

    protected function createTwig(): \Twig_Environment
    {
        $this->boot();
        $twig = new \Twig_Environment($this->createLoader(), $this->twigOptions);
        $extension = new TwigExtension(
            $this->getRenderer(),
            $this->getGenerator(),
            $this->getThemeManager(),
            $this->getDateFormatter()
        );
        $twig->addExtension($extension);

        return $twig;
    }

    private function createLoader()
    {
        $this->boot();

        $loader = new \Twig_Loader_Filesystem([$this->drupalRoot], $this->drupalRoot);
        $discovery = new MannequinExtensionDiscovery($this->drupalRoot, $this->cache);
        foreach ($discovery->scan('module', false) as $key => $extension) {
            $dir = sprintf('%s/templates', $extension->getPath());
            if (is_dir(sprintf('%s/%s', $this->drupalRoot, $dir))) {
                $loader->addPath($key, $dir);
            }
        }
        foreach ($discovery->scan('theme', false) as $key => $extension) {
            $dir = sprintf('%s/templates', $extension->getPath());
            if (is_dir(sprintf('%s/%s', $this->drupalRoot, $dir))) {
                $loader->addPath($key, $dir);
            }
        }

        return $loader;
    }

    private function boot()
    {
        if (!$this->booted) {
            $this->booted = true;
            require sprintf('%s/autoload.php', $this->drupalRoot);
            require_once sprintf('%s/core/includes/bootstrap.inc', $this->drupalRoot);
        }
    }

    private function getRenderer(): RendererInterface
    {
        return new MannequinRenderer();
    }

    private function getGenerator(): UrlGeneratorInterface
    {
        return new MannequinUrlGenerator();
    }

    private function getThemeManager(): ThemeManagerInterface
    {
        return new MannequinThemeManager();
    }

    private function getDateFormatter(): DateFormatterInterface
    {
        return new MannequinDateFormatter();
    }
}
<commit_msg>Fix argument order for Twig_Loader_Filesystem::addPath<commit_after><?php

/*
 * This file is part of Mannequin.
 *
 * (c) 2017 Last Call Media, <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace LastCall\Mannequin\Drupal\Driver;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Template\TwigExtension;
use Drupal\Core\Theme\ThemeManagerInterface;
use LastCall\Mannequin\Core\Cache\NullCacheItemPool;
use LastCall\Mannequin\Drupal\Drupal\MannequinDateFormatter;
use LastCall\Mannequin\Drupal\Drupal\MannequinExtensionDiscovery;
use LastCall\Mannequin\Drupal\Drupal\MannequinRenderer;
use LastCall\Mannequin\Drupal\Drupal\MannequinThemeManager;
use LastCall\Mannequin\Drupal\Drupal\MannequinUrlGenerator;
use LastCall\Mannequin\Twig\Driver\SimpleTwigDriver;
use Psr\Cache\CacheItemPoolInterface;

class DrupalTwigDriver extends SimpleTwigDriver
{
    private $booted;
    private $drupalRoot;
    private $cache;
    private $twigOptions;

    public function __construct(string $drupalRoot, array $twigOptions = [], CacheItemPoolInterface $cache = null)
    {
        if (!is_dir($drupalRoot)) {
            throw new \InvalidArgumentException(sprintf('Drupal root %s does not exist', $drupalRoot));
        }
        if (!file_exists(sprintf('%s/autoload.php', $drupalRoot))) {
            throw new \InvalidArgumentException(sprintf('Directory %s does not look like a Drupal installation', $drupalRoot));
        }
        $this->drupalRoot = $drupalRoot;
        $this->twigOptions = $twigOptions;
        $this->cache = $cache ?: new NullCacheItemPool();
    }

    public function getTwigRoot(): string
    {
        return $this->drupalRoot;
    }

    protected function createTwig(): \Twig_Environment
    {
        $this->boot();
        $twig = new \Twig_Environment($this->createLoader(), $this->twigOptions);
        $extension = new TwigExtension(
            $this->getRenderer(),
            $this->getGenerator(),
            $this->getThemeManager(),
            $this->getDateFormatter()
        );
        $twig->addExtension($extension);

        return $twig;
    }

    private function createLoader()
    {
        $this->boot();

        $loader = new \Twig_Loader_Filesystem([$this->drupalRoot], $this->drupalRoot);
        $discovery = new MannequinExtensionDiscovery($this->drupalRoot, $this->cache);
        foreach ($discovery->scan('module', false) as $key => $extension) {
            $dir = sprintf('%s/templates', $extension->getPath());
            if (is_dir(sprintf('%s/%s', $this->drupalRoot, $dir))) {
                $loader->addPath($dir, $key);
            }
        }
        foreach ($discovery->scan('theme', false) as $key => $extension) {
            $dir = sprintf('%s/templates', $extension->getPath());
            if (is_dir(sprintf('%s/%s', $this->drupalRoot, $dir))) {
                $loader->addPath($dir, $key);
            }
        }

        return $loader;
    }

    private function boot()
    {
        if (!$this->booted) {
            $this->booted = true;
            require sprintf('%s/autoload.php', $this->drupalRoot);
            require_once sprintf('%s/core/includes/bootstrap.inc', $this->drupalRoot);
        }
    }

    private function getRenderer(): RendererInterface
    {
        return new MannequinRenderer();
    }

    private function getGenerator(): UrlGeneratorInterface
    {
        return new MannequinUrlGenerator();
    }

    private function getThemeManager(): ThemeManagerInterface
    {
        return new MannequinThemeManager();
    }

    private function getDateFormatter(): DateFormatterInterface
    {
        return new MannequinDateFormatter();
    }
}

{'fraction_non_alphanumeric': 0.12635143936433502, 'fraction_numerical': 0.0010420737267161652, 'mean_word_length': 2.959773078906653, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 71, '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': '5201984', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2241, 'n_words': 458}