<?php
namespace Boldr\Cms\CmsBundle\Renderer;
use JsonSerializable;
class Assets implements JsonSerializable
{
/** @var string[] */
private $headContents = [];
/** @var bool[] */
private $headContentsRendered = [];
/** @var string[] */
private $bodyContents = [];
/** @var bool[] */
private $bodyContentsRendered = [];
/** @var string[] */
private $webpackEncoreEntries = [];
/** @var bool[] */
private $webpackEncoreScriptEntriesRendered = [];
/** @var bool[] */
private $webpackEncoreStyleEntriesRendered = [];
/** @var string[] */
private $cssFiles = [];
/** @var bool[] */
private $cssFilesRendered = [];
/** @var string[] */
private $jsFiles = [];
/** @var bool[] */
private $jsFilesRendered = [];
public function jsonSerialize()
{
return [
'headContents' => $this->headContents,
'bodyContents' => $this->bodyContents,
'webpackEncoreEntries' => $this->webpackEncoreEntries,
'cssFiles' => $this->cssFiles,
'jsFiles' => $this->jsFiles,
];
}
public function addWebpackEncoreEntry(string $entryName): self
{
if (!in_array($entryName, $this->webpackEncoreEntries))
{
$this->webpackEncoreEntries[] = $entryName;
}
return $this;
}
public function addCssFile(string $path): self
{
if (!in_array($path, $this->cssFiles))
{
$this->cssFiles[] = $path;
}
return $this;
}
public function addJsFile(string $path): self
{
if (!in_array($path, $this->jsFiles))
{
$this->jsFiles[] = $path;
}
return $this;
}
public function addHtmlContentToHead(string $htmlContent): self
{
if (!in_array($htmlContent, $this->headContents))
{
$this->headContents[] = $htmlContent;
}
return $this;
}
public function addHtmlContentToBody(string $htmlContent): self
{
if (!in_array($htmlContent, $this->bodyContents))
{
$this->bodyContents[] = $htmlContent;
}
return $this;
}
public function getUnrenderedWebpackEncoreStyleEntries(): iterable
{
foreach ($this->webpackEncoreEntries as $i => $v)
{
if (empty($this->webpackEncoreStyleEntriesRendered[$i]))
{
yield $v;
}
}
}
public function getUnrenderedWebpackEncoreScriptEntries(): iterable
{
foreach ($this->webpackEncoreEntries as $i => $v)
{
if (empty($this->webpackEncoreScriptEntriesRendered[$i]))
{
yield $v;
}
}
}
public function getUnrenderedCssFiles(): iterable
{
foreach ($this->cssFiles as $i => $v)
{
if (empty($this->cssFilesRendered[$i]))
{
yield $v;
}
}
}
public function getUnrenderedJsFiles(): iterable
{
foreach ($this->jsFiles as $i => $v)
{
if (empty($this->jsFilesRendered[$i]))
{
yield $v;
}
}
}
public function getUnrenderedHeadContents(): iterable
{
foreach ($this->headContents as $i => $v)
{
if (empty($this->headContentsRendered[$i]))
{
yield $v;
}
}
}
public function getUnrenderedBodyContents(): iterable
{
foreach ($this->bodyContents as $i => $v)
{
if (empty($this->bodyContentsRendered[$i]))
{
yield $v;
}
}
}
public function renderWebpackEncoreScriptEntries(): void
{
foreach($this->webpackEncoreEntries as $i => $_)
{
$this->webpackEncoreScriptEntriesRendered[$i] = true;
}
}
public function renderWebpackEncoreStyleEntries(): void
{
foreach($this->webpackEncoreEntries as $i => $_)
{
$this->webpackEncoreStyleEntriesRendered[$i] = true;
}
}
public function renderCssFiles(): void
{
foreach($this->cssFiles as $i => $_)
{
$this->cssFilesRendered[$i] = true;
}
}
public function renderJsFiles(): void
{
foreach($this->jsFiles as $i => $_)
{
$this->jsFilesRendered[$i] = true;
}
}
public function renderHeadContents(): void
{
foreach($this->headContents as $i => $_)
{
$this->headContentsRendered[$i] = true;
}
}
public function renderBodyContents(): void
{
foreach($this->bodyContents as $i => $_)
{
$this->bodyContentsRendered[$i] = true;
}
}
public function addAll(Assets ...$assets): self
{
foreach ($assets as $a)
{
foreach ($a->getUnrenderedJsFiles() as $jsFile)
{
$this->addJsFile($jsFile);
}
foreach ($a->getUnrenderedCssFiles() as $cssFile)
{
$this->addCssFile($cssFile);
}
foreach ($a->getUnrenderedWebpackEncoreStyleEntries() as $entry)
{
$this->addWebpackEncoreEntry($entry);
}
foreach ($a->getUnrenderedWebpackEncoreScriptEntries() as $entry)
{
$this->addWebpackEncoreEntry($entry);
}
foreach ($a->getUnrenderedHeadContents() as $htmlContent)
{
$this->addHtmlContentToHead($htmlContent);
}
foreach ($a->getUnrenderedBodyContents() as $htmlContent)
{
$this->addHtmlContentToBody($htmlContent);
}
}
return $this;
}
}