1
0
mirror of https://github.com/flarum/core.git synced 2025-08-16 13:24:11 +02:00

Major refactor and improvements

- Reorganised all namespaces and class names for consistency and structure. Following PSR bylaws (Abstract prefix, Interface/Trait suffix).
  - Move models into root of Core, because writing `use Flarum\Core\Discussion` is nice. Namespace the rest by type. (Namespacing by entity was too arbitrary.)
  - Moved some non-domain stuff out of Core: Database, Formatter, Settings.
  - Renamed config table and all references to "settings" for consistency.
  - Remove Core class and add url()/isInstalled()/inDebugMode() as instance methods of Foundation\Application.
  - Cleanup, docblocking, etc.

- Improvements to HTTP architecture
  - API and forum/admin Actions are now actually all the same thing (simple PSR-7 Request handlers), renamed to Controllers.
  - Upgrade to tobscure/json-api 0.2 branch.
  - Where possible, moved generic functionality to tobscure/json-api (e.g. pagination links). I'm quite happy with the backend balance now re: #262

- Improvements to other architecture
  - Use Illuminate's Auth\Access\Gate interface/implementation instead of our old Locked trait. We still use events to actually determine the permissions though. Our Policy classes are actually glorified event subscribers.
  - Extract model validation into Core\Validator classes.
  - Make post visibility permission stuff much more efficient and DRY.

- Renamed Flarum\Event classes for consistency. ref #246
  - `Configure` prefix for events dedicated to configuring an object.
  - `Get` prefix for events whose listeners should return something.
  - `Prepare` prefix when a variable is passed by reference so it can be modified.
  - `Scope` prefix when a query builder is passed.

- Miscellaneous improvements/bug-fixes. I'm easily distracted!
  - Increase default height of post composer.
  - Improve post stream redraw flickering in Safari by keying loading post placeholders with their IDs. ref #451
  - Use a PHP JavaScript minification library for minifying TextFormatter's JavaScript, instead of ClosureCompilerService (can't rely on external service!)
  - Use UrlGenerator properly in various places. closes #123
  - Make Api\Client return Response object. closes #128
  - Allow extensions to specify custom icon images.
  - Allow external API/admin URLs to be optionally specified in config.php. If the value or "url" is an array, we look for the corresponding path inside. Otherwise, we append the path to the base URL, using the corresponding value in "paths" if present. closes #244
This commit is contained in:
Toby Zerner
2015-10-08 14:28:02 +10:30
parent 8c7cdb184f
commit dd67291ce0
434 changed files with 8676 additions and 7997 deletions

210
src/Formatter/Formatter.php Normal file
View File

