Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2f1f76

Browse files
committed
refactor ContaoFilesystemLoader + make it only apply to Contao namespaces
1 parentc8474d4 commitc2f1f76

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

‎core-bundle/src/Resources/config/services.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ services:
916916
-'%kernel.project_dir%'
917917
tags:
918918
-{ name: twig.loader, priority: 2 }
919+
-{ name: kernel.reset, method: reset }
919920

920921
Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoaderWarmer:
921922
arguments:

‎core-bundle/src/Twig/Loader/ContaoFilesystemLoader.php‎

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public function __construct(CacheItemPoolInterface $cachePool, TemplateLocator $
9090
// Restore hierarchy from cache
9191
$hierarchyItem =$cachePool->getItem(self::CACHE_KEY_HIERARCHY);
9292

93-
if ($hierarchyItem->isHit()) {
94-
$this->hierarchy =$hierarchyItem->get();
95-
$this->hierarchyHash =$this->createHash($this->hierarchy);
93+
if ($hierarchyItem->isHit() &&null !== ($hierarchy =$hierarchyItem->get())) {
94+
$this->hierarchy =$hierarchy;
95+
$this->hierarchyHash =$this->createHash($hierarchy);
9696
}
9797
}
9898

@@ -103,10 +103,14 @@ public function __construct(CacheItemPoolInterface $cachePool, TemplateLocator $
103103
* that should be available in the Contao template hierarchy.
104104
*
105105
* @param string $path A path where to look for templates
106-
* @param string $namespace A path namespace
106+
* @param string $namespace A"Contao" or "Contao_*"path namespace
107107
*/
108-
publicfunctionaddPath($path,$namespace =self::MAIN_NAMESPACE,bool$trackTemplates =false):void
108+
publicfunctionaddPath($path,$namespace ='Contao',bool$trackTemplates =false):void
109109
{
110+
if (null ===$this->parseName("@$namespace")) {
111+
thrownewLoaderError("Tried to register an invalid Contao namespace '$namespace'.");
112+
}
113+
110114
try {
111115
parent::addPath($path,$namespace);
112116
}catch (LoaderError$error) {
@@ -127,17 +131,26 @@ public function addPath($path, $namespace = self::MAIN_NAMESPACE, bool $trackTem
127131
* Prepends a path where templates are stored (if it exists).
128132
*
129133
* @param string $path A path where to look for templates
130-
* @param string $namespace A path namespace
134+
* @param string $namespace A"Contao" or "Contao_*"path namespace
131135
*/
132-
publicfunctionprependPath($path,$namespace =self::MAIN_NAMESPACE):void
136+
publicfunctionprependPath($path,$namespace ='Contao'):void
133137
{
138+
if (null ===$this->parseName("@$namespace")) {
139+
thrownewLoaderError("Tried to register an invalid Contao namespace '$namespace'.");
140+
}
141+
134142
try {
135143
parent::prependPath($path,$namespace);
136144
}catch (LoaderError$error) {
137145
// Ignore
138146
}
139147
}
140148

149+
publicfunctiongetPaths($namespace ='Contao'):array
150+
{
151+
returnparent::getPaths($namespace);
152+
}
153+
141154
/**
142155
* Clears all registered template paths.
143156
*/
@@ -178,11 +191,13 @@ public function persist(): void
178191
*/
179192
publicfunctiongetCacheKey($name):string
180193
{
181-
// We're basically cache busting `@Contao` and `@Contao_*` namespaced
182-
// templates by appending a hash that changes whenever the registered
183-
// hierarchy changes
184-
$suffix =null !==$this->hierarchyHash &&1 ===preg_match('%^(@Contao(_.*)?)/%',$name) ?
185-
"_$this->hierarchyHash" :'';
194+
// We're basically cache busting templates by appending a hash that
195+
// changes whenever the registered hierarchy changes
196+
if (null ===$this->hierarchyHash) {
197+
$this->buildHierarchy();
198+
}
199+
200+
$suffix ="_$this->hierarchyHash";
186201

187202
if (null !== ($themeTemplateName =$this->getThemeTemplateName($name))) {
188203
returnparent::getCacheKey($themeTemplateName).$suffix;
@@ -295,9 +310,20 @@ public function buildHierarchy(): void
295310
$this->hierarchyHash =$this->createHash($hierarchy);
296311
}
297312

298-
privatefunctioncreateHash(array$array):string
313+
/**
314+
* Split a Contao name into [namespace, short name]. The short name part
315+
* will be null if $name is only a namespace.
316+
*
317+
* If parsing fails - i.e. if the given name does not describe a "Contao"
318+
* or "Contao_*" namespace - null is returned instead.
319+
*/
320+
privatefunctionparseName(string$logicalNameOrNamespace): ?array
299321
{
300-
returnsubstr(md5(json_encode($array,JSON_THROW_ON_ERROR)),0,6);
322+
if (1 ===preg_match('%^@(Contao(?:_[A-za-z0-9]+)?)(?:/(.*))?$%',$logicalNameOrNamespace,$matches)) {
323+
return [$matches[1],$matches[2] ??null];
324+
}
325+
326+
returnnull;
301327
}
302328

303329
privatefunctiongetIdentifier(string$shortName):string
@@ -306,21 +332,26 @@ private function getIdentifier(string $shortName): string
306332
returnpreg_replace('/(.*)(\.html5|\.html.twig)/','$1',$shortName);
307333
}
308334

335+
privatefunctioncreateHash(array$array):string
336+
{
337+
returnsubstr(md5(json_encode($array,JSON_THROW_ON_ERROR)),0,6);
338+
}
339+
309340
/**
310341
* Returns the template name of a theme specific variant of the given name
311342
* or null if not applicable.
312343
*/
313344
privatefunctiongetThemeTemplateName(string$name): ?string
314345
{
315-
if (1 !==preg_match('%^@Contao/(.*)%',$name,$matches)) {
346+
if (null === ($parts =$this->parseName($name)) ||'Contao' !==$parts[0]) {
316347
returnnull;
317348
}
318349

319350
if (false === ($themeSlug =$this->currentThemeSlug ??$this->getThemeSlug())) {
320351
returnnull;
321352
}
322353

323-
$template ="@Contao_Theme_$themeSlug/$matches[1]";
354+
$template ="@Contao_Theme_$themeSlug/$parts[1]";
324355

325356
return$this->exists($template) ?$template :null;
326357
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp