Skip to content

Commit

Permalink
Merge pull request #3 from vdlp/feature/add-multisite-and-images-support
Browse files Browse the repository at this point in the history
Add multisite and images support and other fixes
  • Loading branch information
adrenth authored Nov 15, 2022
2 parents 503a125 + 4cc794d commit 1cc3804
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 57 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.2.0] - 2022-11-15

* Add support for images.
* Add sitemap config resolver to configure the sitemap config on runtime. This can be useful for multisite projects.
* Add support for oc 1.
* Add support for priority zero.
* Fixed bug where sitemap would never regenerate when sitemap file exists.
* Escape illegal xml characters in loc and title elements.
* Log exception with stack trace and show 500 error when an error occurs.

## [2.0.0] - 2021-07-13

* Add support for PHP 7.4 or higher. Please review plugin configuration, check README.md
* Add support for PHP 7.4 or higher. Please review plugin configuration, check README.md

## [1.1.0] - 2021-05-28

Expand Down
62 changes: 47 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

## Requirements

- PHP 8.0.2 or higher
- October CMS 2.x or 3.x
- PHP 8.0 or higher
- October CMS 1.1 or higher

## Usage

Expand All @@ -29,7 +29,7 @@ To generate sitemap items you can create your own sitemap definition generator.

Example:

```
```php
final class DefinitionGenerator implements Contracts\DefinitionGenerator
{
public function getDefinitions(): Definitions
Expand All @@ -52,18 +52,18 @@ final class DefinitionGenerator implements Contracts\DefinitionGenerator

Register your generator in the `boot` method of your plugin class:

```
```php
Event::listen(Contracts\SitemapGenerator::GENERATE_EVENT, static function(): DefinitionGenerator {
return new DefinitionGenerator();
});
```

You can also register multiple generators:

```
```php
Event::listen(Contracts\SitemapGenerator::GENERATE_EVENT, static function(): array {
return [
new DefinitionGeneratorOne(),
new DefinitionGeneratorOne(),
new DefinitionGeneratorTwo(),
// ..
];
Expand All @@ -74,13 +74,13 @@ Event::listen(Contracts\SitemapGenerator::GENERATE_EVENT, static function(): arr

You can fire an event to invalidate the sitemap cache

```
```php
Event::fire(Contracts\SitemapGenerator::INVALIDATE_CACHE_EVENT);
```

Or resolve the generator instance and use the invalidate cache method

```
```php
/** @var SitemapGenerator $sitemapGenerator */
$sitemapGenerator = resolve(Contracts\SitemapGenerator::class);
$sitemapGenerator->invalidateCache();
Expand All @@ -90,14 +90,14 @@ $sitemapGenerator->invalidateCache();

First resolve the sitemap generator

```
```php
/** @var SitemapGenerator $sitemapGenerator */
$sitemapGenerator = resolve(Contracts\SitemapGenerator::class);
```

### Add definitions

```
```php
$sitemapGenerator->addDefinition(
(new Definition())
->setUrl('example.com/new-url')
Expand All @@ -111,7 +111,7 @@ $sitemapGenerator->addDefinition(

> Note, definitions are updated by their URL.
```
```php
$sitemapGenerator->updateDefinition(
(new Definition())
->setUrl('example.com/page/1')
Expand All @@ -124,7 +124,7 @@ $sitemapGenerator->updateDefinition(

### Update or add definitions

```
```php
$sitemapGenerator->updateOrAddDefinition(
(new Definition())
->setUrl('example.com/create-or-add')
Expand All @@ -137,13 +137,13 @@ $sitemapGenerator->updateOrAddDefinition(

### Delete definitions

```
```php
$sitemapGenerator->deleteDefinition('example.com/new-url');
```

## Exclude URLs from sitemap

```
```php
Event::listen(SitemapGenerator::EXCLUDE_URLS_EVENT, static function (): array {
return [
'example.com/page/1',
Expand All @@ -162,11 +162,43 @@ php artisan vendor:publish --provider="Vdlp\Sitemap\ServiceProvider" --tag="conf
You can change the amount of seconds the sitemap is cached in your `.env` file.
You can also cache the sitemap forever.

```
```dotenv
VDLP_SITEMAP_CACHE_TIME = 3600
VDLP_SITEMAP_CACHE_FOREVER = false
```

### ConfigResolver

Optionally you can override how the sitemap config should be resolved by giving your own ConfigResolver implementation in the config file.
This can be useful for multisite projects, where the sitemap should be cached per domain.

```php
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\Request;
use Vdlp\Sitemap\Classes\Contracts\ConfigResolver;
use Vdlp\Sitemap\Classes\Dto\SitemapConfig;

final class MultisiteConfigResolver implements ConfigResolver
{
public function __construct(private Repository $config, private Request $request)
{
}

public function getConfig(): SitemapConfig
{
$domain = $this->request->getHost();

return new SitemapConfig(
'vdlp_sitemap_cache_' . $domain,
'vdlp_sitemap_definitions_' . $domain,
sprintf('vdlp/sitemap/sitemap_%s.xml', $domain),
(int) $this->config->get('sitemap.cache_time', 3600),
(bool) $this->config->get('sitemap.cache_forever', false)
);
}
}
```

## Issues

If you have issues using this plugin. Please create an issue on GitHub or contact us at [[email protected]]().
Expand Down
11 changes: 11 additions & 0 deletions ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Vdlp\Sitemap;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider as ServiceProviderBase;
use Vdlp\Sitemap\Classes\Contracts;
use Vdlp\Sitemap\Classes\SitemapGenerator;
Expand All @@ -19,6 +21,15 @@ public function boot(): void

public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/config.php', 'sitemap');

$this->app->alias(SitemapGenerator::class, Contracts\SitemapGenerator::class);

$this->app->bind(Contracts\ConfigResolver::class, static function (Application $app): Contracts\ConfigResolver {
/** @var Repository $config */
$config = $app->make(Repository::class);

return $app->make($config->get('sitemap.config_resolver'));
});
}
}
Loading

0 comments on commit 1cc3804

Please sign in to comment.