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

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.      * @var string|null
  68.      *
  69.      * @phpstan-var class-string<T>|null
  70.      */
  71.     private $modelClass;
  72.     /**
  73.      * @var string|null
  74.      */
  75.     private $label;
  76.     /**
  77.      * @var non-empty-array<string, array<string, mixed>>
  78.      */
  79.     private $listModes TaggedAdminInterface::DEFAULT_LIST_MODES;
  80.     /**
  81.      * @var string
  82.      */
  83.     private $pagerType Pager::TYPE_DEFAULT;
  84.     /**
  85.      * The manager type to use for the admin.
  86.      *
  87.      * @var string|null
  88.      */
  89.     private $managerType;
  90.     /**
  91.      * Roles and permissions per role.
  92.      *
  93.      * @var array<string, string[]> 'role' => ['permission1', 'permission2']
  94.      */
  95.     private $securityInformation = [];
  96.     /**
  97.      * Component responsible for persisting filters.
  98.      *
  99.      * @var FilterPersisterInterface|null
  100.      */
  101.     private $filterPersister;
  102.     /**
  103.      * The Entity or Document manager.
  104.      *
  105.      * @var ModelManagerInterface|null
  106.      * @phpstan-var ModelManagerInterface<T>|null
  107.      */
  108.     private $modelManager;
  109.     /**
  110.      * @var DataSourceInterface|null
  111.      */
  112.     private $dataSource;
  113.     /**
  114.      * The related form contractor.
  115.      *
  116.      * @var FormContractorInterface|null
  117.      */
  118.     private $formContractor;
  119.     /**
  120.      * The related view builder.
  121.      *
  122.      * @var ShowBuilderInterface|null
  123.      */
  124.     private $showBuilder;
  125.     /**
  126.      * The related list builder.
  127.      *
  128.      * @var ListBuilderInterface|null
  129.      */
  130.     private $listBuilder;
  131.     /**
  132.      * The related datagrid builder.
  133.      *
  134.      * @var DatagridBuilderInterface<ProxyQueryInterface>|null
  135.      */
  136.     private $datagridBuilder;
  137.     /**
  138.      * The translator component.
  139.      *
  140.      * @var TranslatorInterface|null
  141.      */
  142.     private $translator;
  143.     /**
  144.      * The configuration pool.
  145.      *
  146.      * @var Pool|null
  147.      */
  148.     private $configurationPool;
  149.     /**
  150.      * The router instance.
  151.      *
  152.      * @var RouteGeneratorInterface|null
  153.      */
  154.     private $routeGenerator;
  155.     /**
  156.      * @var SecurityHandlerInterface|null
  157.      */
  158.     private $securityHandler;
  159.     /**
  160.      * @var FactoryInterface|null
  161.      */
  162.     private $menuFactory;
  163.     /**
  164.      * @var RouteBuilderInterface|null
  165.      */
  166.     private $routeBuilder;
  167.     /**
  168.      * @var LabelTranslatorStrategyInterface|null
  169.      */
  170.     private $labelTranslatorStrategy;
  171.     /**
  172.      * @var FieldDescriptionFactoryInterface|null
  173.      */
  174.     private $fieldDescriptionFactory;
  175.     /**
  176.      * @var MutableTemplateRegistryInterface|null
  177.      */
  178.     private $templateRegistry;
  179.     /**
  180.      * NEXT_MAJOR: Remove the __construct method.
  181.      *
  182.      * @phpstan-param class-string<T>|null $class
  183.      */
  184.     public function __construct(?string $code null, ?string $class null, ?string $baseControllerName null)
  185.     {
  186.         if (\func_num_args() > 0) {
  187.             @trigger_error(
  188.                 'Setting the code, the model class and the base controller name with the constructor is deprecated'
  189.                 .' since sonata-project/admin-bundle version 4.8 and will not be possible in 5.0 version.'
  190.                 .' Use the `code`, `model_class` and `controller` attribute of the `sonata.admin` tag or'
  191.                 .' the method "setCode()", "setModelClass()" and "setBaseControllerName()" instead.',
  192.                 \E_USER_DEPRECATED
  193.             );
  194.         }
  195.         if (null !== $code) {
  196.             $this->code $code;
  197.         }
  198.         $this->class $class;
  199.         $this->modelClass $class;
  200.         if (null !== $baseControllerName) {
  201.             $this->baseControllerName $baseControllerName;
  202.         }
  203.     }
  204.     abstract public function initialize(): void;
  205.     final public function setCode(string $code): void
  206.     {
  207.         $this->code $code;
  208.     }
  209.     final public function getCode(): string
  210.     {
  211.         if (null === $this->code) {
  212.             return static::class;
  213.         }
  214.         return $this->code;
  215.     }
  216.     /**
  217.      * @param class-string<T> $modelClass
  218.      */
  219.     final public function setModelClass(string $modelClass): void
  220.     {
  221.         $this->modelClass $modelClass;
  222.     }
  223.     /**
  224.      * @return class-string<T>
  225.      */
  226.     final public function getModelClass(): string
  227.     {
  228.         if (null === $this->modelClass) {
  229.             throw new \LogicException(sprintf('Admin "%s" has no model class.', static::class));
  230.         }
  231.         return $this->modelClass;
  232.     }
  233.     final public function setBaseControllerName(string $baseControllerName): void
  234.     {
  235.         $this->baseControllerName $baseControllerName;
  236.     }
  237.     final public function getBaseControllerName(): string
  238.     {
  239.         if (null === $this->baseControllerName) {
  240.             throw new \LogicException(sprintf('Admin "%s" has no base controller name.', static::class));
  241.         }
  242.         return $this->baseControllerName;
  243.     }
  244.     final public function setLabel(?string $label): void
  245.     {
  246.         $this->label $label;
  247.     }
  248.     final public function getLabel(): ?string
  249.     {
  250.         return $this->label;
  251.     }
  252.     final public function setListModes(array $listModes): void
  253.     {
  254.         $this->listModes $listModes;
  255.     }
  256.     final public function getListModes(): array
  257.     {
  258.         return $this->listModes;
  259.     }
  260.     final public function setPagerType(string $pagerType): void
  261.     {
  262.         $this->pagerType $pagerType;
  263.     }
  264.     final public function getPagerType(): string
  265.     {
  266.         return $this->pagerType;
  267.     }
  268.     final public function setManagerType(string $managerType): void
  269.     {
  270.         $this->managerType $managerType;
  271.     }
  272.     final public function getManagerType(): string
  273.     {
  274.         if (null === $this->managerType) {
  275.             throw new \LogicException(sprintf('Admin "%s" has no manager type.', static::class));
  276.         }
  277.         return $this->managerType;
  278.     }
  279.     /**
  280.      * @param array<string, string[]> $information
  281.      */
  282.     final public function setSecurityInformation(array $information): void
  283.     {
  284.         $this->securityInformation $information;
  285.     }
  286.     /**
  287.      * @return array<string, string[]>
  288.      */
  289.     final public function getSecurityInformation(): array
  290.     {
  291.         return $this->securityInformation;
  292.     }
  293.     final public function setFilterPersister(?FilterPersisterInterface $filterPersister null): void
  294.     {
  295.         $this->filterPersister $filterPersister;
  296.     }
  297.     final public function getFilterPersister(): FilterPersisterInterface
  298.     {
  299.         if (!$this->hasFilterPersister()) {
  300.             throw new \LogicException(sprintf('Admin "%s" has no filter persister.', static::class));
  301.         }
  302.         \assert(null !== $this->filterPersister);
  303.         return $this->filterPersister;
  304.     }
  305.     final public function hasFilterPersister(): bool
  306.     {
  307.         return null !== $this->filterPersister;
  308.     }
  309.     final public function setModelManager(ModelManagerInterface $modelManager): void
  310.     {
  311.         $this->modelManager $modelManager;
  312.     }
  313.     final public function getModelManager(): ModelManagerInterface
  314.     {
  315.         if (null === $this->modelManager) {
  316.             throw new \LogicException(sprintf('Admin "%s" has no model manager.', static::class));
  317.         }
  318.         return $this->modelManager;
  319.     }
  320.     final public function setDataSource(DataSourceInterface $dataSource): void
  321.     {
  322.         $this->dataSource $dataSource;
  323.     }
  324.     final public function getDataSource(): DataSourceInterface
  325.     {
  326.         if (null === $this->dataSource) {
  327.             throw new \LogicException(sprintf('Admin "%s" has no data source.', static::class));
  328.         }
  329.         return $this->dataSource;
  330.     }
  331.     final public function setFieldDescriptionFactory(FieldDescriptionFactoryInterface $fieldDescriptionFactory): void
  332.     {
  333.         $this->fieldDescriptionFactory $fieldDescriptionFactory;
  334.     }
  335.     public function getFieldDescriptionFactory(): FieldDescriptionFactoryInterface
  336.     {
  337.         if (null === $this->fieldDescriptionFactory) {
  338.             throw new \LogicException(sprintf(
  339.                 'Admin "%s" has no field description factory.',
  340.                 static::class
  341.             ));
  342.         }
  343.         return $this->fieldDescriptionFactory;
  344.     }
  345.     final public function setFormContractor(FormContractorInterface $formContractor): void
  346.     {
  347.         $this->formContractor $formContractor;
  348.     }
  349.     final public function getFormContractor(): FormContractorInterface
  350.     {
  351.         if (null === $this->formContractor) {
  352.             throw new \LogicException(sprintf('Admin "%s" has no form contractor.', static::class));
  353.         }
  354.         return $this->formContractor;
  355.     }
  356.     final public function setShowBuilder(ShowBuilderInterface $showBuilder): void
  357.     {
  358.         $this->showBuilder $showBuilder;
  359.     }
  360.     final public function getShowBuilder(): ShowBuilderInterface
  361.     {
  362.         if (null === $this->showBuilder) {
  363.             throw new \LogicException(sprintf('Admin "%s" has no show builder.', static::class));
  364.         }
  365.         return $this->showBuilder;
  366.     }
  367.     final public function setListBuilder(ListBuilderInterface $listBuilder): void
  368.     {
  369.         $this->listBuilder $listBuilder;
  370.     }
  371.     final public function getListBuilder(): ListBuilderInterface
  372.     {
  373.         if (null === $this->listBuilder) {
  374.             throw new \LogicException(sprintf('Admin "%s" has no list builder.', static::class));
  375.         }
  376.         return $this->listBuilder;
  377.     }
  378.     final public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder): void
  379.     {
  380.         $this->datagridBuilder $datagridBuilder;
  381.     }
  382.     final public function getDatagridBuilder(): DatagridBuilderInterface
  383.     {
  384.         if (null === $this->datagridBuilder) {
  385.             throw new \LogicException(sprintf('Admin "%s" has no datagrid builder.', static::class));
  386.         }
  387.         return $this->datagridBuilder;
  388.     }
  389.     final public function setTranslator(TranslatorInterface $translator): void
  390.     {
  391.         $this->translator $translator;
  392.     }
  393.     final public function getTranslator(): TranslatorInterface
  394.     {
  395.         if (null === $this->translator) {
  396.             throw new \LogicException(sprintf('Admin "%s" has no translator.', static::class));
  397.         }
  398.         return $this->translator;
  399.     }
  400.     final public function setConfigurationPool(Pool $configurationPool): void
  401.     {
  402.         $this->configurationPool $configurationPool;
  403.     }
  404.     final public function getConfigurationPool(): Pool
  405.     {
  406.         if (null === $this->configurationPool) {
  407.             throw new \LogicException(sprintf('Admin "%s" has no pool.', static::class));
  408.         }
  409.         return $this->configurationPool;
  410.     }
  411.     final public function setRouteGenerator(RouteGeneratorInterface $routeGenerator): void
  412.     {
  413.         $this->routeGenerator $routeGenerator;
  414.     }
  415.     final public function getRouteGenerator(): RouteGeneratorInterface
  416.     {
  417.         if (null === $this->routeGenerator) {
  418.             throw new \LogicException(sprintf('Admin "%s" has no route generator.', static::class));
  419.         }
  420.         return $this->routeGenerator;
  421.     }
  422.     final public function setSecurityHandler(SecurityHandlerInterface $securityHandler): void
  423.     {
  424.         $this->securityHandler $securityHandler;
  425.     }
  426.     final public function getSecurityHandler(): SecurityHandlerInterface
  427.     {
  428.         if (null === $this->securityHandler) {
  429.             throw new \LogicException(sprintf('Admin "%s" has no security handler.', static::class));
  430.         }
  431.         return $this->securityHandler;
  432.     }
  433.     final public function setMenuFactory(FactoryInterface $menuFactory): void
  434.     {
  435.         $this->menuFactory $menuFactory;
  436.     }
  437.     final public function getMenuFactory(): FactoryInterface
  438.     {
  439.         if (null === $this->menuFactory) {
  440.             throw new \LogicException(sprintf('Admin "%s" has no security handler.', static::class));
  441.         }
  442.         return $this->menuFactory;
  443.     }
  444.     final public function setRouteBuilder(RouteBuilderInterface $routeBuilder): void
  445.     {
  446.         $this->routeBuilder $routeBuilder;
  447.     }
  448.     final public function getRouteBuilder(): RouteBuilderInterface
  449.     {
  450.         if (null === $this->routeBuilder) {
  451.             throw new \LogicException(sprintf('Admin "%s" has no route builder.', static::class));
  452.         }
  453.         return $this->routeBuilder;
  454.     }
  455.     final public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy): void
  456.     {
  457.         $this->labelTranslatorStrategy $labelTranslatorStrategy;
  458.     }
  459.     final public function getLabelTranslatorStrategy(): LabelTranslatorStrategyInterface
  460.     {
  461.         if (null === $this->labelTranslatorStrategy) {
  462.             throw new \LogicException(sprintf('Admin "%s" has no label translator strategy.', static::class));
  463.         }
  464.         return $this->labelTranslatorStrategy;
  465.     }
  466.     final public function getTemplateRegistry(): MutableTemplateRegistryInterface
  467.     {
  468.         if (false === $this->hasTemplateRegistry()) {
  469.             throw new \LogicException(sprintf('Unable to find the template registry for admin `%s`.', static::class));
  470.         }
  471.         \assert(null !== $this->templateRegistry);
  472.         return $this->templateRegistry;
  473.     }
  474.     final public function hasTemplateRegistry(): bool
  475.     {
  476.         return null !== $this->templateRegistry;
  477.     }
  478.     final public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry): void
  479.     {
  480.         $this->templateRegistry $templateRegistry;
  481.     }
  482.     final public function setTemplates(array $templates): void
  483.     {
  484.         $this->getTemplateRegistry()->setTemplates($templates);
  485.     }
  486.     final public function setTemplate(string $namestring $template): void
  487.     {
  488.         $this->getTemplateRegistry()->setTemplate($name$template);
  489.     }
  490. }