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

Use site_url to build form cache #7193

Merged
merged 2 commits into from Apr 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/bundles/CoreBundle/Config/config.php
Expand Up @@ -213,6 +213,17 @@
'templating.helper.assets',
],
],
'mautic.core.subscriber.router' => [
'class' => \Mautic\CoreBundle\EventListener\RouterSubscriber::class,
'arguments' => [
'router',
'%router.request_context.scheme%',
'%router.request_context.host%',
'%request_listener.https_port%',
'%request_listener.http_port%',
'%router.request_context.base_url%',
],
],
],
'forms' => [
'mautic.form.type.spacer' => [
Expand Down
109 changes: 109 additions & 0 deletions app/bundles/CoreBundle/EventListener/RouterSubscriber.php
@@ -0,0 +1,109 @@
<?php

/*
* @copyright 2018 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\CoreBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;

class RouterSubscriber implements EventSubscriberInterface
{
/**
* @var RouterInterface
*/
private $router;

/**
* @var string|null
*/
private $baseUrl;

/**
* @var string|null
*/
private $scheme;

/**
* @var string|null
*/
private $host;

/**
* @var string|null
*/
private $httpsPort;

/**
* @var string|null
*/
private $httpPort;

/**
* RouterSubscriber constructor.
*
* @param RouterInterface $router
* @param null|string $scheme
* @param null|string $host
* @param null|string $httpsPort
* @param null|string $httpPort
* @param null|string $baseUrl
*/
public function __construct(RouterInterface $router, $scheme, $host, $httpsPort, $httpPort, $baseUrl)
{
$this->router = $router;
$this->scheme = $scheme;
$this->host = $host;
$this->httpsPort = $httpsPort;
$this->httpPort = $httpPort;
$this->baseUrl = $baseUrl;
}

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['setRouterRequestContext', 1],
];
}

/**
* This forces generated routes to be the same as what is configured as Mautic's site_url
* in order to prevent mismatches between cached URLs generated during web requests and URLs generated
* via CLI/cron jobs.
*
* @param GetResponseEvent $event
*/
public function setRouterRequestContext(GetResponseEvent $event)
{
if (empty($this->host)) {
return;
}

if (!$event->isMasterRequest()) {
return;
}

if ('dev' === MAUTIC_ENV) {
$this->baseUrl = '/index_dev.php'.$this->baseUrl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to work for any dev site that isn't at root.
For example, all mautibox PRs now redirect haphazardly:
https://mautibox.com/index_dev.php/7210/s/dashboard

Can we remove this? I don't see it as needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alanhartless what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll be a pain for developing if none of the URLs you click on has index_dev.php in it. I'd rather fix this to work with paths than just remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed... will you be doing that or should I?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

$context = $this->router->getContext();
$context->setBaseUrl($this->baseUrl);
$context->setScheme($this->scheme);
$context->setHost($this->host);
$context->setHttpPort($this->httpPort);
$context->setHttpsPort($this->httpsPort);
}
}