Skip to content

Commit

Permalink
Added RemoveResource extender. (#34)
Browse files Browse the repository at this point in the history
* Added `RemoveResource` extender.

* Added docs for removing a resource to README
  • Loading branch information
iPurpl3x authored Sep 30, 2021
1 parent 6f2262e commit abf7e00
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ _Best for larger forums, starting at 50.000 items._

## Extending

### Register a new Resource

In order to register your own resource, create a class that implements `FoF\Sitemap\Resources\Resource`. Make sure
to implement all abstract methods, check other implementations for examples. After this, register your

Expand All @@ -63,6 +65,15 @@ return [
```
That's it.

### Remove a Resource

In a very similar way, you can also remove resources from the sitemap:
```php
return [
new \FoF\Sitemap\Extend\RemoveResource(User::class)
];
```

## Scheduling

If the size of your forum requires one of the cache modes - either in-memory or disk, consider setting up the Flarum scheduler. Read more information about this [here](https://discuss.flarum.org/d/24118)
Expand Down
49 changes: 49 additions & 0 deletions src/Extend/RemoveResource.php
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;
});
}
}

0 comments on commit abf7e00

Please sign in to comment.