-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
RemoveResource
extender. (#34)
* Added `RemoveResource` extender. * Added docs for removing a resource to README
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/sitemap. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace FoF\Sitemap\Extend; | ||
|
||
use Flarum\Extend\ExtenderInterface; | ||
use Flarum\Extension\Extension; | ||
use FoF\Sitemap\Resources\Resource; | ||
use Illuminate\Contracts\Container\Container; | ||
use InvalidArgumentException; | ||
|
||
class RemoveResource implements ExtenderInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $resource; | ||
|
||
public function __construct(string $resource) | ||
{ | ||
$this->resource = $resource; | ||
} | ||
|
||
public function extend(Container $container, Extension $extension = null) | ||
{ | ||
$container->extend('fof.sitemap.resources', function (array $resources) use ($container) { | ||
$resource = $container->make($this->resource); | ||
|
||
if ($resource instanceof Resource) { | ||
$resources = array_filter($resources, function ($res) { | ||
return get_class($res) !== $this->resource; | ||
}); | ||
} else { | ||
throw new InvalidArgumentException("{$this->resource} has to extend ".Resource::class); | ||
} | ||
|
||
return $resources; | ||
}); | ||
} | ||
} |