Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: split frontend asset generation into separate steps for more extensibility #3446

Merged
merged 2 commits into from
Jun 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions framework/core/src/Frontend/Content/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@

class Assets
{
/**
* @var Container
*/
protected $container;

/**
* @var Config
*/
protected $config;

/**
Expand All @@ -32,26 +39,56 @@ public function __construct(Container $container, Config $config)
$this->config = $config;
}

public function forFrontend(string $name)
/**
* Sets the frontend to generate assets for.
*
* @param string $name frontend name
* @return $this
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function forFrontend(string $name): Assets
{
$this->assets = $this->container->make('flarum.assets.'.$name);

return $this;
}

public function __invoke(Document $document, Request $request)
public function __invoke(Document $document, Request $request): void
{
$locale = $request->getAttribute('locale');

$compilers = [
'js' => [$this->assets->makeJs(), $this->assets->makeLocaleJs($locale)],
'css' => [$this->assets->makeCss(), $this->assets->makeLocaleCss($locale)]
];
$compilers = $this->assembleCompilers($locale);

if ($this->config->inDebugMode()) {
$this->forceCommit(Arr::flatten($compilers));
}

$this->addAssetsToDocument($document, $compilers);
}

/**
* Assembles JS and CSS compilers to be used to generate frontend assets.
*
* @param string|null $locale
* @return array[]
*/
protected function assembleCompilers(?string $locale): array
{
return [
'js' => [$this->assets->makeJs(), $this->assets->makeLocaleJs($locale)],
'css' => [$this->assets->makeCss(), $this->assets->makeLocaleCss($locale)]
];
}

/**
* Adds URLs of frontend JS and CSS to the {@link Document} class.
*
* @param Document $document
* @param array $compilers
* @return void
*/
protected function addAssetsToDocument(Document $document, array $compilers): void
{
$document->js = array_merge($document->js, $this->getUrls($compilers['js']));
$document->css = array_merge($document->css, $this->getUrls($compilers['css']));
}
Expand All @@ -61,7 +98,7 @@ public function __invoke(Document $document, Request $request)
*
* @param array $compilers
*/
private function forceCommit(array $compilers)
protected function forceCommit(array $compilers): void
{
/** @var CompilerInterface $compiler */
foreach ($compilers as $compiler) {
Expand All @@ -70,10 +107,12 @@ private function forceCommit(array $compilers)
}

/**
* Maps provided {@link CompilerInterface}s to their URLs.
*
* @param CompilerInterface[] $compilers
* @return string[]
*/
private function getUrls(array $compilers)
protected function getUrls(array $compilers): array
{
return array_filter(array_map(function (CompilerInterface $compiler) {
return $compiler->getUrl();
Expand Down