<commit_before><?php

declare(strict_types=1);

namespace whatwedo\CrudBundle\EventListener;

use Symfony\Component\HttpKernel\Event\ControllerEvent;
use whatwedo\CrudBundle\Controller\CrudDefinitionController;
use whatwedo\CrudBundle\Manager\DefinitionManager;

class CrudDefinitionListener
{
    protected $definitionManager;

    public function __construct(DefinitionManager $definitionManager)
    {
        $this->definitionManager = $definitionManager;
    }

    public function onKernelController(ControllerEvent $event)
    {
        $controller = $event->getController();

        if (! is_array($controller)) {
            return;
        }

        if ($controller[0] instanceof CrudDefinitionController) {
            try {
                $controller[0]->setDefinition(
                    $this->definitionManager->getDefinitionByRoute($event->getRequest()->attributes->get('_route'))
                );
            } catch (\InvalidArgumentException $e) {
            }
        }
    }
}
<commit_msg>feat(crud-definition-listener): set definition by resource or automatically<commit_after><?php

declare(strict_types=1);

namespace whatwedo\CrudBundle\EventListener;

use ReflectionClass;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use whatwedo\CrudBundle\Controller\CrudDefinitionController;
use whatwedo\CrudBundle\Definition\DefinitionInterface;
use whatwedo\CrudBundle\Manager\DefinitionManager;

class CrudDefinitionListener
{
    protected $definitionManager;

    public function __construct(DefinitionManager $definitionManager)
    {
        $this->definitionManager = $definitionManager;
    }

    public function onKernelController(ControllerEvent $event)
    {
        $controller = $event->getController();

        if (! is_array($controller)) {
            return;
        }

        if (! $controller[0] instanceof CrudDefinitionController) {
            return;
        }

        try {
            $controller[0]->setDefinition(
                $this->definitionManager->getDefinitionByRoute($event->getRequest()->attributes->get('_route'))
            );
        } catch (\InvalidArgumentException $e) {
            $resource = $event->getRequest()->attributes->get('_resource', '');
            $resourceImplementsDefinitionInterface = class_exists($resource)
                && in_array(DefinitionInterface::class, (new ReflectionClass($resource))->getInterfaceNames(), true);
            if ($resourceImplementsDefinitionInterface) {
                $controller[0]->setDefinition(
                    $this->definitionManager->getDefinitionByClassName($resource)
                );
                return;
            }
            foreach ($this->definitionManager->getDefinitions() as $definition) {
                if ($definition::getController() === $controller[0]::class) {
                    $controller[0]->setDefinition($definition);
                    return;
                }
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.1092436974789916, 'fraction_numerical': 0.0030252100840336134, 'mean_word_length': 2.7061021170610213, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '5537759', 'n_tokens_mistral': 738, 'n_tokens_neox': 712, 'n_words': 111}