Skip to content

Commit

Permalink
FEATURE: Allow to have empty uriSegment for host-based default
Browse files Browse the repository at this point in the history
  • Loading branch information
kdambekalns committed Nov 22, 2017
1 parent 7842aab commit b176aff
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
55 changes: 55 additions & 0 deletions Classes/Aspects/RouteCacheAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Flownative\Neos\HostBasedDefaultPreset\Aspects;

/*
* This file is part of the Flownative.Neos.HostBasedDefaultPreset package.
*
* (c) 2017 Karsten Dambekalns, Flownative GmbH
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Http\HttpRequestHandlerInterface;

/**
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
class RouteCacheAspect
{
/**
* @Flow\Inject
* @var Bootstrap
*/
protected $bootstrap;

/**
* Add the request host as a tag for the route cache entry
*
* @Flow\Around("method(Neos\Flow\Mvc\Routing\RouterCachingService->buildResolveCacheIdentifier())")
* @param JoinPointInterface $joinPoint The current join point
* @return array
* @throws \Exception
*/
public function addHostname(JoinPointInterface $joinPoint)
{
$resolveCacheIdentifier = $joinPoint->getAdviceChain()->proceed($joinPoint);

$activeRequestHandler = $this->bootstrap->getActiveRequestHandler();
if ($activeRequestHandler instanceof HttpRequestHandlerInterface) {
$requestedHost = $activeRequestHandler->getHttpRequest()->getUri()->getHost();
$resolveCacheIdentifier = md5(sprintf(
'%s_%s',
$resolveCacheIdentifier,
$requestedHost
));
}

return $resolveCacheIdentifier;
}
}
19 changes: 18 additions & 1 deletion Classes/Service/ConfigurationContentDimensionPresetSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class ConfigurationContentDimensionPresetSource extends NeosConfigurationContent
*/
protected $configuration;

/**
* @Flow\InjectConfiguration(path="forceEmptyUriSegment")
* @var array
*/
protected $forceEmptyUriSegment;

/**
* @Flow\InjectConfiguration(path="dimensions")
* @var array
Expand All @@ -42,6 +48,11 @@ class ConfigurationContentDimensionPresetSource extends NeosConfigurationContent
*/
protected $bootstrap;

/**
* Adjusts the incoming configuration as needed when the object is instantiated.
*
* @return void
*/
protected function initializeObject()
{
if ($this->dimensionsSettings === []) {
Expand All @@ -58,7 +69,13 @@ protected function initializeObject()
$this->dimensionsSettings[$dimensionName],
$this->dimensionsSettings[$dimensionName]['defaultPresetByHost'][$requestedHost]
)) {
$dimensionConfiguration['defaultPreset'] = $this->dimensionsSettings[$dimensionName]['defaultPresetByHost'][$requestedHost];
$newDefaultPreset = $this->dimensionsSettings[$dimensionName]['defaultPresetByHost'][$requestedHost];

$dimensionConfiguration['defaultPreset'] = $newDefaultPreset;

if ($this->forceEmptyUriSegment === true) {
$dimensionConfiguration['presets'][$newDefaultPreset]['uriSegment'] = '';
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Flownative:
Neos:
HostBasedDefaultPreset:
forceEmptyUriSegment: false
dimensions: []
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ After setting up your content dimensions as usual, configure the default presets
'acme.com': 'en'
'acme.de': 'de'

If you like, you can configure the package to set the `uriSegment` for each default preset to an empty string:

Flownative:
Neos:
HostBasedDefaultPreset:
forceEmptyUriSegment: true

This way the default dimension value "disappears" from the URL. Make sure to set a segment for all values in the
"real" dimension configuration, to be able to switch on all hosts without issues!

## Credits

Development of this package has been sponsored by web&co OG, Vienna, Austria.

0 comments on commit b176aff

Please sign in to comment.