-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tainting] Fix the precedence of the CachedTemplatesMapping (#89)
Allow alternatives template name notation Isolate template naming in a CachedTemplatesRegistry Allow `render` calls with no second arguments Allow twig template name old notation alternatives
- Loading branch information
1 parent
01b5dcb
commit f75effe
Showing
8 changed files
with
329 additions
and
47 deletions.
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psalm\SymfonyPsalmPlugin\Twig; | ||
|
||
use Exception; | ||
|
||
class CachedTemplateNotFoundException extends Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('No cache found for template with name(s) :'); | ||
} | ||
|
||
public function addTriedName(string $possibleName): void | ||
{ | ||
$this->message .= ' '.$possibleName; | ||
} | ||
} |
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
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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psalm\SymfonyPsalmPlugin\Twig; | ||
|
||
use Generator; | ||
|
||
class CachedTemplatesRegistry | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
private $mapping = []; | ||
|
||
public function addTemplate(string $cacheClassName, string $templateName): void | ||
{ | ||
$this->mapping[$templateName] = $cacheClassName; | ||
} | ||
|
||
/** | ||
* @throws CachedTemplateNotFoundException | ||
*/ | ||
public function getCacheClassName(string $templateName): string | ||
{ | ||
$probableException = new CachedTemplateNotFoundException(); | ||
|
||
foreach (self::generateNames($templateName) as $possibleName) { | ||
if (array_key_exists($possibleName, $this->mapping)) { | ||
return $this->mapping[$possibleName]; | ||
} | ||
$probableException->addTriedName($possibleName); | ||
} | ||
|
||
throw $probableException; | ||
} | ||
|
||
/** | ||
* @return Generator<string> | ||
*/ | ||
private static function generateNames(string $baseName): Generator | ||
{ | ||
yield $baseName; | ||
|
||
/** @var string|null $oldNotation */ | ||
$oldNotation = null; | ||
|
||
$alternativeNotation = preg_replace('/^@([^\/]+)\/?(.+)?\/([^\/]+\.twig)/', '$1Bundle:$2:$3', $baseName); | ||
if ($alternativeNotation !== $baseName) { | ||
yield $alternativeNotation; | ||
$oldNotation = $alternativeNotation; | ||
} | ||
|
||
$alternativeNotation = preg_replace('/^(.+)Bundle:(.+)?:(.+\.twig)$/', '@$1/$2/$3', $baseName); | ||
if ($alternativeNotation !== $baseName) { | ||
yield str_replace('//', '/', $alternativeNotation); | ||
$oldNotation = $baseName; | ||
} | ||
|
||
if (null !== $oldNotation) { | ||
list($bundleName, $rest) = explode(':', $oldNotation, 2); | ||
list($revTemplateName, $revRest) = explode(':', strrev($rest), 2); | ||
$pathParts = explode('/', strrev($revRest)); | ||
$pathParts = array_merge($pathParts, explode('/', strrev($revTemplateName))); | ||
for ($i = 0; $i <= count($pathParts); ++$i) { | ||
yield $bundleName.':'. | ||
implode('/', array_slice($pathParts, 0, $i)).':'. | ||
implode('/', array_slice($pathParts, $i)); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.