<commit_before><?php

namespace Phug;

use Phug\Renderer\Adapter\EvalAdapter;
use Phug\Renderer\Adapter\FileAdapter;
use Phug\Renderer\AdapterInterface;
use Phug\Renderer\CacheInterface;
use Phug\Renderer\Event\HtmlEvent;
use Phug\Renderer\Event\RenderEvent;
use Phug\Renderer\Partial\Debug\DebuggerTrait;
use Phug\Util\ModuleContainerInterface;
use Phug\Util\Partial\ModuleContainerTrait;
use Phug\Util\SandBox;

class Renderer implements ModuleContainerInterface
{
    use ModuleContainerTrait;
    use DebuggerTrait;

    /**
     * @var Compiler
     */
    private $compiler;

    /**
     * @var AdapterInterface
     */
    private $adapter;

    /**
     * @var array
     */
    private $optionEvents = [];

    public function __construct($options = null)
    {
        $this->setOptionsDefaults($options ?: [], [
            'debug'                 => true,
            'enable_profiler'       => false,
            'up_to_date_check'      => true,
            'keep_base_name'        => false,
            'error_handler'         => null,
            'html_error'            => php_sapi_name() !== 'cli',
            'color_support'         => null,
            'error_context_lines'   => 7,
            'adapter_class_name'    => isset($options['cache_dir']) && $options['cache_dir']
                ? FileAdapter::class
                : EvalAdapter::class,
            'shared_variables'    => [],
            'globals'             => [],
            'modules'             => [],
            'compiler_class_name' => Compiler::class,
            'self'                => false,
            'on_render'           => null,
            'on_html'             => null,
            'filters'             => [
                'cdata' => function ($contents) {
                    return '<![CDATA['.trim($contents).']]>';
                },
            ],
        ]);

        $this->initCompiler();

        $this->initDebugOptions($this);

        $adapterClassName = $this->getOption('adapter_class_name');

        if (!is_a($adapterClassName, AdapterInterface::class, true)) {
            throw new RendererException(
                "Passed adapter class $adapterClassName is ".
                'not a valid '.AdapterInterface::class
            );
        }
        $this->adapter = new $adapterClassName($this, $this->getOptions());

        $this->addModules($this->getOption('modules'));
        foreach ($this->getStaticModules() as $moduleClassName) {
            $interfaces = class_implements($moduleClassName);
            if (in_array(CompilerModuleInterface::class, $interfaces) &&
                !$this->compiler->hasModule($moduleClassName)
            ) {
                $this->compiler->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'compiler_modules' => [$moduleClassName],
                ]);
            }
            if (in_array(FormatterModuleInterface::class, $interfaces) &&
                !$this->compiler->getFormatter()->hasModule($moduleClassName)
            ) {
                $this->compiler->getFormatter()->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'formatter_modules' => [$moduleClassName],
                ]);
            }
            if (in_array(ParserModuleInterface::class, $interfaces) &&
                !$this->compiler->getParser()->hasModule($moduleClassName)
            ) {
                $this->compiler->getParser()->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'parser_modules' => [$moduleClassName],
                ]);
            }
            if (in_array(LexerModuleInterface::class, $interfaces) &&
                !$this->compiler->getParser()->getLexer()->hasModule($moduleClassName)
            ) {
                $this->compiler->getParser()->getLexer()->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'lexer_modules' => [$moduleClassName],
                ]);
            }
        }
    }

    private function handleOptionAliases()
    {
        if ($this->hasOption('basedir')) {
            $basedir = $this->getOption('basedir');
            $this->setOption('paths', array_merge(
                $this->hasOption('paths')
                    ? (array) $this->getOption('paths')
                    : [],
                is_array($basedir)
                    ? $basedir
                    : [$basedir]
            ));
        }
    }

    private function mergeWithSharedVariables(array $parameters)
    {
        return array_merge(
            $this->getOption('globals'),
            $this->getOption('shared_variables'),
            $parameters
        );
    }

    private function expectCacheAdapter($adapter)
    {
        if (!($adapter instanceof CacheInterface)) {
            throw new RendererException(
                'You cannot use "cache_dir" option with '.get_class($adapter).
                ' because this adapter does not implement '.CacheInterface::class
            );
        }
    }

    /**<commit_msg>Allow extenders classes to call mergeWithSharedVariables<commit_after><?php

namespace Phug;

use Phug\Renderer\Adapter\EvalAdapter;
use Phug\Renderer\Adapter\FileAdapter;
use Phug\Renderer\AdapterInterface;
use Phug\Renderer\CacheInterface;
use Phug\Renderer\Event\HtmlEvent;
use Phug\Renderer\Event\RenderEvent;
use Phug\Renderer\Partial\Debug\DebuggerTrait;
use Phug\Util\ModuleContainerInterface;
use Phug\Util\Partial\ModuleContainerTrait;
use Phug\Util\SandBox;

class Renderer implements ModuleContainerInterface
{
    use ModuleContainerTrait;
    use DebuggerTrait;

    /**
     * @var Compiler
     */
    private $compiler;

    /**
     * @var AdapterInterface
     */
    private $adapter;

    /**
     * @var array
     */
    private $optionEvents = [];

    public function __construct($options = null)
    {
        $this->setOptionsDefaults($options ?: [], [
            'debug'                 => true,
            'enable_profiler'       => false,
            'up_to_date_check'      => true,
            'keep_base_name'        => false,
            'error_handler'         => null,
            'html_error'            => php_sapi_name() !== 'cli',
            'color_support'         => null,
            'error_context_lines'   => 7,
            'adapter_class_name'    => isset($options['cache_dir']) && $options['cache_dir']
                ? FileAdapter::class
                : EvalAdapter::class,
            'shared_variables'    => [],
            'globals'             => [],
            'modules'             => [],
            'compiler_class_name' => Compiler::class,
            'self'                => false,
            'on_render'           => null,
            'on_html'             => null,
            'filters'             => [
                'cdata' => function ($contents) {
                    return '<![CDATA['.trim($contents).']]>';
                },
            ],
        ]);

        $this->initCompiler();

        $this->initDebugOptions($this);

        $adapterClassName = $this->getOption('adapter_class_name');

        if (!is_a($adapterClassName, AdapterInterface::class, true)) {
            throw new RendererException(
                "Passed adapter class $adapterClassName is ".
                'not a valid '.AdapterInterface::class
            );
        }
        $this->adapter = new $adapterClassName($this, $this->getOptions());

        $this->addModules($this->getOption('modules'));
        foreach ($this->getStaticModules() as $moduleClassName) {
            $interfaces = class_implements($moduleClassName);
            if (in_array(CompilerModuleInterface::class, $interfaces) &&
                !$this->compiler->hasModule($moduleClassName)
            ) {
                $this->compiler->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'compiler_modules' => [$moduleClassName],
                ]);
            }
            if (in_array(FormatterModuleInterface::class, $interfaces) &&
                !$this->compiler->getFormatter()->hasModule($moduleClassName)
            ) {
                $this->compiler->getFormatter()->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'formatter_modules' => [$moduleClassName],
                ]);
            }
            if (in_array(ParserModuleInterface::class, $interfaces) &&
                !$this->compiler->getParser()->hasModule($moduleClassName)
            ) {
                $this->compiler->getParser()->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'parser_modules' => [$moduleClassName],
                ]);
            }
            if (in_array(LexerModuleInterface::class, $interfaces) &&
                !$this->compiler->getParser()->getLexer()->hasModule($moduleClassName)
            ) {
                $this->compiler->getParser()->getLexer()->addModule($moduleClassName);
                $this->setOptionsRecursive([
                    'lexer_modules' => [$moduleClassName],
                ]);
            }
        }
    }

    private function handleOptionAliases()
    {
        if ($this->hasOption('basedir')) {
            $basedir = $this->getOption('basedir');
            $this->setOption('paths', array_merge(
                $this->hasOption('paths')
                    ? (array) $this->getOption('paths')
                    : [],
                is_array($basedir)
                    ? $basedir
                    : [$basedir]
            ));
        }
    }

    private function expectCacheAdapter($adapter)
    {
        if (!($adapter instanceof CacheInterface)) {
            throw new RendererException(
                'You cannot use "cache_dir" option with '.get_class($adapter).
                ' because this adapter does not implement '.CacheInterface::class
            );
        }
    }

    /**
     * Returns merged globals, shared variables and locals.
     *
     * @param array $parameters
     *
     * @return array
     */
    protected function mergeWithSharedVariables(array $parameters)
    {
        return array_merge(
            $this->getOption('globals'),
            $this->getOption('shared_variables'),
            $parameters
        );
    }

    /**
{'fraction_non_alphanumeric': 0.1312908182865924, 'fraction_numerical': 0.0001920860545524395, 'mean_word_length': 1.7244897959183674, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 137, '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': '1267416', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2600, 'n_words': 415}