mirror of
https://github.com/flarum/core.git
synced 2025-10-14 00:15:51 +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:
151
src/Core/Validator/AbstractValidator.php
Normal file
151
src/Core/Validator/AbstractValidator.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?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\Validator;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Event\ConfigureModelValidator;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Contracts\Validation\ValidationException;
|
||||
use Illuminate\Validation\Factory;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
class AbstractValidator
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $rules = [];
|
||||
|
||||
/**
|
||||
* @var Factory
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @param Factory $validator
|
||||
*/
|
||||
public function __construct(Factory $validator, Dispatcher $events)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->events = $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a model is valid.
|
||||
*
|
||||
* @param AbstractModel $model
|
||||
* @return bool
|
||||
*/
|
||||
public function valid(AbstractModel $model)
|
||||
{
|
||||
return $this->makeValidator($model)->passes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an exception if a model is not valid.
|
||||
*
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function assertValid(AbstractModel $model)
|
||||
{
|
||||
$validator = $this->makeValidator($model);
|
||||
|
||||
if ($validator->fails()) {
|
||||
$this->throwValidationException($validator);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Validator $validator
|
||||
* @throws ValidationException
|
||||
*/
|
||||
protected function throwValidationException(Validator $validator)
|
||||
{
|
||||
throw new ValidationException($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new validator instance for this model.
|
||||
*
|
||||
* @param AbstractModel $model
|
||||
* @return \Illuminate\Validation\Validator
|
||||
*/
|
||||
protected function makeValidator(AbstractModel $model)
|
||||
{
|
||||
$rules = $this->expandUniqueRules($this->rules, $model);
|
||||
|
||||
$validator = $this->validator->make($model->getAttributes(), $rules);
|
||||
|
||||
$this->events->fire(
|
||||
new ConfigureModelValidator($model, $validator)
|
||||
);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand 'unique' rules in a set of validation rules into a fuller form
|
||||
* that Laravel's validator can understand.
|
||||
*
|
||||
* @param array $rules
|
||||
* @param AbstractModel $model
|
||||
* @return array
|
||||
*/
|
||||
protected function expandUniqueRules($rules, AbstractModel $model)
|
||||
{
|
||||
foreach ($rules as $attribute => &$ruleset) {
|
||||
if (is_string($ruleset)) {
|
||||
$ruleset = explode('|', $ruleset);
|
||||
}
|
||||
|
||||
foreach ($ruleset as &$rule) {
|
||||
if (strpos($rule, 'unique') === 0) {
|
||||
$rule = $this->expandUniqueRule($attribute, $rule, $model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand a 'unique' rule into a fuller form that Laravel's validator can
|
||||
* understand, based on this model's properties.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string $rule
|
||||
* @param AbstractModel $model
|
||||
* @return string
|
||||
*/
|
||||
protected function expandUniqueRule($attribute, $rule, AbstractModel $model)
|
||||
{
|
||||
$parts = explode(':', $rule);
|
||||
$key = $model->getKey() ?: 'NULL';
|
||||
$rule = 'unique:'.$model->getTable().','.$attribute.','.$key.','.$model->getKeyName();
|
||||
|
||||
if (! empty($parts[1])) {
|
||||
$wheres = explode(',', $parts[1]);
|
||||
|
||||
foreach ($wheres as &$where) {
|
||||
$where .= ','.$this->$where;
|
||||
}
|
||||
|
||||
$rule .= ','.implode(',', $wheres);
|
||||
}
|
||||
|
||||
return $rule;
|
||||
}
|
||||
}
|
27
src/Core/Validator/DiscussionValidator.php
Normal file
27
src/Core/Validator/DiscussionValidator.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?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\Validator;
|
||||
|
||||
class DiscussionValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'title' => ['required', 'max:80'],
|
||||
'start_time' => ['required', 'date'],
|
||||
'comments_count' => ['integer'],
|
||||
'participants_count' => ['integer'],
|
||||
'start_user_id' => ['integer'],
|
||||
'start_post_id' => ['integer'],
|
||||
'last_time' => ['date'],
|
||||
'last_user_id' => ['integer'],
|
||||
'last_post_id' => ['integer'],
|
||||
'last_post_number' => ['integer'],
|
||||
];
|
||||
}
|
19
src/Core/Validator/GroupValidator.php
Normal file
19
src/Core/Validator/GroupValidator.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?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\Validator;
|
||||
|
||||
class GroupValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'name_singular' => ['required'],
|
||||
'name_plural' => ['required']
|
||||
];
|
||||
}
|
26
src/Core/Validator/PostValidator.php
Normal file
26
src/Core/Validator/PostValidator.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?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\Validator;
|
||||
|
||||
class PostValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'discussion_id' => ['required', 'integer'],
|
||||
'time' => ['required', 'date'],
|
||||
'content' => ['required', 'max:65535'],
|
||||
'number' => ['integer'],
|
||||
'user_id' => ['integer'],
|
||||
'edit_time' => ['date'],
|
||||
'edit_user_id' => ['integer'],
|
||||
'hide_time' => ['date'],
|
||||
'hide_user_id' => ['integer']
|
||||
];
|
||||
}
|
24
src/Core/Validator/UserValidator.php
Normal file
24
src/Core/Validator/UserValidator.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?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\Validator;
|
||||
|
||||
class UserValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'username' => ['required', 'alpha_dash', 'unique', 'min:3', 'max:30'],
|
||||
'email' => ['required', 'email', 'unique'],
|
||||
'password' => ['required'],
|
||||
'join_time' => ['date'],
|
||||
'last_seen_time' => ['date'],
|
||||
'discussions_count' => ['integer'],
|
||||
'posts_count' => ['integer']
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user