Skip to content

Commit

Permalink
[BUGFIX] Process paths individually in warmup (#397)
Browse files Browse the repository at this point in the history
A bug in the warmup code meant that all templates,
partials and layouts which were in sub-folders in
the root paths, would cause file not resolved errors
due to using basename() on the absolute file path.

To remedy this, each path has to be traversed by
itself and the relative file path has to be determined
by trimming off the root path. The result is that any
level of sub-folders can be supported in warmup.
  • Loading branch information
NamelessCoder authored and mbrodala committed Jul 16, 2018
1 parent a67b31f commit 9726683
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 38 deletions.
80 changes: 49 additions & 31 deletions src/Core/Cache/StandardCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,36 @@ protected function warmupTemplateRootPaths(RenderingContextInterface $renderingC
$paths = $renderingContext->getTemplatePaths();
foreach ($this->formats as $format) {
$paths->setFormat($format);
foreach ($this->detectControllerNamesInTemplateRootPaths($paths->getTemplateRootPaths()) as $controllerName) {
foreach ($paths->resolveAvailableTemplateFiles($controllerName, $format) as $templateFile) {
$formatCutoffPoint = - (strlen($format) + 1);
foreach ($paths->getTemplateRootPaths() as $templateRootPath) {
$pathCutoffPoint = strlen($templateRootPath);
foreach ($this->detectControllerNamesInTemplateRootPaths([$templateRootPath]) as $controllerName) {
foreach ($paths->resolveAvailableTemplateFiles($controllerName, $format) as $templateFile) {
$state = $this->warmSingleFile(
$templateFile,
$paths->getTemplateIdentifier(
$controllerName,
substr($templateFile, $pathCutoffPoint, $formatCutoffPoint)
),
$renderingContext
);
$result->add($state, $templateFile);
}
}
$limitedPaths = clone $paths;
$limitedPaths->setTemplateRootPaths([$templateRootPath]);
foreach ($limitedPaths->resolveAvailableTemplateFiles(null, $format) as $templateFile) {
$state = $this->warmSingleFile(
$templateFile,
$paths->getTemplateIdentifier(
$controllerName,
basename($templateFile, '.' . $format)
'Default',
substr($templateFile, $pathCutoffPoint, $formatCutoffPoint)
),
$renderingContext
);
$result->add($state, $templateFile);
}
}
foreach ($paths->resolveAvailableTemplateFiles(null, $format) as $templateFile) {
$state = $this->warmSingleFile(
$templateFile,
$paths->getTemplateIdentifier(
'Default',
basename($templateFile, '.' . $format)
),
$renderingContext
);
$result->add($state, $templateFile);
}
}
return $result;
}
Expand All @@ -158,14 +164,20 @@ protected function warmupPartialRootPaths(RenderingContextInterface $renderingCo
$result = new FluidCacheWarmupResult();
$paths = $renderingContext->getTemplatePaths();
foreach ($this->formats as $format) {
foreach ($paths->resolveAvailablePartialFiles($format) as $partialFile) {
$paths->setFormat($format);
$state = $this->warmSingleFile(
$partialFile,
$paths->getPartialIdentifier(basename($partialFile, '.' . $format)),
$renderingContext
);
$result->add($state, $partialFile);
$formatCutoffPoint = - (strlen($format) + 1);
foreach ($paths->getPartialRootPaths() as $partialRootPath) {
$limitedPaths = clone $paths;
$limitedPaths->setPartialRootPaths([$partialRootPath]);
$pathCutoffPoint = strlen($partialRootPath);
foreach ($limitedPaths->resolveAvailablePartialFiles($format) as $partialFile) {
$paths->setFormat($format);
$state = $this->warmSingleFile(
$partialFile,
$paths->getPartialIdentifier(substr($partialFile, $pathCutoffPoint, $formatCutoffPoint)),
$renderingContext
);
$result->add($state, $partialFile);
}
}
}
return $result;
Expand All @@ -189,14 +201,20 @@ protected function warmupLayoutRootPaths(RenderingContextInterface $renderingCon
$result = new FluidCacheWarmupResult();
$paths = $renderingContext->getTemplatePaths();
foreach ($this->formats as $format) {
foreach ($paths->resolveAvailableLayoutFiles($format) as $layoutFile) {
$paths->setFormat($format);
$state = $this->warmSingleFile(
$layoutFile,
$paths->getLayoutIdentifier(basename($layoutFile, '.' . $layoutFile)),
$renderingContext
);
$result->add($state, $layoutFile);
$formatCutoffPoint = - (strlen($format) + 1);
foreach ($paths->getLayoutRootPaths() as $layoutRootPath) {
$limitedPaths = clone $paths;
$limitedPaths->setLayoutRootPaths([$layoutRootPath]);
$pathCutoffPoint = strlen($layoutRootPath);
foreach ($limitedPaths->resolveAvailableLayoutFiles($format) as $layoutFile) {
$paths->setFormat($format);
$state = $this->warmSingleFile(
$layoutFile,
$paths->getLayoutIdentifier(substr($layoutFile, $pathCutoffPoint, $formatCutoffPoint)),
$renderingContext
);
$result->add($state, $layoutFile);
}
}
}
return $result;
Expand Down
20 changes: 13 additions & 7 deletions tests/Unit/Core/Cache/StandardCacheWarmerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function testWarm()
$subject = $this->getMockBuilder(StandardCacheWarmer::class)
->setMethods(['warmSingleFile', 'detectControllerNamesInTemplateRootPaths'])
->getMock();
$subject->expects($this->exactly(7))
$subject->expects($this->atLeastOnce())
->method('detectControllerNamesInTemplateRootPaths')
->willReturn(['Default', 'Standard']);
$subject->expects($this->exactly(70))
$subject->expects($this->atLeastOnce())
->method('warmSingleFile')
->willReturn($failedCompilingState);
$context = new RenderingContextFixture();
Expand All @@ -47,20 +47,26 @@ public function testWarm()
'resolveAvailableTemplateFiles',
'resolveAvailablePartialFiles',
'resolveAvailableLayoutFiles',
'resolveFileInPaths'
'resolveFileInPaths',
'getTemplateRootPaths',
'getPartialRootPaths',
'getLayoutRootPaths',
]
)
->getMock();
$paths->expects($this->exactly(21))
$paths->expects($this->atLeastOnce())
->method('resolveAvailableTemplateFiles')
->willReturn(['foo', 'bar']);
$paths->expects($this->exactly(7))
$paths->expects($this->atLeastOnce())
->method('resolveAvailablePartialFiles')
->willReturn(['foo', 'bar']);
$paths->expects($this->exactly(7))
$paths->expects($this->atLeastOnce())
->method('resolveAvailableLayoutFiles')
->willReturn(['foo', 'bar']);
$paths->expects($this->exactly(56))->method('resolveFileInPaths')->willReturn('/dev/null');
$paths->expects($this->atLeastOnce())->method('resolveFileInPaths')->willReturn('/dev/null');
$paths->expects($this->atLeastOnce())->method('getTemplateRootPaths')->willReturn(['/dev/null']);
$paths->expects($this->atLeastOnce())->method('getPartialRootPaths')->willReturn(['/dev/null']);
$paths->expects($this->atLeastOnce())->method('getLayoutRootPaths')->willReturn(['/dev/null']);
$compiler = $this->getMockBuilder(TemplateCompiler::class)
->setMethods(['enterWarmupMode'])
->getMock();
Expand Down

0 comments on commit 9726683

Please sign in to comment.