@@ -0,0 +1,210 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Formatter;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Unparser;
use Flarum\Event\ConfigureFormatter;
use Flarum\Event\ConfigureFormatterParser;
use Flarum\Event\ConfigureFormatterRenderer;
class Formatter
{
/**
* @var Repository
*/
protected $cache;
/**
* @var Dispatcher
*/
protected $events;
/**
* @var string
*/
protected $cacheDir;
/**
* @param Repository $cache
* @param Dispatcher $events
* @param string $cacheDir
*/
public function __construct(Repository $cache, Dispatcher $events, $cacheDir)
{
$this->cache = $cache;
$this->events = $events;
$this->cacheDir = $cacheDir;
}
/**
* Parse text.
*
* @param string $text
* @param mixed $context
* @return string
*/
public function parse($text, $context = null)
{
$parser = $this->getParser($context);
return $parser->parse($text);
}
/**
* Render parsed XML.
*
* @param string $xml
* @param mixed $context
* @return string
*/
public function render($xml, $context = null)
{
$renderer = $this->getRenderer($context);
return $renderer->render($xml);
}
/**
* Unparse XML.
*
* @param string $xml
* @return string
*/
public function unparse($xml)
{
return Unparser::unparse($xml);
}
/**
* Flush the cache so that the formatter components are regenerated.
*/
public function flush()
{
$this->cache->forget('flarum.formatter.parser');
$this->cache->forget('flarum.formatter.renderer');
}
/**
* @return Configurator
*/
protected function getConfigurator()
{
$configurator = new Configurator;
$configurator->rootRules->enableAutoLineBreaks();
$configurator->rendering->engine = 'PHP';
$configurator->rendering->engine->cacheDir = $this->cacheDir;
$configurator->Escaper;
$configurator->Autoemail;
$configurator->Autolink;
$configurator->tags->onDuplicate('replace');
$this->events->fire(new ConfigureFormatter($configurator));
$this->configureExternalLinks($configurator);
return $configurator;
}
/**
* @param Configurator $configurator
*/
protected function configureExternalLinks(Configurator $configurator)
{
$dom = $configurator->tags['URL']->template->asDOM();
foreach ($dom->getElementsByTagName('a') as $a) {
$a->setAttribute('target', '_blank');
$a->setAttribute('rel', 'nofollow');
}
$dom->saveChanges();
}
/**
* Get a TextFormatter component.
*
* @param string $name "renderer" or "parser"
* @return mixed
*/
protected function getComponent($name)
{
$cacheKey = 'flarum.formatter.' . $name;
return $this->cache->rememberForever($cacheKey, function () use ($name) {
return $this->getConfigurator()->finalize()[$name];
});
}
/**
* Get the parser.
*
* @param mixed $context
* @return \s9e\TextFormatter\Parser
*/
protected function getParser($context = null)
{
$parser = $this->getComponent('parser');
$parser->registeredVars['context'] = $context;
$this->events->fire(new ConfigureFormatterParser($parser, $context));
return $parser;
}
/**
* Get the renderer.
*
* @param mixed $context
* @return \s9e\TextFormatter\Renderer
*/
protected function getRenderer($context = null)
{
spl_autoload_register(function ($class) {
if (file_exists($file = $this->cacheDir.'/'.$class.'.php')) {
include $file;
}
});
$renderer = $this->getComponent('renderer');
$this->events->fire(new ConfigureFormatterRenderer($renderer, $context));
return $renderer;
}
/**
* Get the formatter JavaScript.
*
* @return string
*/
public function getJs()
{
$configurator = $this->getConfigurator();
$configurator->enableJavaScript();
$configurator->javascript->exportMethods = ['preview'];
$minifier = $configurator->javascript->setMinifier(new MinifyMinifier);
$minifier->keepGoing = true;
$minifier->cacheDir = $this->cacheDir;
return $configurator->finalize([
'returnParser' => false,
'returnRenderer' => false
])['js'];
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Formatter;
use Flarum\Event\ExtensionWasDisabled;
use Flarum\Event\ExtensionWasEnabled;
use Flarum\Foundation\AbstractServiceProvider;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
class FormatterServiceProvider extends AbstractServiceProvider
{
/**
* {@inheritdoc}
*/
public function boot(Dispatcher $events)
{
$events->listen(ExtensionWasEnabled::class, [$this, 'flushFormatter']);
$events->listen(ExtensionWasDisabled::class, [$this, 'flushFormatter']);
}
/**
* {@inheritdoc}
*/
public function register()
{
$this->app->singleton('flarum.formatter', function (Container $container) {
return new Formatter(
$container->make('cache.store'),
$container->make('events'),
$this->app->storagePath().'/formatter'
);
});
$this->app->alias('flarum.formatter', 'Flarum\Formatter\Formatter');
}
public function flushFormatter()
{
$this->app->make('flarum.formatter')->flush();
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Formatter;
use s9e\TextFormatter\Configurator\JavaScript\Minifier;
use MatthiasMullie\Minify;
class MinifyMinifier extends Minifier
{
/**
* {@inheritdoc}
*/
public function getCacheDifferentiator()
{
return null;
}
/**
* {@inheritdoc}
*/
public function minify($src)
{
$minifier = new Minify\JS($src);
return $minifier->minify();
}
}