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

Apply fixes from StyleCI

[ci skip] [skip ci]
This commit is contained in:
luceos
2020-11-17 03:46:35 +00:00
committed by Matthew Kilgore
parent 4574fa6290
commit a155e41432
10 changed files with 44 additions and 26 deletions

View File

@@ -1,16 +1,20 @@
<?php <?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Discussion; namespace Flarum\Discussion;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\Http\SlugDriverInterface; use Flarum\Http\SlugDriverInterface;
use Flarum\User\User; use Flarum\User\User;
class IdWithSlugDriver implements SlugDriverInterface class IdWithSlugDriver implements SlugDriverInterface
{ {
public function toSlug(AbstractModel $instance): string public function toSlug(AbstractModel $instance): string
{ {
return $instance->id.(trim($instance->slug) ? '-'.$instance->slug : ''); return $instance->id.(trim($instance->slug) ? '-'.$instance->slug : '');
@@ -22,6 +26,7 @@ class IdWithSlugDriver implements SlugDriverInterface
$slug_array = explode('-', $slug); $slug_array = explode('-', $slug);
$slug = $slug_array[0]; $slug = $slug_array[0];
} }
return Discussion::where('id', $slug)->whereVisibleTo($actor)->firstOrFail(); return Discussion::where('id', $slug)->whereVisibleTo($actor)->firstOrFail();
} }
} }

View File

@@ -10,7 +10,6 @@
namespace Flarum\Forum\Content; namespace Flarum\Forum\Content;
use Flarum\Api\Client; use Flarum\Api\Client;
use Flarum\Discussion\Discussion as FlarumDiscussion;
use Flarum\Frontend\Document; use Flarum\Frontend\Document;
use Flarum\Http\Exception\RouteNotFoundException; use Flarum\Http\Exception\RouteNotFoundException;
use Flarum\Http\UrlGenerator; use Flarum\Http\UrlGenerator;

View File

