Skip to content

Commit

Permalink
NEXT-4137 - ignore themeplugins if the current saleschannel dont
Browse files Browse the repository at this point in the history
have this theme activated - also added new themeplugin bundleclass for
themerelated plugins
  • Loading branch information
s.sluiter authored and jenskueper committed Aug 7, 2019
1 parent 49941c6 commit 7f0b4a4
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Framework/DependencyInjection/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@
decorates="Shopware\Core\Framework\Routing\ApiRequestContextResolver">
<argument type="service" id="Shopware\Core\Framework\Routing\SalesChannelRequestContextResolver.inner"/>
<argument type="service" id="Shopware\Core\System\SalesChannel\Context\SalesChannelContextService"/>
<argument type="service" id="event_dispatcher"/>
</service>

<service id="Shopware\Core\Framework\Routing\Subscriber\SalesChannelContextResolvedSubscriber">
<argument type="service" id="twig"/>
<argument>%kernel.cache_dir%</argument>
<tag name="kernel.event_subscriber"/>
</service>

<service id="Shopware\Core\Framework\Api\Controller\SalesChannelProxyController" public="true">
Expand Down
24 changes: 24 additions & 0 deletions Framework/Routing/Event/SalesChannelContextResolvedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\Routing\Event;

use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Contracts\EventDispatcher\Event;

class SalesChannelContextResolvedEvent extends Event
{
/**
* @var SalesChannelContext
*/
private $salesChannelContext;

public function __construct(SalesChannelContext $salesChannelContext)
{
$this->salesChannelContext = $salesChannelContext;
}

public function getSalesChannelContext(): SalesChannelContext
{
return $this->salesChannelContext;
}
}
15 changes: 14 additions & 1 deletion Framework/Routing/SalesChannelRequestContextResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Shopware\Core\Framework\Routing;

use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Shopware\Core\Framework\Util\Random;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

Expand All @@ -20,12 +22,19 @@ class SalesChannelRequestContextResolver implements RequestContextResolverInterf
*/
private $contextService;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

public function __construct(
RequestContextResolverInterface $decorated,
SalesChannelContextServiceInterface $contextService
SalesChannelContextServiceInterface $contextService,
EventDispatcherInterface $eventDispatcher
) {
$this->decorated = $decorated;
$this->contextService = $contextService;
$this->eventDispatcher = $eventDispatcher;
}

public function resolve(SymfonyRequest $master, SymfonyRequest $request): void
Expand Down Expand Up @@ -56,6 +65,10 @@ public function resolve(SymfonyRequest $master, SymfonyRequest $request): void

$request->attributes->set(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT, $context->getContext());
$request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT, $context);

$this->eventDispatcher->dispatch(
new SalesChannelContextResolvedEvent($context)
);
}

public function handleSalesChannelContext(Request $request, Request $master, string $salesChannelId, string $contextToken): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Framework\Routing\Subscriber;

use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;

class SalesChannelContextResolvedSubscriber implements EventSubscriberInterface
{
/**
* @var Environment
*/
private $twig;

/**
* @var string
*/
private $cacheDir;

public function __construct(
Environment $twig,
string $cacheDir
) {
$this->twig = $twig;
$this->cacheDir = $cacheDir;
}

public static function getSubscribedEvents(): array
{
return [
SalesChannelContextResolvedEvent::class => [
['resolved'],
],
];
}

public function resolved(SalesChannelContextResolvedEvent $event): void
{
// Set individual saleschannel twig cache
$this->twig->setCache(
$this->cacheDir . '/'
. $event->getSalesChannelContext()->getSalesChannel()->getId()
);
}
}
29 changes: 19 additions & 10 deletions Framework/Twig/TemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
class TemplateFinder
{
/**
* @var array
* @var FilesystemLoader
*/
private $bundles;
protected $loader;

/**
* @var FilesystemLoader
* @var Kernel
*/
protected $kernel;
/**
* @var array
*/
private $loader;
private $bundles;

public function __construct(FilesystemLoader $loader, Kernel $kernel)
{
$this->loader = $loader;

$this->kernel = $kernel;
$this->addBundles($kernel);
}

Expand Down Expand Up @@ -66,14 +70,14 @@ public function find(string $template, $ignoreMissing = false, ?string $startAt
{
$template = ltrim($template, '@');

$queue = $this->bundles;
$filterdBundles = $queue = $this->filterBundles($this->bundles);

if ($startAt) {
$index = array_search($startAt, $this->bundles, true);
$index = array_search($startAt, $filterdBundles, true);

$queue = array_merge(
array_slice($this->bundles, $index + 1),
array_slice($this->bundles, 0, $index + 1)
array_slice($filterdBundles, $index + 1),
array_slice($filterdBundles, 0, $index + 1)
);
}

Expand All @@ -91,7 +95,12 @@ public function find(string $template, $ignoreMissing = false, ?string $startAt
throw new LoaderError(sprintf('Unable to load template "%s". (Looked into: %s)', $template, implode(', ', array_values($queue))));
}

private function addBundles(Kernel $kernel): void
protected function filterBundles(array $bundles)
{
return $bundles;
}

protected function addBundles(Kernel $kernel): void
{
$bundles = [];

Expand Down
4 changes: 4 additions & 0 deletions SalesChannelRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ final class SalesChannelRequest

public const ATTRIBUTE_THEME_ID = 'theme-id';

public const ATTRIBUTE_THEME_NAME = 'theme-name';

public const ATTRIBUTE_THEME_BASE_NAME = 'theme-base-name';

/**
* domain-resolved attributes
*/
Expand Down

0 comments on commit 7f0b4a4

Please sign in to comment.