Skip to content

Commit

Permalink
CMF Implementation (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg authored Nov 19, 2022
1 parent bec6531 commit f83aea4
Show file tree
Hide file tree
Showing 22 changed files with 814 additions and 718 deletions.
79 changes: 79 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

$header = <<<EOF
This file is part of the Contao Simple News URLs extension.
(c) inspiredminds
@license LGPL-3.0-or-later
EOF;

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__.'/contao',
__DIR__.'/src',
])
;

return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHPUnit60Migration:risky' => true,
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'comment_to_phpdoc' => true,
'compact_nullable_typehint' => true,
'escape_implicit_backslashes' => true,
'fully_qualified_strict_types' => true,
'general_phpdoc_annotation_remove' => [
'annotations' => [
'author',
'expectedException',
'expectedExceptionMessage',
],
],
'header_comment' => ['header' => $header],
'heredoc_to_nowdoc' => true,
'linebreak_after_opening_tag' => true,
'list_syntax' => ['syntax' => 'short'],
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'new_line_for_chained_calls',
],
'native_function_invocation' => [
'include' => ['@compiler_optimized'],
],
'no_alternative_syntax' => true,
'no_binary_string' => true,
'no_null_property_initialization' => true,
'no_superfluous_elseif' => true,
'no_superfluous_phpdoc_tags' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_order' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types_order' => [
'null_adjustment' => 'always_last',
'sort_algorithm' => 'none',
],
'return_assignment' => true,
'strict_comparison' => true,
'strict_param' => true,
'string_line_ending' => true,
'void_return' => true,
])
->setFinder($finder)
->setRiskyAllowed(true)
->setUsingCache(false)
;
495 changes: 160 additions & 335 deletions LICENSE

Large diffs are not rendered by default.

