ManagerConfigurator.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\Query\Filter\SQLFilter;
  5. /**
  6. * Configurator for an EntityManager
  7. */
  8. class ManagerConfigurator
  9. {
  10. /** @var string[] */
  11. private $enabledFilters = [];
  12. /** @var array<string,array<string,string>> */
  13. private $filtersParameters = [];
  14. /**
  15. * @param string[] $enabledFilters
  16. * @param array<string,array<string,string>> $filtersParameters
  17. */
  18. public function __construct(array $enabledFilters, array $filtersParameters)
  19. {
  20. $this->enabledFilters = $enabledFilters;
  21. $this->filtersParameters = $filtersParameters;
  22. }
  23. /**
  24. * Create a connection by name.
  25. */
  26. public function configure(EntityManagerInterface $entityManager)
  27. {
  28. $this->enableFilters($entityManager);
  29. }
  30. /**
  31. * Enables filters for a given entity manager
  32. */
  33. private function enableFilters(EntityManagerInterface $entityManager): void
  34. {
  35. if (empty($this->enabledFilters)) {
  36. return;
  37. }
  38. $filterCollection = $entityManager->getFilters();
  39. foreach ($this->enabledFilters as $filter) {
  40. $filterObject = $filterCollection->enable($filter);
  41. if ($filterObject === null) {
  42. continue;
  43. }
  44. $this->setFilterParameters($filter, $filterObject);
  45. }
  46. }
  47. /**
  48. * Sets default parameters for a given filter
  49. */
  50. private function setFilterParameters(string $name, SQLFilter $filter): void
  51. {
  52. if (empty($this->filtersParameters[$name])) {
  53. return;
  54. }
  55. $parameters = $this->filtersParameters[$name];
  56. foreach ($parameters as $paramName => $paramValue) {
  57. $filter->setParameter($paramName, $paramValue);
  58. }
  59. }
  60. }