mirror of
https://github.com/flarum/core.git
synced 2025-07-18 23:31:17 +02:00
- 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
188 lines
4.5 KiB
PHP
Executable File
188 lines
4.5 KiB
PHP
Executable File
<?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\Database;
|
|
|
|
use Flarum\Event\ConfigureModelDates;
|
|
use Flarum\Event\GetModelRelationship;
|
|
use Illuminate\Database\Eloquent\Model as Eloquent;
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
use LogicException;
|
|
|
|
/**
|
|
* Base model class, building on Eloquent.
|
|
*
|
|
* Adds the ability for custom relations to be added to a model during runtime.
|
|
* These relations behave in the same way that you would expect; they can be
|
|
* queried, eager loaded, and accessed as an attribute.
|
|
*/
|
|
abstract class AbstractModel extends Eloquent
|
|
{
|
|
/**
|
|
* Indicates if the model should be timestamped. Turn off by default.
|
|
*
|
|
* @var boolean
|
|
*/
|
|
public $timestamps = false;
|
|
|
|
/**
|
|
* An array of callbacks to be run once after the model is saved.
|
|
*
|
|
* @var callable[]
|
|
*/
|
|
protected $afterSaveCallbacks = [];
|
|
|
|
/**
|
|
* An array of callbacks to be run once after the model is deleted.
|
|
*
|
|
* @var callable[]
|
|
*/
|
|
protected $afterDeleteCallbacks = [];
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::saved(function (AbstractModel $model) {
|
|
foreach ($model->releaseAfterSaveCallbacks() as $callback) {
|
|
$callback($model);
|
|
}
|
|
});
|
|
|
|
static::deleted(function (AbstractModel $model) {
|
|
foreach ($model->releaseAfterDeleteCallbacks() as $callback) {
|
|
$callback($model);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the attributes that should be converted to dates.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getDates()
|
|
{
|
|
static $dates = [];
|
|
|
|
$class = get_class($this);
|
|
|
|
if (! isset($dates[$class])) {
|
|
static::$dispatcher->fire(
|
|
new ConfigureModelDates($this, $this->dates)
|
|
);
|
|
|
|
$dates[$class] = $this->dates;
|
|
}
|
|
|
|
return $dates[$class];
|
|
}
|
|
|
|
/**
|
|
* Get an attribute from the model. If nothing is found, attempt to load
|
|
* a custom relation method with this key.
|
|
*
|
|
* @param string $key
|
|
* @return mixed
|
|
*/
|
|
public function getAttribute($key)
|
|
{
|
|
if (! is_null($value = parent::getAttribute($key))) {
|
|
return $value;
|
|
}
|
|
|
|
// If a custom relation with this key has been set up, then we will load
|
|
// and return results from the query and hydrate the relationship's
|
|
// value on the "relationships" array.
|
|
if (! $this->relationLoaded($key) && ($relation = $this->getCustomRelation($key))) {
|
|
if (! $relation instanceof Relation) {
|
|
throw new LogicException('Relationship method must return an object of type '
|
|
. 'Illuminate\Database\Eloquent\Relations\Relation');
|
|
}
|
|
|
|
return $this->relations[$key] = $relation->getResults();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a custom relation object.
|
|
*
|
|
* @param string $name
|
|
* @return mixed
|
|
*/
|
|
protected function getCustomRelation($name)
|
|
{
|
|
return static::$dispatcher->until(
|
|
new GetModelRelationship($this, $name)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Register a callback to be run once after the model is saved.
|
|
*
|
|
* @param callable $callback
|
|
* @return void
|
|
*/
|
|
public function afterSave($callback)
|
|
{
|
|
$this->afterSaveCallbacks[] = $callback;
|
|
}
|
|
|
|
/**
|
|
* Register a callback to be run once after the model is deleted.
|
|
*
|
|
* @param callable $callback
|
|
* @return void
|
|
*/
|
|
public function afterDelete($callback)
|
|
{
|
|
$this->afterDeleteCallbacks[] = $callback;
|
|
}
|
|
|
|
/**
|
|
* @return callable[]
|
|
*/
|
|
public function releaseAfterSaveCallbacks()
|
|
{
|
|
$callbacks = $this->afterSaveCallbacks;
|
|
|
|
$this->afterSaveCallbacks = [];
|
|
|
|
return $callbacks;
|
|
}
|
|
|
|
/**
|
|
* @return callable[]
|
|
*/
|
|
public function releaseAfterDeleteCallbacks()
|
|
{
|
|
$callbacks = $this->afterDeleteCallbacks;
|
|
|
|
$this->afterDeleteCallbacks = [];
|
|
|
|
return $callbacks;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __call($method, $arguments)
|
|
{
|
|
if ($relation = $this->getCustomRelation($method)) {
|
|
return $relation;
|
|
}
|
|
|
|
return parent::__call($method, $arguments);
|
|
}
|
|
}
|