vendor/easycorp/easyadmin-bundle/src/Dto/AssetsDto.php line 8

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
  3. /**
  4. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  5. */
  6. final class AssetsDto
  7. {
  8. private $webpackEncoreAssets = [];
  9. private $cssAssets = [];
  10. private $jsAssets = [];
  11. private $headContents = [];
  12. private $bodyContents = [];
  13. public function __construct()
  14. {
  15. }
  16. public function addWebpackEncoreAsset(AssetDto $assetDto): void
  17. {
  18. if (\array_key_exists($entryName = $assetDto->getValue(), $this->webpackEncoreAssets)) {
  19. throw new \InvalidArgumentException(sprintf('The "%s" Webpack Encore entry has been added more than once via the addWebpackEncoreEntry() method, but each entry can only be added once (to not overwrite its configuration).', $entryName));
  20. }
  21. $this->webpackEncoreAssets[$entryName] = $assetDto;
  22. }
  23. public function addCssAsset(AssetDto $assetDto): void
  24. {
  25. if (\array_key_exists($cssPath = $assetDto->getValue(), $this->cssAssets)) {
  26. throw new \InvalidArgumentException(sprintf('The "%s" CSS file has been added more than once via the addCssFile() method, but each asset can only be added once (to not overwrite its configuration).', $cssPath));
  27. }
  28. $this->cssAssets[$cssPath] = $assetDto;
  29. }
  30. public function addJsAsset(AssetDto $assetDto): void
  31. {
  32. if (\array_key_exists($jsPath = $assetDto->getValue(), $this->jsAssets)) {
  33. throw new \InvalidArgumentException(sprintf('The "%s" JS file has been added more than once via the addJsFile() method, but each asset can only be added once (to not overwrite its configuration).', $jsPath));
  34. }
  35. $this->jsAssets[$jsPath] = $assetDto;
  36. }
  37. public function addHtmlContentToHead(string $htmlContent): void
  38. {
  39. if (\in_array($htmlContent, $this->headContents, true)) {
  40. return;
  41. }
  42. $this->headContents[] = $htmlContent;
  43. }
  44. public function addHtmlContentToBody(string $htmlContent): void
  45. {
  46. if (\in_array($htmlContent, $this->bodyContents, true)) {
  47. return;
  48. }
  49. $this->bodyContents[] = $htmlContent;
  50. }
  51. /**
  52. * @return AssetDto[]
  53. */
  54. public function getWebpackEncoreAssets(): array
  55. {
  56. return $this->webpackEncoreAssets;
  57. }
  58. /**
  59. * @return AssetDto[]
  60. */
  61. public function getCssAssets(): array
  62. {
  63. return $this->cssAssets;
  64. }
  65. /**
  66. * @return AssetDto[]
  67. */
  68. public function getJsAssets(): array
  69. {
  70. return $this->jsAssets;
  71. }
  72. public function getHeadContents(): array
  73. {
  74. return $this->headContents;
  75. }
  76. public function getBodyContents(): array
  77. {
  78. return $this->bodyContents;
  79. }
  80. public function mergeWith(self $assetsDto): self
  81. {
  82. $this->webpackEncoreAssets = array_merge($this->webpackEncoreAssets, $assetsDto->getWebpackEncoreAssets());
  83. $this->cssAssets = array_merge($this->cssAssets, $assetsDto->getCssAssets());
  84. $this->jsAssets = array_merge($this->jsAssets, $assetsDto->getJsAssets());
  85. $this->headContents = array_merge($this->headContents, $assetsDto->getHeadContents());
  86. $this->bodyContents = array_merge($this->bodyContents, $assetsDto->getBodyContents());
  87. return $this;
  88. }
  89. }