platform/src/Core/System/CustomField/CustomFieldService.php line 78

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\CustomField;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\FetchModeHelper;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\AllowHtml;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\FloatField;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CustomFieldService implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var string[]|null
  19.      */
  20.     private $customFields;
  21.     /**
  22.      * @var Connection
  23.      */
  24.     private $connection;
  25.     public function __construct(Connection $connection)
  26.     {
  27.         $this->connection $connection;
  28.     }
  29.     public function getCustomField(string $attributeName): ?Field
  30.     {
  31.         $type $this->getCustomFields()[$attributeName] ?? null;
  32.         if (!$type) {
  33.             return null;
  34.         }
  35.         switch ($type) {
  36.             case CustomFieldTypes::INT:
  37.                 return (new IntField($attributeName$attributeName))->addFlags(new ApiAware());
  38.             case CustomFieldTypes::FLOAT:
  39.                 return (new FloatField($attributeName$attributeName))->addFlags(new ApiAware());
  40.             case CustomFieldTypes::BOOL:
  41.                 return (new BoolField($attributeName$attributeName))->addFlags(new ApiAware());
  42.             case CustomFieldTypes::DATETIME:
  43.                 return (new DateTimeField($attributeName$attributeName))->addFlags(new ApiAware());
  44.             case CustomFieldTypes::TEXT:
  45.                 return (new LongTextField($attributeName$attributeName))->addFlags(new ApiAware());
  46.             case CustomFieldTypes::HTML:
  47.                 return (new LongTextField($attributeName$attributeName))->addFlags(new ApiAware(), new AllowHtml());
  48.             case CustomFieldTypes::JSON:
  49.             default:
  50.                 return (new JsonField($attributeName$attributeName))->addFlags(new ApiAware());
  51.         }
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             CustomFieldEvents::CUSTOM_FIELD_DELETED_EVENT => 'invalidateCache',
  57.             CustomFieldEvents::CUSTOM_FIELD_WRITTEN_EVENT => 'invalidateCache',
  58.         ];
  59.     }
  60.     /**
  61.      * @internal
  62.      */
  63.     public function invalidateCache(): void
  64.     {
  65.         $this->customFields null;
  66.     }
  67.     /**
  68.      * @return string[]
  69.      */
  70.     private function getCustomFields(): array
  71.     {
  72.         if ($this->customFields !== null) {
  73.             return $this->customFields;
  74.         }
  75.         $fields $this->connection->fetchAll('SELECT `name`, `type` FROM `custom_field` WHERE `active` = 1');
  76.         $this->customFields FetchModeHelper::keyPair($fields);
  77.         return $this->customFields;
  78.     }
  79. }