mirror of
https://github.com/flarum/framework.git
synced 2025-03-09 19:55:26 +08:00

* Extender docblocks cleanup * Excplicit type hinting in extenders * Bring method under constructor * Mark some classes and methods as internal * Remove beta references Co-authored-by: Clark Winkelmann <clark.winkelmann@gmail.com>
55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
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\Extend;
|
|
|
|
use Flarum\Extension\Extension;
|
|
use Illuminate\Contracts\Container\Container;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class ModelUrl implements ExtenderInterface
|
|
{
|
|
private $modelClass;
|
|
private $slugDrivers = [];
|
|
|
|
/**
|
|
* @param string $modelClass: The ::class attribute of the model you are modifying.
|
|
* This model should extend from \Flarum\Database\AbstractModel.
|
|
*/
|
|
public function __construct(string $modelClass)
|
|
{
|
|
$this->modelClass = $modelClass;
|
|
}
|
|
|
|
/**
|
|
* Add a slug driver.
|
|
*
|
|
* @param string $identifier: Identifier for slug driver.
|
|
* @param string $driver: ::class attribute of driver class, which must implement Flarum\Http\SlugDriverInterface.
|
|
* @return self
|
|
*/
|
|
public function addSlugDriver(string $identifier, string $driver): self
|
|
{
|
|
$this->slugDrivers[$identifier] = $driver;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function extend(Container $container, Extension $extension = null)
|
|
{
|
|
if ($this->slugDrivers) {
|
|
$container->extend('flarum.http.slugDrivers', function ($existingDrivers) {
|
|
$existingDrivers[$this->modelClass] = array_merge(Arr::get($existingDrivers, $this->modelClass, []), $this->slugDrivers);
|
|
|
|
return $existingDrivers;
|
|
});
|
|
}
|
|
}
|
|
}
|