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

Added RemoveResource extender. #34

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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;
});
}
}