@@ -33,7 +33,7 @@ class HttpServiceProvider extends AbstractServiceProvider
return new Middleware\CheckCsrfToken($app->make('flarum.http.csrfExemptPaths')); return new Middleware\CheckCsrfToken($app->make('flarum.http.csrfExemptPaths'));
}); });
$this->app->singleton('flarum.http.slugDrivers', function() { $this->app->singleton('flarum.http.slugDrivers', function () {
return [ return [
Discussion::class => [ Discussion::class => [
'default' => IdWithSlugDriver::class 'default' => IdWithSlugDriver::class
@@ -44,12 +44,12 @@ class HttpServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->singleton('flarum.http.selectedSlugDrivers', function() { $this->app->singleton('flarum.http.selectedSlugDrivers', function () {
$settings = $this->app->make(SettingsRepositoryInterface::class); $settings = $this->app->make(SettingsRepositoryInterface::class);
$compiledDrivers = []; $compiledDrivers = [];
foreach($this->app->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) { foreach ($this->app->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) {
$driverClass = $resourceDrivers[$settings->get("slug_driver_$resourceClass", 'default')]; $driverClass = $resourceDrivers[$settings->get("slug_driver_$resourceClass", 'default')];
$compiledDrivers[$resourceClass] = $this->app->make($driverClass); $compiledDrivers[$resourceClass] = $this->app->make($driverClass);
} }
@@ -57,36 +57,37 @@ class HttpServiceProvider extends AbstractServiceProvider
return $compiledDrivers; return $compiledDrivers;
}); });
$this->app->singleton('flarum.http.resourceUrlGenerators', function() { $this->app->singleton('flarum.http.resourceUrlGenerators', function () {
$slugManager = $this->app->make(SlugManager::class); $slugManager = $this->app->make(SlugManager::class);
return [ return [
Discussion::class => function(UrlGenerator $urlGenerator, Discussion $discussion) use ($slugManager) { Discussion::class => function (UrlGenerator $urlGenerator, Discussion $discussion) use ($slugManager) {
return $urlGenerator->to('forum')->route('discussion', [ return $urlGenerator->to('forum')->route('discussion', [
'id' => $slugManager->toResource(Discussion::class)->toSlug($discussion) 'id' => $slugManager->toResource(Discussion::class)->toSlug($discussion)
]); ]);
}, },
Post::class => function(UrlGenerator $urlGenerator, Post $post) use ($slugManager) { Post::class => function (UrlGenerator $urlGenerator, Post $post) use ($slugManager) {
return $urlGenerator->to('forum')->route('user', [ return $urlGenerator->to('forum')->route('user', [
'id' => $slugManager->toResource(Discussion::class)->toSlug($post->discussion), 'id' => $slugManager->toResource(Discussion::class)->toSlug($post->discussion),
'near' => $post->id, 'near' => $post->id,
]); ]);
}, },
User::class => function(UrlGenerator $urlGenerator, User $user) use ($slugManager) { User::class => function (UrlGenerator $urlGenerator, User $user) use ($slugManager) {
return $urlGenerator->to('forum')->route('user', [ return $urlGenerator->to('forum')->route('user', [
'id' => $slugManager->toResource(User::class)->toSlug($user) 'id' => $slugManager->toResource(User::class)->toSlug($user)
]); ]);
}, },
]; ];
}); });
$this->app->bind(SlugManager::class, function() { $this->app->bind(SlugManager::class, function () {
return new SlugManager($this->app->make('flarum.http.selectedSlugDrivers')); return new SlugManager($this->app->make('flarum.http.selectedSlugDrivers'));
}); });
$this->app->bind(UrlGenerator::class, function() { $this->app->bind(UrlGenerator::class, function () {
return new UrlGenerator( return new UrlGenerator(
$this->app->make(Application::class), $this->app->make(Application::class),
$this->app->make('flarum.http.resourceUrlGenerators')); $this->app->make('flarum.http.resourceUrlGenerators')
);
}); });
} }
} }

View File

@@ -1,9 +1,14 @@
<?php <?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Http; namespace Flarum\Http;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\User\User; use Flarum\User\User;

View File

@@ -9,7 +9,6 @@
namespace Flarum\Http; namespace Flarum\Http;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
class SlugManager class SlugManager

View File

@@ -68,7 +68,7 @@ class UrlGenerator
} }
/** /**
* Generate a URL to an instance of a resource * Generate a URL to an instance of a resource.
* *
* @param string $resourceClass * @param string $resourceClass
* @param AbstractModel $instance * @param AbstractModel $instance

View File

@@ -1,15 +1,19 @@
<?php <?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\User; namespace Flarum\User;
use Flarum\Database\AbstractModel; use Flarum\Database\AbstractModel;
use Flarum\Http\SlugDriverInterface; use Flarum\Http\SlugDriverInterface;
class UsernameSlugDriver implements SlugDriverInterface class UsernameSlugDriver implements SlugDriverInterface
{ {
public function toSlug(AbstractModel $instance): string public function toSlug(AbstractModel $instance): string
{ {
return $instance->username; return $instance->username;

View File

@@ -77,7 +77,7 @@ class ShowTest extends TestCase
$this->request('GET', '/api/discussions/1-fdsafdsajfsakf', [ $this->request('GET', '/api/discussions/1-fdsafdsajfsakf', [
'authenticatedAs' => 2, 'authenticatedAs' => 2,
])->withQueryParams([ ])->withQueryParams([
"bySlug" => true 'bySlug' => true
]) ])
); );

View File

@@ -57,7 +57,7 @@ class CreateTest extends TestCase
$this->assertEquals(422, $response->getStatusCode()); $this->assertEquals(422, $response->getStatusCode());
// The response body should contain details about the failed validation // The response body should contain details about the failed validation
$body = (string)$response->getBody(); $body = (string) $response->getBody();
$this->assertJson($body); $this->assertJson($body);
$this->assertEquals([ $this->assertEquals([
'errors' => [ 'errors' => [

View File

@@ -1,9 +1,14 @@
<?php <?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\api\users; namespace Flarum\Tests\integration\api\users;
use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase; use Flarum\Tests\integration\TestCase;
@@ -55,7 +60,7 @@ class ShowTest extends TestCase
$this->request('GET', '/api/users/normal', [ $this->request('GET', '/api/users/normal', [
'authenticatedAs' => 1, 'authenticatedAs' => 1,
])->withQueryParams([ ])->withQueryParams([
"bySlug" => true 'bySlug' => true
]) ])
); );
@@ -81,7 +86,7 @@ class ShowTest extends TestCase
{ {
$response = $this->send( $response = $this->send(
$this->request('GET', '/api/users/2')->withQueryParams([ $this->request('GET', '/api/users/2')->withQueryParams([
"bySlug" => true 'bySlug' => true
]) ])
); );
@@ -111,7 +116,7 @@ class ShowTest extends TestCase
$this->request('GET', '/api/users/normal', [ $this->request('GET', '/api/users/normal', [
'authenticatedAs' => 2, 'authenticatedAs' => 2,
])->withQueryParams([ ])->withQueryParams([
"bySlug" => true 'bySlug' => true
]) ])
); );
@@ -141,7 +146,7 @@ class ShowTest extends TestCase
$this->request('GET', '/api/users/admin', [ $this->request('GET', '/api/users/admin', [
'authenticatedAs' => 2, 'authenticatedAs' => 2,
])->withQueryParams([ ])->withQueryParams([
"bySlug" => true 'bySlug' => true
]) ])
); );
@@ -173,7 +178,7 @@ class ShowTest extends TestCase
$this->request('GET', '/api/users/admin', [ $this->request('GET', '/api/users/admin', [
'authenticatedAs' => 2, 'authenticatedAs' => 2,
])->withQueryParams([ ])->withQueryParams([
"bySlug" => true 'bySlug' => true
]) ])
); );