2018-06-30 11:01:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
2019-11-28 08:16:50 +08:00
|
|
|
* For detailed copyright and license information, please view the
|
|
|
|
* LICENSE file that was distributed with this source code.
|
2018-06-30 11:01:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Flarum\Forum;
|
|
|
|
|
|
|
|
use Flarum\Foundation\ValidationException;
|
2018-11-16 11:24:13 +08:00
|
|
|
use Flarum\Frontend\Assets;
|
2018-06-30 11:01:12 +08:00
|
|
|
use Flarum\Locale\LocaleManager;
|
|
|
|
use Flarum\Settings\Event\Saved;
|
|
|
|
use Flarum\Settings\Event\Saving;
|
|
|
|
use Flarum\Settings\OverrideSettingsRepository;
|
|
|
|
use Flarum\Settings\SettingsRepositoryInterface;
|
|
|
|
use Illuminate\Contracts\Container\Container;
|
|
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
|
|
use League\Flysystem\Adapter\NullAdapter;
|
|
|
|
use League\Flysystem\Filesystem;
|
|
|
|
use Less_Exception_Parser;
|
|
|
|
|
2018-11-16 11:24:13 +08:00
|
|
|
class ValidateCustomLess
|
2018-06-30 11:01:12 +08:00
|
|
|
{
|
2018-11-16 11:24:13 +08:00
|
|
|
/**
|
|
|
|
* @var Assets
|
|
|
|
*/
|
|
|
|
protected $assets;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LocaleManager
|
|
|
|
*/
|
|
|
|
protected $locales;
|
|
|
|
|
2018-06-30 11:01:12 +08:00
|
|
|
/**
|
|
|
|
* @var Container
|
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
2018-11-16 11:24:13 +08:00
|
|
|
* @param Assets $assets
|
2018-06-30 11:01:12 +08:00
|
|
|
* @param LocaleManager $locales
|
|
|
|
* @param Container $container
|
|
|
|
*/
|
2018-11-16 11:24:13 +08:00
|
|
|
public function __construct(Assets $assets, LocaleManager $locales, Container $container)
|
2018-06-30 11:01:12 +08:00
|
|
|
{
|
2018-11-16 11:24:13 +08:00
|
|
|
$this->assets = $assets;
|
|
|
|
$this->locales = $locales;
|
2018-06-30 11:01:12 +08:00
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function whenSettingsSaving(Saving $event)
|
|
|
|
{
|
2018-12-14 04:58:00 +08:00
|
|
|
if (! isset($event->settings['custom_less'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We haven't saved the settings yet, but we want to trial a full
|
|
|
|
// recompile of the CSS to see if this custom LESS will break
|
|
|
|
// anything. In order to do that, we will temporarily override the
|
|
|
|
// settings repository with the new settings so that the recompile
|
|
|
|
// is effective. We will also use a dummy filesystem so that nothing
|
|
|
|
// is actually written yet.
|
|
|
|
|
|
|
|
$settings = $this->container->make(SettingsRepositoryInterface::class);
|
|
|
|
|
|
|
|
$this->container->extend(
|
|
|
|
SettingsRepositoryInterface::class,
|
|
|
|
function ($settings) use ($event) {
|
|
|
|
return new OverrideSettingsRepository($settings, $event->settings);
|
2018-06-30 11:01:12 +08:00
|
|
|
}
|
2018-12-14 04:58:00 +08:00
|
|
|
);
|
2018-06-30 11:01:12 +08:00
|
|
|
|
2018-12-14 04:58:00 +08:00
|
|
|
$assetsDir = $this->assets->getAssetsDir();
|
|
|
|
$this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem(new NullAdapter)));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->assets->makeCss()->commit();
|
|
|
|
|
|
|
|
foreach ($this->locales->getLocales() as $locale => $name) {
|
|
|
|
$this->assets->makeLocaleCss($locale)->commit();
|
|
|
|
}
|
|
|
|
} catch (Less_Exception_Parser $e) {
|
|
|
|
throw new ValidationException(['custom_less' => $e->getMessage()]);
|
2018-06-30 11:01:12 +08:00
|
|
|
}
|
2018-12-14 04:58:00 +08:00
|
|
|
|
|
|
|
$this->assets->setAssetsDir($assetsDir);
|
|
|
|
$this->container->instance(SettingsRepositoryInterface::class, $settings);
|
2018-06-30 11:01:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function whenSettingsSaved(Saved $event)
|
|
|
|
{
|
2018-12-14 04:58:00 +08:00
|
|
|
if (! isset($event->settings['custom_less'])) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-16 11:24:13 +08:00
|
|
|
|
2018-12-14 04:58:00 +08:00
|
|
|
$this->assets->makeCss()->flush();
|
|
|
|
|
|
|
|
foreach ($this->locales->getLocales() as $locale => $name) {
|
|
|
|
$this->assets->makeLocaleCss($locale)->flush();
|
2018-06-30 11:01:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|