Skip to content

Commit

Permalink
Merge pull request #11 from dimaip/3.0
Browse files Browse the repository at this point in the history
!!! TASK: Neos 3.x compatibility
  • Loading branch information
aertmann authored Jan 26, 2017
2 parents b5fcfc8 + 262d964 commit b685905
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 176 deletions.
152 changes: 0 additions & 152 deletions Classes/MOC/NotFound/ViewHelpers/RequestViewHelper.php

This file was deleted.

157 changes: 157 additions & 0 deletions Classes/ViewHelpers/RequestViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
namespace MOC\NotFound\ViewHelpers;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Http\Request;
use Neos\Flow\Http\RequestHandler;
use Neos\Flow\Http\Response;
use Neos\Flow\Http\Uri;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\Mvc\Dispatcher;
use Neos\Flow\Mvc\Routing\Router;
use Neos\Flow\Security\Context as SecurityContext;
use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
use Neos\Neos\Domain\Service\ContentDimensionPresetSourceInterface;
use Neos\Neos\Routing\FrontendNodeRoutePartHandler;

/**
* Loads the content of a given URL
*/
class RequestViewHelper extends AbstractViewHelper
{
/**
* @Flow\Inject
* @var Dispatcher
*/
protected $dispatcher;

/**
* @Flow\Inject(lazy=false)
* @var Bootstrap
*/
protected $bootstrap;

/**
* @Flow\Inject(lazy=false)
* @var Router
*/
protected $router;

/**
* @Flow\Inject(lazy=false)
* @var SecurityContext
*/
protected $securityContext;

/**
* @Flow\Inject
* @var ConfigurationManager
*/
protected $configurationManager;

/**
* @Flow\InjectConfiguration(path="routing.supportEmptySegmentForDimensions", package="Neos.Neos")
* @var boolean
*/
protected $supportEmptySegmentForDimensions;

/**
* @Flow\Inject
* @var ContentDimensionPresetSourceInterface
*/
protected $contentDimensionPresetSource;

/**
* Initialize this engine
*
* @return void
*/
public function initializeObject()
{
$this->router->setRoutesConfiguration($this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_ROUTES));
}

/**
* @param string $path
* @return string
* @throws \Exception
*/
public function render($path = null)
{
$this->appendFirstUriPartIfValidDimension($path);
/** @var RequestHandler $activeRequestHandler */
$activeRequestHandler = $this->bootstrap->getActiveRequestHandler();
$parentHttpRequest = $activeRequestHandler->getHttpRequest();
$uri = rtrim($parentHttpRequest->getBaseUri(), '/') . '/' . $path;
$httpRequest = Request::create(new Uri($uri));
$matchingRoute = $this->router->route($httpRequest);
if (!$matchingRoute) {
$exception = new \Exception(sprintf('Uri with path "%s" could not be found.', $uri), 1426446160);
$exceptionHandler = set_exception_handler(null)[0];
$exceptionHandler->handleException($exception);
exit();
}
$request = new ActionRequest($parentHttpRequest);
foreach ($matchingRoute as $argumentName => $argumentValue) {
$request->setArgument($argumentName, $argumentValue);
}
$response = new Response($activeRequestHandler->getHttpResponse());

$this->securityContext->withoutAuthorizationChecks(function () use ($request, $response) {
$this->dispatcher->dispatch($request, $response);
});

return $response->getContent();
}

/**
* @param string $path
* @return void
*/
protected function appendFirstUriPartIfValidDimension(&$path)
{
$requestPath = ltrim($this->controllerContext->getRequest()->getHttpRequest()->getUri()->getPath(), '/');
$matches = [];
preg_match(FrontendNodeRoutePartHandler::DIMENSION_REQUEST_PATH_MATCHER, $requestPath, $matches);
if (!isset($matches['firstUriPart']) && !isset($matches['dimensionPresetUriSegments'])) {
return;
}

$dimensionPresets = $this->contentDimensionPresetSource->getAllPresets();
if (count($dimensionPresets) === 0) {
return;
}

$firstUriPartExploded = explode('_', $matches['firstUriPart'] ?: $matches['dimensionPresetUriSegments']);
if ($this->supportEmptySegmentForDimensions) {
foreach ($firstUriPartExploded as $uriSegment) {
$uriSegmentIsValid = false;
foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
$preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment);
if ($preset !== null) {
$uriSegmentIsValid = true;
break;
}
}
if (!$uriSegmentIsValid) {
return;
}
}
} else {
if (count($firstUriPartExploded) !== count($dimensionPresets)) {
return;
}
foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
$uriSegment = array_shift($firstUriPartExploded);
$preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment);
if ($preset === null) {
return;
}
}
}

$path = $matches['firstUriPart'] . '/' . $path;
}
}
2 changes: 1 addition & 1 deletion Configuration/Settings.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TYPO3:
Neos:
Flow:
error:
exceptionHandler:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ Introduction

Neos CMS package that loads a normal editable page for displaying a 404 error.

Compatible with Neos 2.x+
Compatible with Neos 2.x, 3.x

Supports multiple content dimensions with URI segments and empty segments for default dimensions.

Installation
------------
```composer require "moc/notfound:~2.0"```
```composer require "moc/notfound:^3.0"```

Create a page with the URI segment "404" in the root of your site. If using content dimensions with URI segments,
ensure a page exists in all contexts or through fallbacks.

Alternatively set the following configuration in ``Settings.yaml``:

```yaml
TYPO3:
Neos:
Flow:
error:
exceptionHandler:
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Templates/404.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{namespace notfound=MOC\NotFound\ViewHelpers}
<f:format.raw><notfound:request path="{f:if(condition: path, then: path, else: '404')}" /></f:format.raw>
<f:format.raw><notfound:request path="{f:if(condition: path, then: path, else: '404')}" /></f:format.raw>
Loading

0 comments on commit b685905

Please sign in to comment.