<commit_before><?php
/**
 * This file is part of the BootstrapBundle project.
 *
 * (c) 2013 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace P2\Bundle\BootstrapBundle\DependencyInjection\Compiler;

use P2\Bundle\BootstrapBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Class ThemePass
 * @package P2\Bundle\BootstrapBundle\DependencyInjection\Compiler
 */
class ThemePass implements CompilerPassInterface
{
    /**
     * {@inheritDoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (! $container->hasDefinition('p2_bootstrap.theme_builder')) {
            throw new \RuntimeException('Missing theme builder service "p2_bootstrap.theme_builder".');
        }

        $processor = new Processor();
        $configs = $container->getExtensionConfig('p2_bootstrap');
        $config = $processor->processConfiguration(new Configuration(), $configs);

        if ($config['use_themes'] === true) {
            $this->buildBootstrapLess($config, $container);

            $themeBuilder = $container->getDefinition('p2_bootstrap.theme_builder');

            foreach ($container->findTaggedServiceIds('bootstrap.theme') as $id => $attributes) {
                $themeBuilder->addMethodCall('addTheme', array(new Reference($id)));
            }
        }
    }

    /**
     * Creates the bootstrap.less file for your themes.
     *
     * @param array $config
     * @param ContainerBuilder $container
     */
    protected function buildBootstrapLess(array $config, ContainerBuilder $container)
    {
        $filepath = $container->getParameterBag()->resolveValue($config['themes_path']);

        if (! is_dir($filepath)) {
            mkdir($filepath, 0777, true);
        }

        file_put_contents($filepath . '/bootstrap.less', $this->generateBootstrapLess($config, $container));
    }

    /**
     * Generates the bootstrap.less file for the given theme.
     *
     * @param array $config
     * @param ContainerBuilder $container
     *
     * @return string
     * @throws \RuntimeException
     */
    protected function generateBootstrapLess(array $config, ContainerBuilder $container)
    {
        $relativePath = $this->getRelativeBootstrapPath($config, $container);
        $imports = $this->parseImports($config, $container);

        $contents = "// imports\n";

        foreach ($imports as $import) {
            $contents .= sprintf('@import "%s/less/%s";', $relativePath, $import) . "\n";
        }

        $sourcePath = __DIR__ . '/../../Resources/less';
        $template = file_get_contents($sourcePath . '/bootstrap.less');
        $less = str_replace('//__IMPORTS__//', $contents, $template);

        if ($config['use_extensions'] === true) {
            $extensions = file_get_contents($sourcePath . '/extensions.less');
            $less .= "\n" . $extensions;
        }

        return $less;
    }

    /**
     * Parses import statements from bootstrap.less and returns an array of its values.
     *
     * @param array $config
     * @param ContainerBuilder $container
     *
     * @return array
     * @throws \RuntimeException
     */<commit_msg>Fix logical error in theme builder: variables.less was not shifted from the stack of imports, so the default bootstrap variables overwrite the theme ones.<commit_after><?php
/**
 * This file is part of the BootstrapBundle project.
 *
 * (c) 2013 <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace P2\Bundle\BootstrapBundle\DependencyInjection\Compiler;

use P2\Bundle\BootstrapBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Class ThemePass
 * @package P2\Bundle\BootstrapBundle\DependencyInjection\Compiler
 */
class ThemePass implements CompilerPassInterface
{
    /**
     * {@inheritDoc}
     */
    public function process(ContainerBuilder $container)
    {
        if (! $container->hasDefinition('p2_bootstrap.theme_builder')) {
            throw new \RuntimeException('Missing theme builder service "p2_bootstrap.theme_builder".');
        }

        $processor = new Processor();
        $configs = $container->getExtensionConfig('p2_bootstrap');
        $config = $processor->processConfiguration(new Configuration(), $configs);

        if ($config['use_themes'] === true) {
            $this->buildBootstrapLess($config, $container);

            $themeBuilder = $container->getDefinition('p2_bootstrap.theme_builder');

            foreach ($container->findTaggedServiceIds('bootstrap.theme') as $id => $attributes) {
                $themeBuilder->addMethodCall('addTheme', array(new Reference($id)));
            }
        }
    }

    /**
     * Creates the bootstrap.less file for your themes.
     *
     * @param array $config
     * @param ContainerBuilder $container
     */
    protected function buildBootstrapLess(array $config, ContainerBuilder $container)
    {
        $filepath = $container->getParameterBag()->resolveValue($config['themes_path']);

        if (! is_dir($filepath)) {
            mkdir($filepath, 0777, true);
        }

        file_put_contents($filepath . '/bootstrap.less', $this->generateBootstrapLess($config, $container));
    }

    /**
     * Generates the bootstrap.less file for the given theme.
     *
     * @param array $config
     * @param ContainerBuilder $container
     *
     * @return string
     * @throws \RuntimeException
     */
    protected function generateBootstrapLess(array $config, ContainerBuilder $container)
    {
        $relativePath = $this->getRelativeBootstrapPath($config, $container);
        $imports = $this->parseImports($config, $container);

        if ('variables.less' !== array_shift($imports)) {
            throw new \RuntimeException('Unknown bootstrap import inheritance chain.');
        }

        $contents = "// imports\n";

        foreach ($imports as $import) {
            $contents .= sprintf('@import "%s/less/%s";', $relativePath, $import) . "\n";
        }

        $sourcePath = __DIR__ . '/../../Resources/less';
        $template = file_get_contents($sourcePath . '/bootstrap.less');
        $less = str_replace('//__IMPORTS__//', $contents, $template);

        if ($config['use_extensions'] === true) {
            $extensions = file_get_contents($sourcePath . '/extensions.less');
            $less .= "\n" . $extensions;
        }

        return $less;
    }

    /**
     * Parses import statements from bootstrap.less and returns an array of its values.
     *
     * @param array $config
     * @param ContainerBuilder $container
     *
     * @return array
     * @throws \RuntimeException
     */
{'fraction_non_alphanumeric': 0.12700165654334622, 'fraction_numerical': 0.0041413583655438985, 'mean_word_length': 2.9525368248772503, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '1548762', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1884, 'n_words': 443}