Skip to content

Commit

Permalink
feat: add symfony config for block names
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed Nov 15, 2022
1 parent 0ffeb45 commit 87293f9
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 19 deletions.
26 changes: 26 additions & 0 deletions src/Component/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Frosh\DevelopmentHelper\Component\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('frosh_development_helper');

$rootNode = $treeBuilder->getRootNode();
$rootNode
->children()
->arrayNode('twig')
->children()
->arrayNode('exclude_keywords')
->scalarPrototype()
->end()
;

return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Frosh\DevelopmentHelper\Component\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class FroshDevelopmentHelperExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
$this->addConfig($container, $this->getAlias(), $config);
}

public function getConfiguration(array $config, ContainerBuilder $container): Configuration
{
return new Configuration();
}

private function addConfig(ContainerBuilder $container, string $alias, array $options): void
{
foreach ($options as $key => $option) {
$container->setParameter($alias . '.' . $key, $option);

if (\is_array($option)) {
$this->addConfig($container, $alias . '.' . $key, $option);
}
}
}
}
7 changes: 5 additions & 2 deletions src/Component/Twig/Extension/BlockCommentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ class BlockCommentExtension extends AbstractExtension
*/
private $kernelRootDir;

public function __construct(string $kernelRootDir)
private array $twigExcludeKeywords;

public function __construct(string $kernelRootDir, array $twigExcludeKeywords)
{
$this->kernelRootDir = $kernelRootDir;
$this->twigExcludeKeywords = $twigExcludeKeywords;
}

public function getNodeVisitors()
{
return [new BlogCommentNodeVisitor($this->kernelRootDir)];
return [new BlogCommentNodeVisitor($this->kernelRootDir, $this->twigExcludeKeywords)];
}
}
21 changes: 5 additions & 16 deletions src/Component/Twig/NodeVisitor/BlogCommentNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Node\BlockNode;
use Twig\Node\BodyNode;
use Twig\Node\ModuleNode;
Expand All @@ -15,27 +14,17 @@

class BlogCommentNodeVisitor extends AbstractNodeVisitor
{

/** Parts of block names which need to be skipped, for example block that appear
 * inside a HTML attribute and would corrupt rendering */
private const SKIP_BLOCK_KEYWORDS = [
'form_action',
'class',
'attribute',
'title',
'sitemap',
'head_meta_tags',
'page_checkout_additional',
];

/**
* @var string
*/
private $kernelRootDir;

public function __construct(string $kernelRootDir)
private array $twigExcludeKeywords;

public function __construct(string $kernelRootDir, array $twigExcludeKeywords)
{
$this->kernelRootDir = $kernelRootDir;
$this->twigExcludeKeywords = $twigExcludeKeywords;
}


Expand Down Expand Up @@ -110,7 +99,7 @@ private function shouldSkip(Node $node): bool
return true;
}

foreach (self::SKIP_BLOCK_KEYWORDS as $key) {
foreach ($this->twigExcludeKeywords as $key) {
if (strpos($name, $key) !== false) {
return true;
}
Expand Down
32 changes: 31 additions & 1 deletion src/FroshDevelopmentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Frosh\DevelopmentHelper;

use Composer\Autoload\ClassLoader;
use Frosh\DevelopmentHelper\Component\DependencyInjection\BuildEntityDefinitionNamesCompilerPass;
use Frosh\DevelopmentHelper\Component\DependencyInjection\CustomProfilerExtensions;
use Frosh\DevelopmentHelper\Component\DependencyInjection\DisableTwigCacheCompilerPass;
use Frosh\DevelopmentHelper\Component\DependencyInjection\FroshDevelopmentHelperExtension;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Kernel;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
Expand All @@ -20,6 +26,30 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new DisableTwigCacheCompilerPass());
$container->addCompilerPass(new CustomProfilerExtensions());
$container->addCompilerPass(new BuildEntityDefinitionNamesCompilerPass());

$this->buildConfig($container);

parent::build($container);
}

public function createContainerExtension(): FroshDevelopmentHelperExtension
{
return new FroshDevelopmentHelperExtension();
}

private function buildConfig(ContainerBuilder $container): void
{
$locator = new FileLocator('Resources/config');

$resolver = new LoaderResolver([
new YamlFileLoader($container, $locator),
new GlobFileLoader($container, $locator),
]);

$configLoader = new DelegatingLoader($resolver);

$confDir = $this->getPath() . '/Resources/config';

$configLoader->load($confDir . '/{packages}/*' . Kernel::CONFIG_EXTS, 'glob');
}
}
10 changes: 10 additions & 0 deletions src/Resources/config/packages/frosh_development_helper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
frosh_development_helper:
twig:
exclude_keywords:
- form_action
- class
- attribute
- title
- sitemap
- head_meta_tags
- page_checkout_additional
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<bind key="$kernelRootDir">%kernel.project_dir%</bind>
<bind key="$entityDefinitions">%frosh_development_helper.names%</bind>
<bind key="$pluginInfos">%kernel.plugin_infos%</bind>
<bind key="$twigExcludeKeywords">%frosh_development_helper.twig.exclude_keywords%</bind>
</defaults>
<prototype namespace="Frosh\DevelopmentHelper\" resource="../../*" exclude="../../{Component/Profiler,Patches}"/>
<defaults>
Expand Down

0 comments on commit 87293f9

Please sign in to comment.