1
0
mirror of https://github.com/flarum/core.git synced 2025-10-15 08:55:53 +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

184
src/Core/Post/CommentPost.php Executable file
View File

@@ -0,0 +1,184 @@
<?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\Core\Post;
use DomainException;
use Flarum\Core\Post;
use Flarum\Formatter\Formatter;
use Flarum\Event\PostWasPosted;
use Flarum\Event\PostWasRevised;
use Flarum\Event\PostWasHidden;
use Flarum\Event\PostWasRestored;
use Flarum\Core\User;
/**
* A standard comment in a discussion.
*
* @property string $parsed_content
* @property string $content_html
*/
class CommentPost extends Post
{
/**
* {@inheritdoc}
*/
public static $type = 'comment';
/**
* The text formatter instance.
*
* @var \Flarum\Formatter\Formatter
*/
protected static $formatter;
/**
* Create a new instance in reply to a discussion.
*
* @param int $discussionId
* @param string $content
* @param int $userId
* @return static
*/
public static function reply($discussionId, $content, $userId)
{
$post = new static;
$post->time = time();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
$post->type = static::$type;
// Set content last, as the parsing may rely on other post attributes.
$post->content = $content;
$post->raise(new PostWasPosted($post));
return $post;
}
/**
* Revise the post's content.
*
* @param string $content
* @param User $actor
* @return $this
*/
public function revise($content, User $actor)
{
if ($this->content !== $content) {
$this->content = $content;
$this->edit_time = time();
$this->edit_user_id = $actor->id;
$this->raise(new PostWasRevised($this));
}
return $this;
}
/**
* Hide the post.
*
* @param User $actor
* @return $this
*/
public function hide(User $actor = null)
{
if (! $this->hide_time) {
$this->hide_time = time();
$this->hide_user_id = $actor ? $actor->id : null;
$this->raise(new PostWasHidden($this));
}
return $this;
}
/**
* Restore the post.
*
* @return $this
*/
public function restore()
{
if ($this->hide_time !== null) {
$this->hide_time = null;
$this->hide_user_id = null;
$this->raise(new PostWasRestored($this));
}
return $this;
}
/**
* Unparse the parsed content.
*
* @param string $value
* @return string
*/
public function getContentAttribute($value)
{
return static::$formatter->unparse($value);
}
/**
* Get the parsed/raw content.
*
* @return string
*/
public function getParsedContentAttribute()
{
return $this->attributes['content'];
}
/**
* Parse the content before it is saved to the database.
*
* @param string $value
*/
public function setContentAttribute($value)
{
$this->attributes['content'] = $value ? static::$formatter->parse($value, $this) : null;
}
/**
* Get the content rendered as HTML.
*
* @param string $value
* @return string
*/
public function getContentHtmlAttribute($value)
{
return static::$formatter->render($this->attributes['content'], $this);
}
/**
* Get the text formatter instance.
*
* @return \Flarum\Formatter\Formatter
*/
public static function getFormatter()
{
return static::$formatter;
}
/**
* Set the text formatter instance.
*
* @param \Flarum\Formatter\Formatter $formatter
*/
public static function setFormatter(Formatter $formatter)
{
static::$formatter = $formatter;
}
}