-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch layout builder for improved performance
- Loading branch information
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
services/drupal/patches/drupal-layout-builder-performance-3186116-11.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php | ||
index 587fe66b1f9a6ad0ae6d044ef0dbf09856e0e3c4..96fbf9e0d645ef1e53fd0332553adb856e821ef8 100644 | ||
--- a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php | ||
+++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php | ||
@@ -62,13 +62,33 @@ protected function getContextDefinitions($plugin_definition) { | ||
*/ | ||
public function checkRequirements(array $contexts, array $requirements) { | ||
foreach ($requirements as $requirement) { | ||
- if ($requirement->isRequired() && !$this->getMatchingContexts($contexts, $requirement)) { | ||
+ if ($requirement->isRequired() && !$this->isAnyContextMatching($contexts, $requirement)) { | ||
return FALSE; | ||
} | ||
} | ||
return TRUE; | ||
} | ||
|
||
+ /** | ||
+ * Determines if any contexts satisfy the constraints of a given definition. | ||
+ * | ||
+ * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts | ||
+ * An array of contexts. | ||
+ * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $definition | ||
+ * The definition to satisfy. | ||
+ * | ||
+ * @return bool | ||
+ * TRUE if any of the contexts satisfy the constraints, FALSE otherwise. | ||
+ */ | ||
+ private function isAnyContextMatching(array $contexts, ContextDefinitionInterface $definition): bool { | ||
+ foreach ($contexts as $context) { | ||
+ if ($definition->isSatisfiedBy($context)) { | ||
+ return TRUE; | ||
+ } | ||
+ } | ||
+ return FALSE; | ||
+ } | ||
+ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
diff --git a/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php b/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php | ||
index 075cd51d92f6be29e47308b6da15faf8b5dd142d..66c7ffa13033d66b62e078b198833793e9747bc9 100644 | ||
--- a/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php | ||
+++ b/core/tests/Drupal/Tests/Core/Plugin/ContextHandlerTest.php | ||
@@ -19,7 +19,9 @@ | ||
use Drupal\Core\DependencyInjection\ContainerBuilder; | ||
use Drupal\Core\Extension\ModuleHandlerInterface; | ||
use Drupal\Core\Plugin\Context\ContextDefinition; | ||
+use Drupal\Core\Plugin\Context\ContextDefinitionInterface; | ||
use Drupal\Core\Plugin\Context\ContextHandler; | ||
+use Drupal\Core\Plugin\Context\ContextInterface; | ||
use Drupal\Core\Plugin\ContextAwarePluginInterface; | ||
use Drupal\Core\TypedData\TypedDataManager; | ||
use Drupal\Core\Validation\ConstraintManager; | ||
@@ -124,6 +126,31 @@ public function providerTestCheckRequirements() { | ||
return $data; | ||
} | ||
|
||
+ /** | ||
+ * @covers ::checkRequirements | ||
+ */ | ||
+ public function testCheckRequirementsEarlyReturn() { | ||
+ // Set up three contexts. | ||
+ $context1 = $this->prophesize(ContextInterface::class); | ||
+ $context2 = $this->prophesize(ContextInterface::class); | ||
+ $context3 = $this->prophesize(ContextInterface::class); | ||
+ $contexts[] = $context1->reveal(); | ||
+ $contexts[] = $context2->reveal(); | ||
+ $contexts[] = $context3->reveal(); | ||
+ | ||
+ $requirement = $this->prophesize(ContextDefinitionInterface::class); | ||
+ $requirement->isRequired()->willReturn(TRUE); | ||
+ | ||
+ // Set the second context to satisfy the requirement. The third context | ||
+ // should not be checked. | ||
+ $requirement->isSatisfiedBy($context1)->willReturn(FALSE); | ||
+ $requirement->isSatisfiedBy($context2)->willReturn(TRUE); | ||
+ $requirement->isSatisfiedBy($context3)->shouldNotBeCalled(); | ||
+ | ||
+ $requirements[] = $requirement->reveal(); | ||
+ $this->contextHandler->checkRequirements($contexts, $requirements); | ||
+ } | ||
+ | ||
/** | ||
* @covers ::getMatchingContexts | ||
* |