<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\AdminBundle\Metadata;

use Darvin\AdminBundle\Event\Metadata\MetadataEvent;
use Darvin\AdminBundle\Event\Metadata\MetadataEvents;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Metadata manager
 */
class MetadataManager
{
    const CACHE_ID = 'darvinAdminMetadata';

    /**
     * @var \Doctrine\Common\Cache\Cache
     */
    private $cache;

    /**
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
     */
    private $eventDispatcher;

    /**
     * @var \Darvin\AdminBundle\Metadata\MetadataPool
     */
    private $metadataPool;

    /**
     * @var bool
     */
    private $cacheDisabled;

    /**
     * @var array
     */
    private $entityOverride;

    /**
     * @var array
     */
    private $checkedIfHasMetadataClasses;

    /**
     * @var bool
     */
    private $initialized;

    /**
     * @var \Darvin\AdminBundle\Metadata\Metadata[]
     */
    private $metadata;

    /**
     * @param \Doctrine\Common\Cache\Cache                                $cache           Cache
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher Event dispatcher
     * @param \Darvin\AdminBundle\Metadata\MetadataPool                   $metadataPool    Metadata pool
     * @param bool                                                        $cacheDisabled   Is cache disabled
     * @param array                                                       $entityOverride  Entity override configuration
     */
    public function __construct(
        Cache $cache,
        EventDispatcherInterface $eventDispatcher,
        MetadataPool $metadataPool,
        $cacheDisabled,
        array $entityOverride
    ) {
        $this->cache = $cache;
        $this->eventDispatcher = $eventDispatcher;
        $this->metadataPool = $metadataPool;
        $this->cacheDisabled = $cacheDisabled;
        $this->entityOverride = $entityOverride;

        $this->checkedIfHasMetadataClasses = $this->metadata = [];
        $this->initialized = false;
    }

    /**
     * @param mixed $entityOrClass Entity or class
     *
     * @return bool
     */
    public function hasMetadata($entityOrClass)
    {
        $entityClass = is_object($entityOrClass) ? ClassUtils::getClass($entityOrClass) : $entityOrClass;

        if (!isset($this->checkedIfHasMetadataClasses[$entityClass])) {
            $this->checkedIfHasMetadataClasses[$entityClass] = true;

            try {
                $this->getMetadata($entityClass);
            } catch (MetadataException $ex) {
                $this->checkedIfHasMetadataClasses[$entityClass] = false;
            }
        }

        return $this->checkedIfHasMetadataClasses[$entityClass];
    }

    /**
     * @param mixed $entityOrClass Entity or class
     *
     * @return array
     */
    public function getConfiguration($entityOrClass)
    {
        return $this->getMetadata($entityOrClass)->getConfiguration();
    }

    /**
     * @param mixed $entityOrClass Entity or class
     *
     * @return \Darvin\AdminBundle\Metadata\Metadata
     * @throws \Darvin\AdminBundle\Metadata\MetadataException
     */
    public function getMetadata($entityOrClass)
    {
        $this->init();

        $entityClass = is_object($entityOrClass) ? ClassUtils::getClass($entityOrClass) : $entityOrClass;

        if (!isset($this->metadata[$entityClass])) {
            $childClass = $entityClass;

            while ($parentClass = get_parent_class($childClass)) {
                if (isset($this->metadata[$parentClass])) {
                    $this->metadata[$entityClass] = $this->metadata[$parentClass];

                    return $this->metadata[$parentClass];
                }

                $childClass = $parentClass;
            }
<commit_msg>Use entity override configuration in the MetadataManager::hasMetadata() and getMetadata().<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\AdminBundle\Metadata;

use Darvin\AdminBundle\Event\Metadata\MetadataEvent;
use Darvin\AdminBundle\Event\Metadata\MetadataEvents;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Metadata manager
 */
class MetadataManager
{
    const CACHE_ID = 'darvinAdminMetadata';

    /**
     * @var \Doctrine\Common\Cache\Cache
     */
    private $cache;

    /**
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
     */
    private $eventDispatcher;

    /**
     * @var \Darvin\AdminBundle\Metadata\MetadataPool
     */
    private $metadataPool;

    /**
     * @var bool
     */
    private $cacheDisabled;

    /**
     * @var array
     */
    private $entityOverride;

    /**
     * @var array
     */
    private $checkedIfHasMetadataClasses;

    /**
     * @var bool
     */
    private $initialized;

    /**
     * @var \Darvin\AdminBundle\Metadata\Metadata[]
     */
    private $metadata;

    /**
     * @param \Doctrine\Common\Cache\Cache                                $cache           Cache
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher Event dispatcher
     * @param \Darvin\AdminBundle\Metadata\MetadataPool                   $metadataPool    Metadata pool
     * @param bool                                                        $cacheDisabled   Is cache disabled
     * @param array                                                       $entityOverride  Entity override configuration
     */
    public function __construct(
        Cache $cache,
        EventDispatcherInterface $eventDispatcher,
        MetadataPool $metadataPool,
        $cacheDisabled,
        array $entityOverride
    ) {
        $this->cache = $cache;
        $this->eventDispatcher = $eventDispatcher;
        $this->metadataPool = $metadataPool;
        $this->cacheDisabled = $cacheDisabled;
        $this->entityOverride = $entityOverride;

        $this->checkedIfHasMetadataClasses = $this->metadata = [];
        $this->initialized = false;
    }

    /**
     * @param mixed $entityOrClass Entity or class
     *
     * @return bool
     */
    public function hasMetadata($entityOrClass)
    {
        $entityClass = is_object($entityOrClass) ? ClassUtils::getClass($entityOrClass) : $entityOrClass;

        if (isset($this->entityOverride[$entityClass])) {
            $entityClass = $this->entityOverride[$entityClass];
        }
        if (!isset($this->checkedIfHasMetadataClasses[$entityClass])) {
            $this->checkedIfHasMetadataClasses[$entityClass] = true;

            try {
                $this->getMetadata($entityClass);
            } catch (MetadataException $ex) {
                $this->checkedIfHasMetadataClasses[$entityClass] = false;
            }
        }

        return $this->checkedIfHasMetadataClasses[$entityClass];
    }

    /**
     * @param mixed $entityOrClass Entity or class
     *
     * @return array
     */
    public function getConfiguration($entityOrClass)
    {
        return $this->getMetadata($entityOrClass)->getConfiguration();
    }

    /**
     * @param mixed $entityOrClass Entity or class
     *
     * @return \Darvin\AdminBundle\Metadata\Metadata
     * @throws \Darvin\AdminBundle\Metadata\MetadataException
     */
    public function getMetadata($entityOrClass)
    {
        $this->init();

        $entityClass = is_object($entityOrClass) ? ClassUtils::getClass($entityOrClass) : $entityOrClass;

        if (isset($this->entityOverride[$entityClass])) {
            $entityClass = $this->entityOverride[$entityClass];
        }
        if (!isset($this->metadata[$entityClass])) {
            $childClass = $entityClass;

            while ($parentClass = get_parent_class($childClass)) {
                if (isset($this->metadata[$parentClass])) {
                    $this->metadata[$entityClass] = $this->metadata[$parentClass];

                    return $this->metadata[$parentClass];
                }

                $childClass = $parentClass;
            }

{'fraction_non_alphanumeric': 0.11389178542463214, 'fraction_numerical': 0.000926891437840343, 'mean_word_length': 2.2160953800298064, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '490477', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2200, 'n_words': 432}