<commit_before><?php

/**
 * This file is part of the ORMiny library.
 * (c) <NAME> <<EMAIL>>
 *
 * For licensing information see the LICENSE file.
 */

namespace ORMiny;

use Modules\DBAL\AbstractQueryBuilder;
use Modules\DBAL\Driver;
use ORMiny\Drivers\AnnotationMetadataDriver;

class EntityManager
{
    /**
     * @var Driver
     */
    private $driver;

    /**
     * @var string[]
     */
    private $entityClassMap = [];

    /**
     * @var Entity[]
     */
    private $entities = [];

    /**
     * @var ResultProcessor
     */
    private $resultProcessor;

    /**
     * @var string
     */
    private $defaultNamespace = '';

    /**
     * @var AnnotationMetadataDriver
     */
    private $metadataDriver;
    private $pendingQueries = [];

    public function __construct(Driver $driver, MetadataDriverInterface $metadataDriver)
    {
        $this->driver          = $driver;
        $this->metadataDriver  = $metadataDriver;
        $this->resultProcessor = new ResultProcessor($this);
    }

    /**
     * @return ResultProcessor
     */
    public function getResultProcessor()
    {
        return $this->resultProcessor;
    }

    /**
     * @return Driver
     */
    public function getDriver()
    {
        return $this->driver;
    }

    public function setDefaultNamespace($namespace)
    {
        $this->defaultNamespace = $namespace;
    }

    public function register($entityName, $className)
    {
        $this->entityClassMap[$entityName] = $className;
    }

    /**
     * Returns the name of class that $entityName handles.
     *
     * @param $entityName
     *
     * @return string
     */
    private function getEntityClassName($entityName)
    {
        if (!isset($this->entityClassMap[$entityName])) {
            $className = $entityName;
            if (!class_exists($className)) {
                $className = $this->defaultNamespace . $entityName;
                if (!class_exists($className)) {
                    $className = false;
                }
            }
            $this->entityClassMap[$entityName] = $className;
        }

        if ($this->entityClassMap[$entityName] === false) {
            throw new \OutOfBoundsException("Unknown entity {$entityName}");
        }

        return $this->entityClassMap[$entityName];
    }

    private function getEntityByClass($className)
    {
        if (!isset($this->entities[$className])) {
            $metadata = $this->metadataDriver->readEntityMetadata($className);

            $this->entities[$className] = new Entity($this, $metadata);
        }

        return $this->entities[$className];
    }

    public function getEntityForObject($object)
    {
        return $this->getEntityByClass(
            get_class($object)
        );
    }

    /**
     * @param $entityName
     *
     * @return Entity
     */
    public function get($entityName)
    {
        return $this->getEntityByClass(
            $this->getEntityClassName($entityName)
        );
    }

    /**
     * @param        $entityName
     * @param string $alias the table alias
     *
     * @return EntityFinder
     */
    public function find($entityName, $alias = null)
    {
        return $this->get($entityName)->find($alias);
    }

    public function postPendingQuery(AbstractQueryBuilder $query, array $params = [])
    {
        $this->pendingQueries[] = [$query, $params];
    }

    /**
     * Executes pending queries
     */
    public function commit()
    {
        $this->driver->inTransaction(
            function (Driver $driver, array $pendingQueries) {
                foreach ($pendingQueries as $item) {
                    list($query, $params) = $item;
                    /** @var AbstractQueryBuilder $query */
                    $query->query($params);
                }
            },
            $this->pendingQueries
        );
        $this->pendingQueries = [];<commit_msg>Add method to get the current pending queries<commit_after><?php

/**
 * This file is part of the ORMiny library.
 * (c) <NAME> <<EMAIL>>
 *
 * For licensing information see the LICENSE file.
 */

namespace ORMiny;

use Modules\DBAL\AbstractQueryBuilder;
use Modules\DBAL\Driver;
use ORMiny\Drivers\AnnotationMetadataDriver;

class EntityManager
{
    /**
     * @var Driver
     */
    private $driver;

    /**
     * @var string[]
     */
    private $entityClassMap = [];

    /**
     * @var Entity[]
     */
    private $entities = [];

    /**
     * @var ResultProcessor
     */
    private $resultProcessor;

    /**
     * @var string
     */
    private $defaultNamespace = '';

    /**
     * @var AnnotationMetadataDriver
     */
    private $metadataDriver;
    private $pendingQueries = [];

    public function __construct(Driver $driver, MetadataDriverInterface $metadataDriver)
    {
        $this->driver          = $driver;
        $this->metadataDriver  = $metadataDriver;
        $this->resultProcessor = new ResultProcessor($this);
    }

    /**
     * @return ResultProcessor
     */
    public function getResultProcessor()
    {
        return $this->resultProcessor;
    }

    /**
     * @return Driver
     */
    public function getDriver()
    {
        return $this->driver;
    }

    public function setDefaultNamespace($namespace)
    {
        $this->defaultNamespace = $namespace;
    }

    public function register($entityName, $className)
    {
        $this->entityClassMap[$entityName] = $className;
    }

    /**
     * Returns the name of class that $entityName handles.
     *
     * @param $entityName
     *
     * @return string
     */
    private function getEntityClassName($entityName)
    {
        if (!isset($this->entityClassMap[$entityName])) {
            $className = $entityName;
            if (!class_exists($className)) {
                $className = $this->defaultNamespace . $entityName;
                if (!class_exists($className)) {
                    $className = false;
                }
            }
            $this->entityClassMap[$entityName] = $className;
        }

        if ($this->entityClassMap[$entityName] === false) {
            throw new \OutOfBoundsException("Unknown entity {$entityName}");
        }

        return $this->entityClassMap[$entityName];
    }

    private function getEntityByClass($className)
    {
        if (!isset($this->entities[$className])) {
            $metadata = $this->metadataDriver->readEntityMetadata($className);

            $this->entities[$className] = new Entity($this, $metadata);
        }

        return $this->entities[$className];
    }

    public function getEntityForObject($object)
    {
        return $this->getEntityByClass(
            get_class($object)
        );
    }

    /**
     * @param $entityName
     *
     * @return Entity
     */
    public function get($entityName)
    {
        return $this->getEntityByClass(
            $this->getEntityClassName($entityName)
        );
    }

    /**
     * @param        $entityName
     * @param string $alias the table alias
     *
     * @return EntityFinder
     */
    public function find($entityName, $alias = null)
    {
        return $this->get($entityName)->find($alias);
    }

    public function postPendingQuery(AbstractQueryBuilder $query, array $params = [])
    {
        $this->pendingQueries[] = [$query, $params];
    }

    /**
     * Returns the pending queries
     *
     * The returned array has the structure of [string $query, array $parameters]
     *
     * @return array the pending queries
     */
    public function getPendingQueries()
    {
        return $this->pendingQueries;
    }

    /**
     * Executes pending queries
     */
    public function commit()
    {
        $this->driver->inTransaction(
            function (Driver $driver, array $pendingQueries) {
                foreach ($pendingQueries as $item) {
                    list($query, $params) = $item;
                    /** @var AbstractQueryBuilder $query */
                    $query->query($params);
                }
            },
            $this->pendingQueries
        );
        $this->pendingQueries = [];
{'fraction_non_alphanumeric': 0.12655117336282098, 'fraction_numerical': 0.0, 'mean_word_length': 1.96, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 66, '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': '4188384', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2172, 'n_words': 458}