Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Event\ListFieldChoicesEvent;
|
||||
use Mautic\LeadBundle\Exception\ChoicesNotFoundException;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
final class FieldChoicesProvider implements FieldChoicesProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private array $cachedTypeChoices = [];
|
||||
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private array $cachedAliasChoices = [];
|
||||
|
||||
public function __construct(
|
||||
private EventDispatcherInterface $dispatcher,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getChoicesForField(string $fieldType, string $fieldAlias, string $search = ''): array
|
||||
{
|
||||
$aliasChoices = $this->getAllChoicesForListFieldAliases($search);
|
||||
$typeChoices = $this->getAllChoicesForListFieldTypes();
|
||||
|
||||
if (isset($aliasChoices[$fieldAlias])) {
|
||||
return $aliasChoices[$fieldAlias];
|
||||
}
|
||||
|
||||
if (isset($typeChoices[$fieldType])) {
|
||||
return $typeChoices[$fieldType];
|
||||
}
|
||||
|
||||
throw new ChoicesNotFoundException("No choices for field type {$fieldType} nor alias {$fieldAlias} were found");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function getAllChoicesForListFieldTypes(): array
|
||||
{
|
||||
$this->lookForFieldChoices();
|
||||
|
||||
return $this->cachedTypeChoices;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function getAllChoicesForListFieldAliases(string $search = ''): array
|
||||
{
|
||||
$this->lookForFieldChoices($search);
|
||||
|
||||
return $this->cachedAliasChoices;
|
||||
}
|
||||
|
||||
private function lookForFieldChoices(string $search = ''): void
|
||||
{
|
||||
if (empty($this->cachedTypeChoices)) {
|
||||
$event = new ListFieldChoicesEvent();
|
||||
$event->setSearchTerm($search);
|
||||
$this->dispatcher->dispatch($event, LeadEvents::COLLECT_FILTER_CHOICES_FOR_LIST_FIELD_TYPE);
|
||||
|
||||
$this->cachedTypeChoices = $event->getChoicesForAllListFieldTypes();
|
||||
$this->cachedAliasChoices = $event->getChoicesForAllListFieldAliases();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Exception\ChoicesNotFoundException;
|
||||
|
||||
interface FieldChoicesProviderInterface
|
||||
{
|
||||
/**
|
||||
* @return mixed[]
|
||||
*
|
||||
* @throws ChoicesNotFoundException
|
||||
*/
|
||||
public function getChoicesForField(string $fieldType, string $fieldAlias, string $search = ''): array;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Event\LeadListFiltersOperatorsEvent;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
final class FilterOperatorProvider implements FilterOperatorProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private array $cachedOperators = [];
|
||||
|
||||
public function __construct(
|
||||
private EventDispatcherInterface $dispatcher,
|
||||
private TranslatorInterface $translator,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getAllOperators(): array
|
||||
{
|
||||
if (empty($this->cachedOperators)) {
|
||||
$event = new LeadListFiltersOperatorsEvent([], $this->translator);
|
||||
|
||||
$this->dispatcher->dispatch($event, LeadEvents::LIST_FILTERS_OPERATORS_ON_GENERATE);
|
||||
|
||||
$this->cachedOperators = $this->translateOperatorLabels($event->getOperators());
|
||||
}
|
||||
|
||||
return $this->cachedOperators;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $operators
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function translateOperatorLabels(array $operators): array
|
||||
{
|
||||
foreach ($operators as $key => $operatorSettings) {
|
||||
$operators[$key]['label'] = $this->translator->trans($operatorSettings['label']);
|
||||
}
|
||||
|
||||
return $operators;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
interface FilterOperatorProviderInterface
|
||||
{
|
||||
/**
|
||||
* Finds all operators and reutrn them in an array.
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getAllOperators(): array;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Event\FormAdjustmentEvent;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
final class FormAdjustmentsProvider implements FormAdjustmentsProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EventDispatcherInterface $dispatcher,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FormInterface<FormInterface<mixed>> $form
|
||||
* @param mixed[] $fieldDetails
|
||||
*
|
||||
* @return FormInterface<FormInterface<mixed>>
|
||||
*/
|
||||
public function adjustForm(FormInterface $form, string $fieldAlias, string $fieldObject, string $operator, array $fieldDetails): FormInterface
|
||||
{
|
||||
$event = new FormAdjustmentEvent($form, $fieldAlias, $fieldObject, $operator, $fieldDetails);
|
||||
$this->dispatcher->dispatch($event, LeadEvents::ADJUST_FILTER_FORM_TYPE_FOR_FIELD);
|
||||
|
||||
return $event->getForm();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
interface FormAdjustmentsProviderInterface
|
||||
{
|
||||
/**
|
||||
* Allows subscribers to adjust a form so new fields can be added, deleted or modified.
|
||||
*
|
||||
* @param FormInterface<FormInterface<mixed>> $form
|
||||
* @param mixed[] $fieldDetails
|
||||
*
|
||||
* @return FormInterface<FormInterface<mixed>>
|
||||
*/
|
||||
public function adjustForm(FormInterface $form, string $fieldAlias, string $fieldObject, string $operator, array $fieldDetails): FormInterface;
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Entity\OperatorListTrait;
|
||||
use Mautic\LeadBundle\Event\FieldOperatorsEvent;
|
||||
use Mautic\LeadBundle\Event\TypeOperatorsEvent;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
final class TypeOperatorProvider implements TypeOperatorProviderInterface
|
||||
{
|
||||
use OperatorListTrait;
|
||||
|
||||
/**
|
||||
* @var array<string,mixed[]>
|
||||
*/
|
||||
private array $cachedTypeOperators = [];
|
||||
|
||||
/**
|
||||
* @var array<string,mixed[]>
|
||||
*/
|
||||
private array $cachedTypeOperatorsChoices = [];
|
||||
|
||||
public function __construct(
|
||||
private EventDispatcherInterface $dispatcher,
|
||||
private FilterOperatorProviderInterface $filterOperatorProvider,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getOperatorsIncluding(array $operators): array
|
||||
{
|
||||
return $this->getOperatorChoiceList(['include' => $operators]);
|
||||
}
|
||||
|
||||
public function getOperatorsExcluding(array $operators): array
|
||||
{
|
||||
return $this->getOperatorChoiceList(['exclude' => $operators]);
|
||||
}
|
||||
|
||||
public function getOperatorsForFieldType(string $fieldType): array
|
||||
{
|
||||
// If we already processed this
|
||||
if (isset($this->cachedTypeOperatorsChoices[$fieldType])) {
|
||||
return $this->cachedTypeOperatorsChoices[$fieldType];
|
||||
}
|
||||
|
||||
$typeOperators = $this->getAllTypeOperators();
|
||||
|
||||
if (array_key_exists($fieldType, $typeOperators)) {
|
||||
$this->cachedTypeOperatorsChoices[$fieldType] = $this->getOperatorChoiceList($typeOperators[$fieldType]);
|
||||
} else {
|
||||
$this->cachedTypeOperatorsChoices[$fieldType] = $this->getOperatorChoiceList($typeOperators['default']);
|
||||
}
|
||||
|
||||
return $this->cachedTypeOperatorsChoices[$fieldType];
|
||||
}
|
||||
|
||||
public function getAllTypeOperators(): array
|
||||
{
|
||||
if (empty($this->cachedTypeOperators)) {
|
||||
$event = new TypeOperatorsEvent();
|
||||
|
||||
$this->dispatcher->dispatch($event, LeadEvents::COLLECT_OPERATORS_FOR_FIELD_TYPE);
|
||||
|
||||
$this->cachedTypeOperators = $event->getOperatorsForAllFieldTypes();
|
||||
}
|
||||
|
||||
return $this->cachedTypeOperators;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will add the default operators for the $type like the getOperatorsForFieldType() method
|
||||
* but also allows plugins to add more operators.
|
||||
*
|
||||
* @return array<string,string>
|
||||
*/
|
||||
public function getOperatorsForField(string $type, string $field): array
|
||||
{
|
||||
$event = new FieldOperatorsEvent(
|
||||
$type,
|
||||
$field,
|
||||
$this->filterOperatorProvider->getAllOperators(),
|
||||
$this->getOperatorsForFieldType($type)
|
||||
);
|
||||
|
||||
$this->dispatcher->dispatch($event, LeadEvents::COLLECT_OPERATORS_FOR_FIELD);
|
||||
|
||||
return $event->getOperators();
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwriting deprecated method from OperatorListTrait.
|
||||
*
|
||||
* @param string $operator
|
||||
*
|
||||
* @return array<string,mixed[]>
|
||||
*/
|
||||
public function getFilterExpressionFunctions($operator = null)
|
||||
{
|
||||
$operatorOptions = $this->filterOperatorProvider->getAllOperators();
|
||||
|
||||
return (null === $operator) ? $operatorOptions : $operatorOptions[$operator];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Exception\OperatorsNotFoundException;
|
||||
|
||||
interface TypeOperatorProviderInterface
|
||||
{
|
||||
/**
|
||||
* @param mixed[] $operators
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getOperatorsIncluding(array $operators): array;
|
||||
|
||||
/**
|
||||
* @param mixed[] $operators
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getOperatorsExcluding(array $operators): array;
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*
|
||||
* @throws OperatorsNotFoundException
|
||||
*/
|
||||
public function getOperatorsForFieldType(string $fieldType): array;
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getAllTypeOperators(): array;
|
||||
}
|
||||
Reference in New Issue
Block a user