<commit_before><?php
namespace Grav\Plugin;

use Grav\Common\Page\Collection;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Common\Page\Page;
use Grav\Common\Taxonomy;

class SimplesearchPlugin extends Plugin
{
    /**
     * @var array
     */
    protected $query;

    /**
     * @var string
     */
    protected $query_id;

    /**
     * @var Collection
     */
    protected $collection;

    /**
     * @return array
     */
    public static function getSubscribedEvents() {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0]
        ];
    }
    /**
     * Enable search only if url matches to the configuration.
     */
    public function onPluginsInitialized()
    {
        /** @var Uri $uri */
        $uri = $this->grav['uri'];
        $query = $uri->param('query');
        $route = $this->config->get('plugins.simplesearch.route');

        if ($route && $route == $uri->path() && $query) {
            $this->query = explode(',', $query);

            // disable debugger if JSON format
            if ($uri->extension() == 'json') {
                $this->config->set('system.debugger.enabled', false);
            }

            $this->enable([
                'onPagesInitialized' => ['onPagesInitialized', 0],
                'onPageInitialized' => ['onPageInitialized', 0],
                'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
                'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
            ]);
        }
    }

    /**
     * Build search results.
     */
    public function onPagesInitialized()
    {
        /** @var Taxonomy $taxonomy_map */
        $taxonomy_map = $this->grav['taxonomy'];

        $filters = (array) $this->config->get('plugins.simplesearch.filters');

        $this->collection = new Collection();
        foreach ($filters as $taxonomy => $items) {
            if (isset($items)) {
                $this->collection->append($taxonomy_map->findTaxonomy([$taxonomy => $items])->toArray());
            }
        }

        /** @var Page $page */
        foreach ($this->collection as $page) {
            foreach ($this->query as $query) {
                $query = trim($query);

                if (stripos($page->content(), $query) === false && stripos($page->title(), $query) === false) {
                    $this->collection->remove($page);
                }
            }
        }
    }

    /**
     * Create search result page.
     */
    public function onPageInitialized()
    {
        $page = new Page;
        $page->init(new \SplFileInfo(__DIR__ . '/pages/simplesearch.md'));

        // override the template is set in the config
        $template_override = $this->config->get('plugins.simplesearch.template');
        if ($template_override) {
            $page->template($template_override);
        }

        $this->grav['page'] = $page;
    }

    /**
     * Add current directory to twig lookup paths.
     */
    public function onTwigTemplatePaths()
    {
        $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
    }

    /**
     * Set needed variables to display the search results.
     */
    public function onTwigSiteVariables()
    {
        $twig = $this->grav['twig'];
        $twig->twig_vars['query'] = implode(', ', $this->query);

        $twig->twig_vars['search_results'] = $this->collection;

        if ($this->config->get('plugins.simplesearch.built_in_css')) {
            $this->grav['assets']->add('plugin://simplesearch/css/simplesearch.css');
        }
    }
}
<commit_msg>Fixes RuntimeException: Cannot override frozen service issue<commit_after><?php
namespace Grav\Plugin;

use Grav\Common\Page\Collection;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Common\Page\Page;
use Grav\Common\Taxonomy;

class SimplesearchPlugin extends Plugin
{
    /**
     * @var array
     */
    protected $query;

    /**
     * @var string
     */
    protected $query_id;

    /**
     * @var Collection
     */
    protected $collection;

    /**
     * @return array
     */
    public static function getSubscribedEvents() {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0]
        ];
    }
    /**
     * Enable search only if url matches to the configuration.
     */
    public function onPluginsInitialized()
    {
        /** @var Uri $uri */
        $uri = $this->grav['uri'];
        $query = $uri->param('query');
        $route = $this->config->get('plugins.simplesearch.route');

        if ($route && $route == $uri->path() && $query) {
            $this->query = explode(',', $query);

            // disable debugger if JSON format
            if ($uri->extension() == 'json') {
                $this->config->set('system.debugger.enabled', false);
            }

            $this->enable([
                'onPagesInitialized' => ['onPagesInitialized', 0],
                'onPageInitialized' => ['onPageInitialized', 0],
                'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
                'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
            ]);
        }
    }

    /**
     * Build search results.
     */
    public function onPagesInitialized()
    {
        /** @var Taxonomy $taxonomy_map */
        $taxonomy_map = $this->grav['taxonomy'];

        $filters = (array) $this->config->get('plugins.simplesearch.filters');

        $this->collection = new Collection();
        foreach ($filters as $taxonomy => $items) {
            if (isset($items)) {
                $this->collection->append($taxonomy_map->findTaxonomy([$taxonomy => $items])->toArray());
            }
        }

        /** @var Page $page */
        foreach ($this->collection as $page) {
            foreach ($this->query as $query) {
                $query = trim($query);

                if (stripos($page->content(), $query) === false && stripos($page->title(), $query) === false) {
                    $this->collection->remove($page);
                }
            }
        }
    }

    /**
     * Create search result page.
     */
    public function onPageInitialized()
    {
        $page = new Page;
        $page->init(new \SplFileInfo(__DIR__ . '/pages/simplesearch.md'));

        // override the template is set in the config
        $template_override = $this->config->get('plugins.simplesearch.template');
        if ($template_override) {
            $page->template($template_override);
        }

        // allows us to redefine the page service without triggering RuntimeException: Cannot override frozen service
        // "page" issue
        unset($this->grav['page']);
        
        $this->grav['page'] = $page;
    }

    /**
     * Add current directory to twig lookup paths.
     */
    public function onTwigTemplatePaths()
    {
        $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
    }

    /**
     * Set needed variables to display the search results.
     */
    public function onTwigSiteVariables()
    {
        $twig = $this->grav['twig'];
        $twig->twig_vars['query'] = implode(', ', $this->query);

        $twig->twig_vars['search_results'] = $this->collection;

        if ($this->config->get('plugins.simplesearch.built_in_css')) {
            $this->grav['assets']->add('plugin://simplesearch/css/simplesearch.css');
        }
    }
}

{'fraction_non_alphanumeric': 0.1582244008714597, 'fraction_numerical': 0.0013616557734204794, 'mean_word_length': 1.9906351791530945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 98, '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': '573000', 'n_tokens_mistral': 2132, 'n_tokens_neox': 2065, 'n_words': 425}