Skip to content

Commit 87293f9

Browse files
committed
feat: add symfony config for block names
1 parent 0ffeb45 commit 87293f9

File tree

7 files changed

+109
-19
lines changed

7 files changed

+109
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Frosh\DevelopmentHelper\Component\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
class Configuration implements ConfigurationInterface
9+
{
10+
public function getConfigTreeBuilder(): TreeBuilder
11+
{
12+
$treeBuilder = new TreeBuilder('frosh_development_helper');
13+
14+
$rootNode = $treeBuilder->getRootNode();
15+
$rootNode
16+
->children()
17+
->arrayNode('twig')
18+
->children()
19+
->arrayNode('exclude_keywords')
20+
->scalarPrototype()
21+
->end()
22+
;
23+
24+
return $treeBuilder;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Frosh\DevelopmentHelper\Component\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Extension\Extension;
7+
8+
class FroshDevelopmentHelperExtension extends Extension
9+
{
10+
public function load(array $configs, ContainerBuilder $container)
11+
{
12+
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
13+
$this->addConfig($container, $this->getAlias(), $config);
14+
}
15+
16+
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
17+
{
18+
return new Configuration();
19+
}
20+
21+
private function addConfig(ContainerBuilder $container, string $alias, array $options): void
22+
{
23+
foreach ($options as $key => $option) {
24+
$container->setParameter($alias . '.' . $key, $option);
25+
26+
if (\is_array($option)) {
27+
$this->addConfig($container, $alias . '.' . $key, $option);
28+
}
29+
}
30+
}
31+
}

src/Component/Twig/Extension/BlockCommentExtension.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ class BlockCommentExtension extends AbstractExtension
1414
*/
1515
private $kernelRootDir;
1616

17-
public function __construct(string $kernelRootDir)
17+
private array $twigExcludeKeywords;
18+
19+
public function __construct(string $kernelRootDir, array $twigExcludeKeywords)
1820
{
1921
$this->kernelRootDir = $kernelRootDir;
22+
$this->twigExcludeKeywords = $twigExcludeKeywords;
2023
}
2124

2225
public function getNodeVisitors()
2326
{
24-
return [new BlogCommentNodeVisitor($this->kernelRootDir)];
27+
return [new BlogCommentNodeVisitor($this->kernelRootDir, $this->twigExcludeKeywords)];
2528
}
2629
}

src/Component/Twig/NodeVisitor/BlogCommentNodeVisitor.php

+5-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Twig\Environment;
66
use Twig\Loader\ArrayLoader;
7-
use Twig\Loader\FilesystemLoader;
87
use Twig\Node\BlockNode;
98
use Twig\Node\BodyNode;
109
use Twig\Node\ModuleNode;
@@ -15,27 +14,17 @@
1514

1615
class BlogCommentNodeVisitor extends AbstractNodeVisitor
1716
{
18-
19-
/** Parts of block names which need to be skipped, for example block that appear
20-
 * inside a HTML attribute and would corrupt rendering */
21-
private const SKIP_BLOCK_KEYWORDS = [
22-
'form_action',
23-
'class',
24-
'attribute',
25-
'title',
26-
'sitemap',
27-
'head_meta_tags',
28-
'page_checkout_additional',
29-
];
30-
3117
/**
3218
* @var string
3319
*/
3420
private $kernelRootDir;
3521

36-
public function __construct(string $kernelRootDir)
22+
private array $twigExcludeKeywords;
23+
24+
public function __construct(string $kernelRootDir, array $twigExcludeKeywords)
3725
{
3826
$this->kernelRootDir = $kernelRootDir;
27+
$this->twigExcludeKeywords = $twigExcludeKeywords;
3928
}
4029

4130

@@ -110,7 +99,7 @@ private function shouldSkip(Node $node): bool
11099
return true;
111100
}
112101

113-
foreach (self::SKIP_BLOCK_KEYWORDS as $key) {
102+
foreach ($this->twigExcludeKeywords as $key) {
114103
if (strpos($name, $key) !== false) {
115104
return true;
116105
}

src/FroshDevelopmentHelper.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
namespace Frosh\DevelopmentHelper;
44

5-
use Composer\Autoload\ClassLoader;
65
use Frosh\DevelopmentHelper\Component\DependencyInjection\BuildEntityDefinitionNamesCompilerPass;
76
use Frosh\DevelopmentHelper\Component\DependencyInjection\CustomProfilerExtensions;
87
use Frosh\DevelopmentHelper\Component\DependencyInjection\DisableTwigCacheCompilerPass;
8+
use Frosh\DevelopmentHelper\Component\DependencyInjection\FroshDevelopmentHelperExtension;
99
use Shopware\Core\Framework\Plugin;
10+
use Shopware\Core\Kernel;
11+
use Symfony\Component\Config\FileLocator;
12+
use Symfony\Component\Config\Loader\DelegatingLoader;
13+
use Symfony\Component\Config\Loader\LoaderResolver;
14+
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
15+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1016
use Symfony\Component\DependencyInjection\ContainerBuilder;
1117

1218
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
@@ -20,6 +26,30 @@ public function build(ContainerBuilder $container): void
2026
$container->addCompilerPass(new DisableTwigCacheCompilerPass());
2127
$container->addCompilerPass(new CustomProfilerExtensions());
2228
$container->addCompilerPass(new BuildEntityDefinitionNamesCompilerPass());
29+
30+
$this->buildConfig($container);
31+
2332
parent::build($container);
2433
}
34+
35+
public function createContainerExtension(): FroshDevelopmentHelperExtension
36+
{
37+
return new FroshDevelopmentHelperExtension();
38+
}
39+
40+
private function buildConfig(ContainerBuilder $container): void
41+
{
42+
$locator = new FileLocator('Resources/config');
43+
44+
$resolver = new LoaderResolver([
45+
new YamlFileLoader($container, $locator),
46+
new GlobFileLoader($container, $locator),
47+
]);
48+
49+
$configLoader = new DelegatingLoader($resolver);
50+
51+
$confDir = $this->getPath() . '/Resources/config';
52+
53+
$configLoader->load($confDir . '/{packages}/*' . Kernel::CONFIG_EXTS, 'glob');
54+
}
2555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
frosh_development_helper:
2+
twig:
3+
exclude_keywords:
4+
- form_action
5+
- class
6+
- attribute
7+
- title
8+
- sitemap
9+
- head_meta_tags
10+
- page_checkout_additional

src/Resources/config/services.xml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<bind key="$kernelRootDir">%kernel.project_dir%</bind>
1010
<bind key="$entityDefinitions">%frosh_development_helper.names%</bind>
1111
<bind key="$pluginInfos">%kernel.plugin_infos%</bind>
12+
<bind key="$twigExcludeKeywords">%frosh_development_helper.twig.exclude_keywords%</bind>
1213
</defaults>
1314
<prototype namespace="Frosh\DevelopmentHelper\" resource="../../*" exclude="../../{Component/Profiler,Patches}"/>
1415
<defaults>

0 commit comments

Comments
 (0)