Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slug Driver Support #2456

Merged
merged 22 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Slug Extender Test works
  • Loading branch information
tankerkiller125 committed Dec 1, 2020
commit 45db62e97aa97592b0d0b50e0d7894615d70c144
14 changes: 8 additions & 6 deletions src/Http/HttpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public function register()
$compiledDrivers = [];

foreach ($this->app->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) {
$driverClass = $resourceDrivers[$settings->get("slug_driver_$resourceClass", 'default')];
echo 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
echo "slug_driver_$resourceClass";
//echo $settings->get("slug_driver_$resourceClass", 'default');
$driverKey = $settings->get("slug_driver_$resourceClass", 'default');
if (array_key_exists($driverKey, $resourceDrivers)) {
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
$driverClass = $resourceDrivers[$driverKey];
} else {
$driverClass = $resourceDrivers['default'];
}
$compiledDrivers[$resourceClass] = $this->app->make($driverClass);
}

Expand All @@ -70,14 +72,14 @@ public function register()
]);
},
Post::class => function (UrlGenerator $urlGenerator, Post $post) use ($slugManager) {
return $urlGenerator->to('forum')->route('user', [
return $urlGenerator->to('forum')->route('post', [
'id' => $slugManager->forResource(Discussion::class)->toSlug($post->discussion),
'near' => $post->id,
]);
},
User::class => function (UrlGenerator $urlGenerator, User $user) use ($slugManager) {
return $urlGenerator->to('forum')->route('user', [
'id' => $slugManager->forResource(User::class)->toSlug($user)
'username' => $slugManager->forResource(User::class)->toSlug($user)
]);
},
];
Expand Down
4 changes: 1 addition & 3 deletions src/Http/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public function toResource(string $resourceClass, AbstractModel $instance, ...$a
{
$callback = $this->resourceUrlGenerators[$resourceClass];

$result = $callback($this, $instance, ...$args);

return $result;
return $callback($this, $instance, ...$args);
}
}
42 changes: 4 additions & 38 deletions tests/integration/extenders/ModelUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Http\SlugDriverInterface;
use Flarum\Http\SlugManager;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\User;
Expand All @@ -24,35 +25,18 @@ class ModelUrlTest extends TestCase

protected function prepDb()
{
$userClass = User::class;
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser(),
],
]);
}

protected function activateCustomDriver()
{
$userClass = User::class;
$this->prepareDatabase([
'settings' => [
['key' => "slug_driver_$userClass", 'value' => 'testDriver'],
],
]
]);
}

// public function tearDown()
// {
// $userClass = User::class;
// $this->prepareDatabase([
// 'settings' => [
// ['key' => "slug_driver_$userClass", 'value' => 'default'],
// ],
// ]);
// parent::tearDown();
// }

/**
* @test
*/
Expand Down Expand Up @@ -102,29 +86,11 @@ public function uses_default_driver_by_default()
/**
* @test
*/
public function custom_slug_driver_doesnt_have_effect_unless_enabled()
{
$this->extend((new Extend\ModelUrl(User::class))->addSlugDriver('testDriver', TestSlugDriver::class));

$this->prepDb();

$slugManager = $this->app()->getContainer()->make(SlugManager::class);

$testUser = User::find(1);

$this->assertEquals('admin', $slugManager->forResource(User::class)->toSlug($testUser));
$this->assertEquals('1', $slugManager->forResource(User::class)->fromSlug('admin', $testUser)->id);
}

/**
* @test
*/
public function custom_slug_driver_has_effect_if_enabled()
public function custom_slug_driver_has_effect_if_added()
{
$this->extend((new Extend\ModelUrl(User::class))->addSlugDriver('testDriver', TestSlugDriver::class));

$this->prepDb();
$this->activateCustomDriver();

$slugManager = $this->app()->getContainer()->make(SlugManager::class);

Expand Down