<commit_before><?php
namespace Bolt\Assets\File;

use Silex\Application;

/**
 * File asset queue processor.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Queue
{
    /** @var \Silex\Application */
    private $app;
    /** @var AssetBase[] */
    private $stylesheet = [];
    /** @var AssetBase[] */
    private $javascript = [];

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

    /**
     * Add a file asset to the queue.
     *
     * @param string $type
     * @param string $fileName
     * @param array  $options
     *
     * @throws \InvalidArgumentException
     */
    public function add($type, $fileName, array $options = [])
    {
        if ($type === 'javascript') {
            $this->javascript[md5($fileName)] = new JavaScript($fileName, $options);
        } elseif ($type === 'stylesheet') {
            $this->stylesheet[md5($fileName)] = new Stylesheet($fileName, $options);
        } else {
            throw new \InvalidArgumentException("Requested asset type of '$type' is not valid.");
        }
    }

    /**
     * Insert all assets in template. Use sorting by priority.
     *
     * @param $html
     *
     * @return string
     */
    public function process($html)
    {
        foreach ($this->sort($this->javascript) as $asset) {
            $html = $this->processJsAssets($asset, $html);
        }

        foreach ($this->sort($this->stylesheet) as $asset) {
            $html = $this->processCssAssets($asset, $html);
        }

        return $html;
    }

    protected function processCssAssets(AssetBase $asset, $html)
    {
        if ($asset->isLate()) {
            $html = $this->app['assets.injector']->bodyTagEnd($html, (string) $asset);
        } else {
            $html = $this->app['assets.injector']->cssTagsBefore($html, (string) $asset);
        }

        return $html;
    }

    protected function processJsAssets(AssetBase $asset, $html)
    {
        if ($asset->isLate()) {
            $html = $this->app['assets.injector']->bodyTagEnd($html, (string) $asset);
        } else {
            $html = $this->app['assets.injector']->jsTagsAfter($html, (string) $asset);
        }

        return $html;
    }

    /**
     * Do a Schwartzian Transform for stable sort
     *
     * @see http://en.wikipedia.org/wiki/Schwartzian_transform
     *
     * @param array $files
     *
     * @return array
     */
    private function sort(array $files)
    {
        // We use create_function(), because it's faster than closure decorate
        array_walk($files, create_function('&$v, $k', '$v = [$v[\'priority\'], $k, $v];'));
        // sort
        sort($files);
        // undecorate
        array_walk($files, create_function('&$v, $k', '$v = $v[2];'));

        return $files;
    }
}
<commit_msg>Don't use slow, vulnerable create_function()<commit_after><?php
namespace Bolt\Assets\File;

use Silex\Application;

/**
 * File asset queue processor.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Queue
{
    /** @var \Silex\Application */
    private $app;
    /** @var AssetBase[] */
    private $stylesheet = [];
    /** @var AssetBase[] */
    private $javascript = [];

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

    /**
     * Add a file asset to the queue.
     *
     * @param string $type
     * @param string $fileName
     * @param array  $options
     *
     * @throws \InvalidArgumentException
     */
    public function add($type, $fileName, array $options = [])
    {
        if ($type === 'javascript') {
            $this->javascript[md5($fileName)] = new JavaScript($fileName, $options);
        } elseif ($type === 'stylesheet') {
            $this->stylesheet[md5($fileName)] = new Stylesheet($fileName, $options);
        } else {
            throw new \InvalidArgumentException("Requested asset type of '$type' is not valid.");
        }
    }

    /**
     * Insert all assets in template. Use sorting by priority.
     *
     * @param $html
     *
     * @return string
     */
    public function process($html)
    {
        foreach ($this->sort($this->javascript) as $asset) {
            $html = $this->processJsAssets($asset, $html);
        }

        foreach ($this->sort($this->stylesheet) as $asset) {
            $html = $this->processCssAssets($asset, $html);
        }

        return $html;
    }

    protected function processCssAssets(AssetBase $asset, $html)
    {
        if ($asset->isLate()) {
            $html = $this->app['assets.injector']->bodyTagEnd($html, (string) $asset);
        } else {
            $html = $this->app['assets.injector']->cssTagsBefore($html, (string) $asset);
        }

        return $html;
    }

    protected function processJsAssets(AssetBase $asset, $html)
    {
        if ($asset->isLate()) {
            $html = $this->app['assets.injector']->bodyTagEnd($html, (string) $asset);
        } else {
            $html = $this->app['assets.injector']->jsTagsAfter($html, (string) $asset);
        }

        return $html;
    }

    /**
     * Do a Schwartzian Transform for stable sort
     *
     * @see http://en.wikipedia.org/wiki/Schwartzian_transform
     *
     * @param AssetBase[] $files
     *
     * @return AssetBase[]
     */
    private function sort(array $files)
    {
    	array_walk($files, function(&$v, $k) {$v = [$v->getPriority(), $k, $v];});
    	sort($files);
    	array_walk($files, function(&$v, $k) {$v = $v[2];});

        return $files;
    }
}

{'fraction_non_alphanumeric': 0.16573033707865167, 'fraction_numerical': 0.001053370786516854, 'mean_word_length': 2.1423055708769994, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 54, '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': '326203', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1730, 'n_words': 393}