26 changes: 6 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,20 @@
[![](https://img.shields.io/packagist/dt/fritzmg/contao-simple-news-urls.svg)](https://packagist.org/packages/fritzmg/contao-simple-news-urls)

Contao Simple News URLs
===================
=======================

Simple Contao extension to allow news URLs without its reader page fragment. Instead of having an URL like
This Contao extension allows news URLs without its reader page fragment. Instead of having an URL like

```
example.org/reader-page/news-entry.html
example.org/reader-page/news-entry
```

all news entries will instead be reachable via their alias directly, e.g.

```
example.org/news-entry.html
example.org/news-entry
```

This is done through Contao's `getPageIdFromUrl` hook. All links to news entries generated by Contao will also be rewritten to its short URL via Contao's `generateFrontendUrl` hook. Furthermore, this extension also implements a `parseArticles` hook which will generate a 301 redirect to the short URL, if the newsreader was accessed via the news' long URL. This avoids having duplicate content. You can change this behavior with the following setting in your `localconfig.php`:
when enabled in the respective news archive. There will also be a 301 redirect from the old URL to the new one.

```php
$GLOBALS['TL_CONFIG']['simpleNewsUrlsRedirect'] = …;
```

Valid values are `301`, `302`, `303` or `'canonical'`. The latter will not create a redirect, but insert a canonical tag into the `<head>` instead.

The extension also works with different URL suffixes, or none at all.

### Requirements

The only requirement is, that you enable the `auto_item` parameter (it is enabled by default) and do not disable the usage of aliases (they are not disabled by default) in the settings.

### Note

The usage of the `generateFrontendUrl` hook might have a negative impact on the website's performance, since there will be an additional database query for every URL that Contao generates with additional parameters.
_Note:_ the extension enforces these URLs, i.e. the URL prefix and suffix settings of the website root will be disregarded.
33 changes: 21 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords":["contao", "news", "urls"],
"type":"contao-module",
"homepage":"https://github.com/fritzmg/contao-simple-news-urls",
"license":"GPL-2.0-only",
"license":"LGPL-3.0-or-later",
"authors":[
{
"name":"Fritz Michael Gschwantner",
Expand All @@ -26,18 +26,27 @@
}
],
"require":{
"php":">=5.2",
"contao/core-bundle":"^3.1|^4.4",
"contao-community-alliance/composer-plugin":"^2.4|^3.0"
"php": ">=7.4",
"contao/core-bundle": "^4.13 || ^5.0",
"contao/news-bundle": "^4.13 || ^5.0",
"doctrine/dbal": "^2.11 || ^3.0",
"symfony/config": "^5.4 || ^6.0",
"symfony/dependency-injection": "^5.4 || ^6.0",
"symfony/http-foundation": "^5.4 || ^6.0",
"symfony/http-kernel": "^5.4 || ^6.0",
"symfony/routing": "^5.4 || ^6.0",
"symfony-cmf/routing": "^2.3 || ^3.0",
"terminal42/service-annotation-bundle": "^1.0"
},
"replace": {
"contao-legacy/simple_news_urls": "self.version"
},
"extra":{
"contao":{
"sources":{
"system/modules/simple_news_urls":"system/modules/simple_news_urls"
}
"autoload": {
"psr-4": {
"InspiredMinds\\ContaoSimpleNewsUrls\\": "src/"
}
},
"extra": {
"contao-manager-plugin": "InspiredMinds\\ContaoSimpleNewsUrls\\ContaoManager\\Plugin"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0"
}
}
43 changes: 43 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
services:
_defaults:
autowire: true
autoconfigure: true

InspiredMinds\ContaoSimpleNewsUrls\:
resource: ../src

contao_simple_news_urls.routing.router:
class: Symfony\Cmf\Component\Routing\DynamicRouter
arguments:
- '@router.request_context'
- '@contao_simple_news_urls.routing.nested_matcher'
- '@contao_simple_news_urls.routing.route_generator'
- ''
- '@event_dispatcher'
- '@InspiredMinds\ContaoSimpleNewsUrls\Routing\RouteProvider'
tags:
- { name: router, priority: 70 }

contao_simple_news_urls.routing.nested_matcher:
class: Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher
arguments:
- '@InspiredMinds\ContaoSimpleNewsUrls\Routing\RouteProvider'
- '@contao_simple_news_urls.routing.final_matcher'

contao_simple_news_urls.routing.final_matcher:
class: Symfony\Cmf\Component\Routing\NestedMatcher\UrlMatcher
arguments:
- '@contao_simple_news_urls.routing.dummy_collection'
- '@contao_simple_news_urls.routing.dummy_context'

contao_simple_news_urls.routing.dummy_context:
class: Symfony\Component\Routing\RequestContext

contao_simple_news_urls.routing.dummy_collection:
class: Symfony\Component\Routing\RouteCollection

contao_simple_news_urls.routing.route_generator:
class: Symfony\Cmf\Component\Routing\ProviderBasedGenerator
arguments:
- '@InspiredMinds\ContaoSimpleNewsUrls\Routing\RouteProvider'
- '@logger'
27 changes: 27 additions & 0 deletions contao/dca/tl_news_archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao Simple News URLs extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

use Contao\CoreBundle\DataContainer\PaletteManipulator;

$GLOBALS['TL_DCA']['tl_news_archive']['fields']['enable_simple_urls'] = [
'exclude' => true,
'filter' => true,
'inputType' => 'checkbox',
'eval' => ['tl_class' => 'w50'],
'sql' => ['type' => 'boolean', 'default' => false],
];

PaletteManipulator::create()
->addLegend('simpleurls_legend', null)
->addField('enable_simple_urls', 'simpleurls_legend', PaletteManipulator::POSITION_APPEND)
->applyToPalette('default', 'tl_news_archive')
;
14 changes: 14 additions & 0 deletions contao/languages/de/tl_news_archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao Simple News URLs extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

$GLOBALS['TL_LANG']['tl_news_archive']['simpleurls_legend'] = 'Einfache Nachrichten-URLs';
$GLOBALS['TL_LANG']['tl_news_archive']['enable_simple_urls'] = ['Einfache Nachrichten-URLs aktivieren', 'Aktiviert Nachrichten-URLs in der Form <code>example.com/&lt;news-alias&gt;</code> für dieses Nachrichtenarchiv.'];
14 changes: 14 additions & 0 deletions contao/languages/en/tl_news_archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao Simple News URLs extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

$GLOBALS['TL_LANG']['tl_news_archive']['simpleurls_legend'] = 'Simple news URLs';
$GLOBALS['TL_LANG']['tl_news_archive']['enable_simple_urls'] = ['Enable simple news URLs', 'Enables news URLs in the format of <code>example.com/&lt;news-alias&gt;</code> for news articles of this news archive.'];
30 changes: 30 additions & 0 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao Simple News URLs extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoSimpleNewsUrls\ContaoManager;

use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use Contao\NewsBundle\ContaoNewsBundle;
use InspiredMinds\ContaoSimpleNewsUrls\ContaoSimpleNewsUrlsBundle;

class Plugin implements BundlePluginInterface
{
public function getBundles(ParserInterface $parser): array
{
return [
BundleConfig::create(ContaoSimpleNewsUrlsBundle::class)
->setLoadAfter([ContaoNewsBundle::class]),
];
}
}
23 changes: 23 additions & 0 deletions src/ContaoSimpleNewsUrlsBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao Simple News URLs extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoSimpleNewsUrls;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class ContaoSimpleNewsUrlsBundle extends Bundle
{
public function getPath(): string
{
return \dirname(__DIR__);
}
}
27 changes: 27 additions & 0 deletions src/DependencyInjection/ContaoSimpleNewsUrlsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Contao Simple News URLs extension.
*
* (c) inspiredminds
*
* @license LGPL-3.0-or-later
*/

namespace InspiredMinds\ContaoSimpleNewsUrls\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class ContaoSimpleNewsUrlsExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services.yaml');
}
}
Loading

0 comments on commit f83aea4

Please sign in to comment.