vendor/shopware/core/System/CustomField/CustomFieldService.php line 87

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\Field\BoolField;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Field\Field;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\AllowHtml;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Field\FloatField;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Field\PriceField;
  14. use Shopware\Core\Framework\Log\Package;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Contracts\Service\ResetInterface;
  17. /**
  18. * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  19. */
  20. #[Package('core')]
  21. class CustomFieldService implements EventSubscriberInterface, ResetInterface
  22. {
  23. /**
  24. * @var array<string>|null
  25. */
  26. private ?array $customFields = null;
  27. private Connection $connection;
  28. /**
  29. * @internal
  30. */
  31. public function __construct(Connection $connection)
  32. {
  33. $this->connection = $connection;
  34. }
  35. public function getCustomField(string $attributeName): ?Field
  36. {
  37. $type = $this->getCustomFields()[$attributeName] ?? null;
  38. if (!$type) {
  39. return null;
  40. }
  41. switch ($type) {
  42. case CustomFieldTypes::INT:
  43. return (new IntField($attributeName, $attributeName))->addFlags(new ApiAware());
  44. case CustomFieldTypes::FLOAT:
  45. return (new FloatField($attributeName, $attributeName))->addFlags(new ApiAware());
  46. case CustomFieldTypes::BOOL:
  47. return (new BoolField($attributeName, $attributeName))->addFlags(new ApiAware());
  48. case CustomFieldTypes::DATETIME:
  49. return (new DateTimeField($attributeName, $attributeName))->addFlags(new ApiAware());
  50. case CustomFieldTypes::TEXT:
  51. return (new LongTextField($attributeName, $attributeName))->addFlags(new ApiAware());
  52. case CustomFieldTypes::HTML:
  53. return (new LongTextField($attributeName, $attributeName))->addFlags(new ApiAware(), new AllowHtml());
  54. case CustomFieldTypes::PRICE:
  55. return (new PriceField($attributeName, $attributeName))->addFlags(new ApiAware());
  56. case CustomFieldTypes::JSON:
  57. default:
  58. return (new JsonField($attributeName, $attributeName))->addFlags(new ApiAware());
  59. }
  60. }
  61. /**
  62. * @return array<string, string>
  63. */
  64. public static function getSubscribedEvents(): array
  65. {
  66. return [
  67. CustomFieldEvents::CUSTOM_FIELD_DELETED_EVENT => 'reset',
  68. CustomFieldEvents::CUSTOM_FIELD_WRITTEN_EVENT => 'reset',
  69. ];
  70. }
  71. public function reset(): void
  72. {
  73. $this->customFields = null;
  74. }
  75. /**
  76. * @return array<string>
  77. */
  78. private function getCustomFields(): array
  79. {
  80. if ($this->customFields !== null) {
  81. return $this->customFields;
  82. }
  83. $this->customFields = $this->connection->fetchAllKeyValue('SELECT `name`, `type` FROM `custom_field` WHERE `active` = 1');
  84. return $this->customFields;
  85. }
  86. }