<commit_before><?php
namespace Bolt\Asset\Snippet;

use Bolt\Asset\QueueInterface;
use Bolt\Asset\Target;
use Bolt\Controller\Zone;
use Silex\Application;

/**
 * Snippet queue processor.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Queue implements QueueInterface
{
    /** @var boolean Whether to add jQuery to the HTML */
    protected $addJquery;
    /** @var Snippet[] Queue with snippets of HTML to insert. */
    protected $queue = [];

    /** @var \Silex\Application */
    private $app;
    /** @var array */
    private $matchedComments;

    /**
     * Constructor.
     *
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * Insert a snippet. And by 'insert' we actually mean 'add it to the queue,
     * to be processed later'.
     *
     * @param string          $location
     * @param callable|string $callback
     * @param string          $extensionName
     * @param array|null      $parameters
     */
    public function add($location, $callback, $extensionName = 'core', array $parameters = [])
    {
        $callback = $this->getCallableResult($extensionName, $callback, $parameters);
        $this->queue[] = new Snippet($location, $callback, $extensionName, $parameters);
    }

    /**
     * {@inheritdoc}
     */
    public function clear()
    {
        $this->queue = [];
    }

    /**
     * {@inheritdoc}
     */
    public function process($html)
    {
        // First, gather all html <!-- comments -->, because they shouldn't be
        // considered for replacements. We use a callback, so we can fill our
        // $this->matchedComments array
        $html = preg_replace_callback('/<!--(.*)-->/Uis', [$this, 'pregCallback'], $html);

        // Process the snippets in the queue.
        foreach ($this->queue as $key => $asset) {
            $html = $this->app['asset.injector']->inject($asset, $asset->getLocation(), $html);
            unset($this->queue[$key]);
        }

        // Conditionally add jQuery
        $html = $this->addJquery($html);

        // Finally, replace back ###comment### with its original comment.
        if (!empty($this->matchedComments)) {
            $html = preg_replace(array_keys($this->matchedComments), $this->matchedComments, $html, 1);
        }

        return $html;
    }

    /**
     * Get the queued snippets.
     *
     * @return \Bolt\Asset\Snippet\Snippet[]
     */
    public function getQueue()
    {
        return $this->queue;
    }

    /**
     * Insert jQuery, if it's not inserted already.
     *
     * Some of the patterns that 'match' are:
     * - jquery.js
     * - jquery.min.js
     * - jquery-latest.js
     * - jquery-latest.min.js
     * - jquery-1.8.2.min.js
     * - jquery-1.5.js
     *
     * @param string $html
     *
     * @return string HTML
     */
    protected function addJquery($html)
    {
        if (!$this->app['config']->get('general/add_jquery', false)) {
            return $html;
        }

        $zone = Zone::FRONTEND;
        /** @var RequestStack $requestStack */
        $requestStack = $this->app['request_stack'];
        if ($request = $requestStack->getCurrentRequest()) {
            $zone = Zone::get($request);
        }

        $regex = '/<script(.*)jquery(-latest|-[0-9\.]*)?(\.min)?\.js/';
        if ($zone === Zone::FRONTEND && !preg_match($regex, $html)) {
            $jqueryfile = $this->app['resources']->getPath('app/view/js/jquery-2.1.4.min.js');
            $asset = new Snippet(Target::BEFORE_JS, '<script src="' . $jqueryfile . '"></script>');
            $html = $this->app['asset.injector']->inject($asset, $asset->getLocation(), $html);
        }

        return $html;<commit_msg>Allow the theme's config.yml to set add_jquery. Fixes #4098<commit_after><?php
namespace Bolt\Asset\Snippet;

use Bolt\Asset\QueueInterface;
use Bolt\Asset\Target;
use Bolt\Controller\Zone;
use Silex\Application;

/**
 * Snippet queue processor.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Queue implements QueueInterface
{
    /** @var boolean Whether to add jQuery to the HTML */
    protected $addJquery;
    /** @var Snippet[] Queue with snippets of HTML to insert. */
    protected $queue = [];

    /** @var \Silex\Application */
    private $app;
    /** @var array */
    private $matchedComments;

    /**
     * Constructor.
     *
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * Insert a snippet. And by 'insert' we actually mean 'add it to the queue,
     * to be processed later'.
     *
     * @param string          $location
     * @param callable|string $callback
     * @param string          $extensionName
     * @param array|null      $parameters
     */
    public function add($location, $callback, $extensionName = 'core', array $parameters = [])
    {
        $callback = $this->getCallableResult($extensionName, $callback, $parameters);
        $this->queue[] = new Snippet($location, $callback, $extensionName, $parameters);
    }

    /**
     * {@inheritdoc}
     */
    public function clear()
    {
        $this->queue = [];
    }

    /**
     * {@inheritdoc}
     */
    public function process($html)
    {
        // First, gather all html <!-- comments -->, because they shouldn't be
        // considered for replacements. We use a callback, so we can fill our
        // $this->matchedComments array
        $html = preg_replace_callback('/<!--(.*)-->/Uis', [$this, 'pregCallback'], $html);

        // Process the snippets in the queue.
        foreach ($this->queue as $key => $asset) {
            $html = $this->app['asset.injector']->inject($asset, $asset->getLocation(), $html);
            unset($this->queue[$key]);
        }

        // Conditionally add jQuery
        $html = $this->addJquery($html);

        // Finally, replace back ###comment### with its original comment.
        if (!empty($this->matchedComments)) {
            $html = preg_replace(array_keys($this->matchedComments), $this->matchedComments, $html, 1);
        }

        return $html;
    }

    /**
     * Get the queued snippets.
     *
     * @return \Bolt\Asset\Snippet\Snippet[]
     */
    public function getQueue()
    {
        return $this->queue;
    }

    /**
     * Insert jQuery, if it's not inserted already.
     *
     * Some of the patterns that 'match' are:
     * - jquery.js
     * - jquery.min.js
     * - jquery-latest.js
     * - jquery-latest.min.js
     * - jquery-1.8.2.min.js
     * - jquery-1.5.js
     *
     * @param string $html
     *
     * @return string HTML
     */
    protected function addJquery($html)
    {
        if (!$this->app['config']->get('general/add_jquery', false) &&
            !$this->app['config']->get('theme/add_jquery', false)) {
            return $html;
        }

        $zone = Zone::FRONTEND;
        /** @var RequestStack $requestStack */
        $requestStack = $this->app['request_stack'];
        if ($request = $requestStack->getCurrentRequest()) {
            $zone = Zone::get($request);
        }

        $regex = '/<script(.*)jquery(-latest|-[0-9\.]*)?(\.min)?\.js/';
        if ($zone === Zone::FRONTEND && !preg_match($regex, $html)) {
            $jqueryfile = $this->app['resources']->getPath('app/view/js/jquery-2.1.4.min.js');
            $asset = new Snippet(Target::BEFORE_JS, '<script src="' . $jqueryfile . '"></script>');
            $html = $this->app['asset.injector']->inject($asset, $asset->getLocation(), $html);
        }

        return $html;
{'fraction_non_alphanumeric': 0.16603477578768466, 'fraction_numerical': 0.0033991371421100798, 'mean_word_length': 2.1650806785270995, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 75, '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': '5366951', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2280, 'n_words': 541}