<commit_before><?php
/**
 * PHP-DI
 *
 * @link      http://php-di.org/
 * @copyright <NAME> (http://mnapoli.fr/)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
 */

namespace DI\Bridge\Symfony;

use DI\ContainerBuilder as DiContainerBuilder;
use Psr\Container\ContainerInterface;
use Symfony\Component\Debug\DebugClassLoader;
use Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Customization of Symfony's kernel to setup PHP-DI.
 *
 * Extend this class instead of Symfony's base kernel.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class Kernel extends \Symfony\Component\HttpKernel\Kernel
{
    /**
     * @var ContainerInterface
     */
    private $phpdiContainer;

    public function __construct($environment, $debug)
    {
        parent::__construct($environment, $debug);
        $this->disableDebugClassLoader();
    }

    /**
     * Implement this method to configure PHP-DI.
     *
     * @param DiContainerBuilder $builder
     *
     * @return ContainerInterface
     */
    abstract protected function buildPHPDIContainer(DiContainerBuilder $builder);

    protected function getContainerBaseClass()
    {
        return SymfonyContainerBridge::class;
    }

    protected function buildContainer()
    {
        $containerBuilder = parent::buildContainer();

        $this->removeInvalidReferenceBehaviorPass($containerBuilder);

        return $containerBuilder;
    }

    protected function initializeContainer()
    {
        parent::initializeContainer();

        /** @var SymfonyContainerBridge $rootContainer */
        $rootContainer = $this->getContainer();

        $rootContainer->setFallbackContainer($this->getPHPDIContainer());
    }

    /**
     * Remove the CheckExceptionOnInvalidReferenceBehaviorPass because
     * it was not looking into PHP-DI's entries and thus throwing exceptions.
     *
     * @todo Replace it by an alternative that can search into PHP-DI too
     *       Problem: PHP-DI is not initialized when Symfony's container is compiled, because
     *       it depends on Symfony's container for fallback (cycle…)
     *
     * @param ContainerBuilder $container
     */
    private function removeInvalidReferenceBehaviorPass(ContainerBuilder $container)
    {
        $passConfig = $container->getCompilerPassConfig();
        $compilationPasses = $passConfig->getRemovingPasses();

        foreach ($compilationPasses as $i => $pass) {
            if ($pass instanceof CheckExceptionOnInvalidReferenceBehaviorPass) {
                unset($compilationPasses[$i]);
                break;
            }
        }

        $passConfig->setRemovingPasses($compilationPasses);
    }

    private function disableDebugClassLoader()
    {
        if (!class_exists('Symfony\Component\Debug\DebugClassLoader')) {
            return;
        }

        DebugClassLoader::disable();
    }

    /**
     * @return ContainerInterface
     */
    protected function getPHPDIContainer()
    {
        if ($this->phpdiContainer === null) {
            $builder = new DiContainerBuilder();
            $builder->wrapContainer($this->getContainer());

            $this->phpdiContainer = $this->buildPHPDIContainer($builder);
        }

        return $this->phpdiContainer;
    }
}<commit_msg>support PHP DI 6 and SF 3.4<commit_after><?php
/**
 * PHP-DI
 *
 * @link      http://php-di.org/
 * @copyright <NAME> (http://mnapoli.fr/)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
 */

namespace DI\Bridge\Symfony;

use DI\ContainerBuilder as DiContainerBuilder;
use Psr\Container\ContainerInterface;
use Symfony\Component\Debug\DebugClassLoader;
use Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Customization of Symfony's kernel to setup PHP-DI.
 *
 * Extend this class instead of Symfony's base kernel.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class Kernel extends \Symfony\Component\HttpKernel\Kernel
{
    /**
     * @var ContainerInterface
     */
    private $phpdiContainer;

    public function __construct($environment, $debug)
    {
        parent::__construct($environment, $debug);
        $this->disableDebugClassLoader();
    }

    /**
     * Implement this method to configure PHP-DI.
     *
     * @param DiContainerBuilder $builder
     *
     * @return ContainerInterface
     */
    abstract protected function buildPHPDIContainer(DiContainerBuilder $builder);

    protected function getContainerBaseClass()
    {
        return SymfonyContainerBridge::class;
    }

    protected function buildContainer()
    {
        $containerBuilder = parent::buildContainer();

        $this->removeInvalidReferenceBehaviorPass($containerBuilder);

        return $containerBuilder;
    }

    protected function initializeContainer()
    {
        parent::initializeContainer();

        /** @var SymfonyContainerBridge $rootContainer */
        $rootContainer = $this->getContainer();

        $rootContainer->setFallbackContainer($this->getPHPDIContainer());
    }

    /**
     * Remove the CheckExceptionOnInvalidReferenceBehaviorPass because
     * it was not looking into PHP-DI's entries and thus throwing exceptions.
     *
     * @todo Replace it by an alternative that can search into PHP-DI too
     *       Problem: PHP-DI is not initialized when Symfony's container is compiled, because
     *       it depends on Symfony's container for fallback (cycle…)
     *
     * @param ContainerBuilder $container
     */
    private function removeInvalidReferenceBehaviorPass(ContainerBuilder $container)
    {
        $passConfig = $container->getCompilerPassConfig();
        $compilationPasses = $passConfig->getRemovingPasses();

        foreach ($compilationPasses as $i => $pass) {
            if ($pass instanceof CheckExceptionOnInvalidReferenceBehaviorPass) {
                unset($compilationPasses[$i]);
                break;
            }
        }

        $passConfig->setRemovingPasses($compilationPasses);
    }

    private function disableDebugClassLoader()
    {
        if (!class_exists(DebugClassLoader::class)) {
            return;
        }

        DebugClassLoader::disable();
    }

    /**
     * @return ContainerInterface
     */
    protected function getPHPDIContainer()
    {
        if ($this->phpdiContainer === null) {
            $builder = new DiContainerBuilder();
            $builder->wrapContainer($this->getContainer());

            $this->phpdiContainer = $this->buildPHPDIContainer($builder);
        }

        return $this->phpdiContainer;
    }
}
{'fraction_non_alphanumeric': 0.10509225092250922, 'fraction_numerical': 0.00044280442804428045, 'mean_word_length': 2.90097869890616, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1821700', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1734, 'n_words': 392}