vendor/sonata-project/admin-bundle/src/DependencyInjection/Admin/AbstractTaggedAdmin.php line 156

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\AdminBundle\DependencyInjection\Admin;
  12. use Knp\Menu\FactoryInterface;
  13. use Sonata\AdminBundle\Admin\Pool;
  14. use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
  15. use Sonata\AdminBundle\Builder\FormContractorInterface;
  16. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  17. use Sonata\AdminBundle\Builder\RouteBuilderInterface;
  18. use Sonata\AdminBundle\Builder\ShowBuilderInterface;
  19. use Sonata\AdminBundle\Datagrid\Pager;
  20. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  21. use Sonata\AdminBundle\Exporter\DataSourceInterface;
  22. use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
  23. use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface;
  24. use Sonata\AdminBundle\Model\ModelManagerInterface;
  25. use Sonata\AdminBundle\Route\RouteGeneratorInterface;
  26. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  27. use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
  28. use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
  29. use Symfony\Contracts\Translation\TranslatorInterface;
  30. /**
  31.  * @phpstan-template T of object
  32.  * @phpstan-implements TaggedAdminInterface<T>
  33.  */
  34. abstract class AbstractTaggedAdmin implements TaggedAdminInterface
  35. {
  36.     /**
  37.      * NEXT_MAJOR: Change visibility to private.
  38.      *
  39.      * The code related to the admin.
  40.      *
  41.      * @var string|null
  42.      */
  43.     protected $code;
  44.     /**
  45.      * NEXT_MAJOR: Remove this property.
  46.      *
  47.      * @deprecated since sonata-project/admin-bundle version 4.8 use $modelClass instead.
  48.      *
  49.      * The class name managed by the admin class.
  50.      *
  51.      * @var string|null
  52.      *
  53.      * @phpstan-var class-string<T>|null
  54.      */
  55.     protected $class;
  56.     /**
  57.      * NEXT_MAJOR: Change visibility to private.
  58.      *
  59.      * The base name controller used to generate the routing information.
  60.      *
  61.      * @var string|null
  62.      */
  63.     protected $baseControllerName;
  64.     /**
  65.      * The class name managed by the admin class.
  66.      *
  67.      * @phpstan-var class-string<T>|null
  68.      */
  69.     private ?string $modelClass null;
  70.     private ?string $label null;
  71.     /**
  72.      * @var non-empty-array<string, array<string, mixed>>
  73.      */
  74.     private array $listModes TaggedAdminInterface::DEFAULT_LIST_MODES;
  75.     private string $pagerType Pager::TYPE_DEFAULT;
  76.     /**
  77.      * The manager type to use for the admin.
  78.      */
  79.     private ?string $managerType null;
  80.     /**
  81.      * Roles and permissions per role.
  82.      *
  83.      * @var array<string, string[]> 'role' => ['permission1', 'permission2']
  84.      */
  85.     private array $securityInformation = [];
  86.     /**
  87.      * Component responsible for persisting filters.
  88.      */
  89.     private ?FilterPersisterInterface $filterPersister null;
  90.     /**
  91.      * The Entity or Document manager.
  92.      *
  93.      * @phpstan-var ModelManagerInterface<T>|null
  94.      */
  95.     private ?ModelManagerInterface $modelManager null;
  96.     private ?DataSourceInterface $dataSource null;
  97.     private ?FormContractorInterface $formContractor null;
  98.     private ?ShowBuilderInterface $showBuilder null;
  99.     private ?ListBuilderInterface $listBuilder null;
  100.     /**
  101.      * @phpstan-var DatagridBuilderInterface<ProxyQueryInterface<T>>|null
  102.      */
  103.     private ?DatagridBuilderInterface $datagridBuilder null;
  104.     private ?TranslatorInterface $translator null;
  105.     private ?Pool $configurationPool null;
  106.     private ?RouteGeneratorInterface $routeGenerator null;
  107.     private ?SecurityHandlerInterface $securityHandler null;
  108.     private ?FactoryInterface $menuFactory null;
  109.     private ?RouteBuilderInterface $routeBuilder null;
  110.     private ?LabelTranslatorStrategyInterface $labelTranslatorStrategy null;
  111.     private ?FieldDescriptionFactoryInterface $fieldDescriptionFactory null;
  112.     private ?MutableTemplateRegistryInterface $templateRegistry null;
  113.     /**
  114.      * NEXT_MAJOR: Remove the __construct method.
  115.      *
  116.      * @phpstan-param class-string<T>|null $class
  117.      */
  118.     public function __construct(?string $code null, ?string $class null, ?string $baseControllerName null)
  119.     {
  120.         if (\func_num_args() > 0) {
  121.             @trigger_error(
  122.                 'Setting the code, the model class and the base controller name with the constructor is deprecated'
  123.                 .' since sonata-project/admin-bundle version 4.8 and will not be possible in 5.0 version.'
  124.                 .' Use the `code`, `model_class` and `controller` attribute of the `sonata.admin` tag or'
  125.                 .' the method "setCode()", "setModelClass()" and "setBaseControllerName()" instead.',
  126.                 \E_USER_DEPRECATED
  127.             );
  128.         }
  129.         if (null !== $code) {
  130.             $this->code $code;
  131.         }
  132.         $this->class $class;
  133.         $this->modelClass $class;
  134.         if (null !== $baseControllerName) {
  135.             $this->baseControllerName $baseControllerName;
  136.         }
  137.     }
  138.     abstract public function initialize(): void;
  139.     final public function setCode(string $code): void
  140.     {
  141.         $this->code $code;
  142.     }
  143.     final public function getCode(): string
  144.     {
  145.         return $this->code ?? static::class;
  146.     }
  147.     /**
  148.      * @param class-string<T> $modelClass
  149.      */
  150.     final public function setModelClass(string $modelClass): void
  151.     {
  152.         $this->modelClass $modelClass;
  153.     }
  154.     /**
  155.      * @return class-string<T>
  156.      */
  157.     final public function getModelClass(): string
  158.     {
  159.         if (null === $this->modelClass) {
  160.             throw new \LogicException(sprintf('Admin "%s" has no model class.', static::class));
  161.         }
  162.         return $this->modelClass;
  163.     }
  164.     final public function setBaseControllerName(string $baseControllerName): void
  165.     {
  166.         $this->baseControllerName $baseControllerName;
  167.     }
  168.     final public function getBaseControllerName(): string
  169.     {
  170.         if (null === $this->baseControllerName) {
  171.             throw new \LogicException(sprintf('Admin "%s" has no base controller name.', static::class));
  172.         }
  173.         return $this->baseControllerName;
  174.     }
  175.     final public function setLabel(?string $label): void
  176.     {
  177.         $this->label $label;
  178.     }
  179.     final public function getLabel(): ?string
  180.     {
  181.         return $this->label;
  182.     }
  183.     final public function setListModes(array $listModes): void
  184.     {
  185.         $this->listModes $listModes;
  186.     }
  187.     final public function getListModes(): array
  188.     {
  189.         return $this->listModes;
  190.     }
  191.     final public function setPagerType(string $pagerType): void
  192.     {
  193.         $this->pagerType $pagerType;
  194.     }
  195.     final public function getPagerType(): string
  196.     {
  197.         return $this->pagerType;
  198.     }
  199.     final public function setManagerType(string $managerType): void
  200.     {
  201.         $this->managerType $managerType;
  202.     }
  203.     final public function getManagerType(): string
  204.     {
  205.         if (null === $this->managerType) {
  206.             throw new \LogicException(sprintf('Admin "%s" has no manager type.', static::class));
  207.         }
  208.         return $this->managerType;
  209.     }
  210.     /**
  211.      * @param array<string, string[]> $information
  212.      */
  213.     final public function setSecurityInformation(array $information): void
  214.     {
  215.         $this->securityInformation $information;
  216.     }
  217.     /**
  218.      * @return array<string, string[]>
  219.      */
  220.     final public function getSecurityInformation(): array
  221.     {
  222.         return $this->securityInformation;
  223.     }
  224.     final public function setFilterPersister(?FilterPersisterInterface $filterPersister null): void
  225.     {
  226.         $this->filterPersister $filterPersister;
  227.     }
  228.     final public function getFilterPersister(): FilterPersisterInterface
  229.     {
  230.         if (!$this->hasFilterPersister()) {
  231.             throw new \LogicException(sprintf('Admin "%s" has no filter persister.', static::class));
  232.         }
  233.         return $this->filterPersister;
  234.     }
  235.     /**
  236.      * @phpstan-assert-if-true !null $this->filterPersister
  237.      */
  238.     final public function hasFilterPersister(): bool
  239.     {
  240.         return null !== $this->filterPersister;
  241.     }
  242.     final public function setModelManager(ModelManagerInterface $modelManager): void
  243.     {
  244.         $this->modelManager $modelManager;
  245.     }
  246.     final public function getModelManager(): ModelManagerInterface
  247.     {
  248.         if (null === $this->modelManager) {
  249.             throw new \LogicException(sprintf('Admin "%s" has no model manager.', static::class));
  250.         }
  251.         return $this->modelManager;
  252.     }
  253.     final public function setDataSource(DataSourceInterface $dataSource): void
  254.     {
  255.         $this->dataSource $dataSource;
  256.     }
  257.     final public function getDataSource(): DataSourceInterface
  258.     {
  259.         if (null === $this->dataSource) {
  260.             throw new \LogicException(sprintf('Admin "%s" has no data source.', static::class));
  261.         }
  262.         return $this->dataSource;
  263.     }
  264.     final public function setFieldDescriptionFactory(FieldDescriptionFactoryInterface $fieldDescriptionFactory): void
  265.     {
  266.         $this->fieldDescriptionFactory $fieldDescriptionFactory;
  267.     }
  268.     public function getFieldDescriptionFactory(): FieldDescriptionFactoryInterface
  269.     {
  270.         if (null === $this->fieldDescriptionFactory) {
  271.             throw new \LogicException(sprintf(
  272.                 'Admin "%s" has no field description factory.',
  273.                 static::class
  274.             ));
  275.         }
  276.         return $this->fieldDescriptionFactory;
  277.     }
  278.     final public function setFormContractor(FormContractorInterface $formContractor): void
  279.     {
  280.         $this->formContractor $formContractor;
  281.     }
  282.     final public function getFormContractor(): FormContractorInterface
  283.     {
  284.         if (null === $this->formContractor) {
  285.             throw new \LogicException(sprintf('Admin "%s" has no form contractor.', static::class));
  286.         }
  287.         return $this->formContractor;
  288.     }
  289.     final public function setShowBuilder(ShowBuilderInterface $showBuilder): void
  290.     {
  291.         $this->showBuilder $showBuilder;
  292.     }
  293.     final public function getShowBuilder(): ShowBuilderInterface
  294.     {
  295.         if (null === $this->showBuilder) {
  296.             throw new \LogicException(sprintf('Admin "%s" has no show builder.', static::class));
  297.         }
  298.         return $this->showBuilder;
  299.     }
  300.     final public function setListBuilder(ListBuilderInterface $listBuilder): void
  301.     {
  302.         $this->listBuilder $listBuilder;
  303.     }
  304.     final public function getListBuilder(): ListBuilderInterface
  305.     {
  306.         if (null === $this->listBuilder) {
  307.             throw new \LogicException(sprintf('Admin "%s" has no list builder.', static::class));
  308.         }
  309.         return $this->listBuilder;
  310.     }
  311.     final public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder): void
  312.     {
  313.         $this->datagridBuilder $datagridBuilder;
  314.     }
  315.     final public function getDatagridBuilder(): DatagridBuilderInterface
  316.     {
  317.         if (null === $this->datagridBuilder) {
  318.             throw new \LogicException(sprintf('Admin "%s" has no datagrid builder.', static::class));
  319.         }
  320.         return $this->datagridBuilder;
  321.     }
  322.     final public function setTranslator(TranslatorInterface $translator): void
  323.     {
  324.         $this->translator $translator;
  325.     }
  326.     final public function getTranslator(): TranslatorInterface
  327.     {
  328.         if (null === $this->translator) {
  329.             throw new \LogicException(sprintf('Admin "%s" has no translator.', static::class));
  330.         }
  331.         return $this->translator;
  332.     }
  333.     final public function setConfigurationPool(Pool $configurationPool): void
  334.     {
  335.         $this->configurationPool $configurationPool;
  336.     }
  337.     final public function getConfigurationPool(): Pool
  338.     {
  339.         if (null === $this->configurationPool) {
  340.             throw new \LogicException(sprintf('Admin "%s" has no pool.', static::class));
  341.         }
  342.         return $this->configurationPool;
  343.     }
  344.     final public function setRouteGenerator(RouteGeneratorInterface $routeGenerator): void
  345.     {
  346.         $this->routeGenerator $routeGenerator;
  347.     }
  348.     final public function getRouteGenerator(): RouteGeneratorInterface
  349.     {
  350.         if (null === $this->routeGenerator) {
  351.             throw new \LogicException(sprintf('Admin "%s" has no route generator.', static::class));
  352.         }
  353.         return $this->routeGenerator;
  354.     }
  355.     final public function setSecurityHandler(SecurityHandlerInterface $securityHandler): void
  356.     {
  357.         $this->securityHandler $securityHandler;
  358.     }
  359.     final public function getSecurityHandler(): SecurityHandlerInterface
  360.     {
  361.         if (null === $this->securityHandler) {
  362.             throw new \LogicException(sprintf('Admin "%s" has no security handler.', static::class));
  363.         }
  364.         return $this->securityHandler;
  365.     }
  366.     final public function setMenuFactory(FactoryInterface $menuFactory): void
  367.     {
  368.         $this->menuFactory $menuFactory;
  369.     }
  370.     final public function getMenuFactory(): FactoryInterface
  371.     {
  372.         if (null === $this->menuFactory) {
  373.             throw new \LogicException(sprintf('Admin "%s" has no security handler.', static::class));
  374.         }
  375.         return $this->menuFactory;
  376.     }
  377.     final public function setRouteBuilder(RouteBuilderInterface $routeBuilder): void
  378.     {
  379.         $this->routeBuilder $routeBuilder;
  380.     }
  381.     final public function getRouteBuilder(): RouteBuilderInterface
  382.     {
  383.         if (null === $this->routeBuilder) {
  384.             throw new \LogicException(sprintf('Admin "%s" has no route builder.', static::class));
  385.         }
  386.         return $this->routeBuilder;
  387.     }
  388.     final public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy): void
  389.     {
  390.         $this->labelTranslatorStrategy $labelTranslatorStrategy;
  391.     }
  392.     final public function getLabelTranslatorStrategy(): LabelTranslatorStrategyInterface
  393.     {
  394.         if (null === $this->labelTranslatorStrategy) {
  395.             throw new \LogicException(sprintf('Admin "%s" has no label translator strategy.', static::class));
  396.         }
  397.         return $this->labelTranslatorStrategy;
  398.     }
  399.     final public function getTemplateRegistry(): MutableTemplateRegistryInterface
  400.     {
  401.         if (!$this->hasTemplateRegistry()) {
  402.             throw new \LogicException(sprintf('Unable to find the template registry for admin `%s`.', static::class));
  403.         }
  404.         return $this->templateRegistry;
  405.     }
  406.     /**
  407.      * @phpstan-assert-if-true !null $this->templateRegistry
  408.      */
  409.     final public function hasTemplateRegistry(): bool
  410.     {
  411.         return null !== $this->templateRegistry;
  412.     }
  413.     final public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry): void
  414.     {
  415.         $this->templateRegistry $templateRegistry;
  416.     }
  417.     final public function setTemplates(array $templates): void
  418.     {
  419.         $this->getTemplateRegistry()->setTemplates($templates);
  420.     }
  421.     final public function setTemplate(string $namestring $template): void
  422.     {
  423.         $this->getTemplateRegistry()->setTemplate($name$template);
  424.     }
  425. }