generated from headsnet/symfony-bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #3
- Loading branch information
Showing
7 changed files
with
220 additions
and
24 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
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,76 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\SymfonyToolsBundle\Twig; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
/** | ||
* Locate all "tpl" directories inside the $baseDir directory, and | ||
* add them to the Twig configuration as namespaced paths. | ||
* | ||
* A template such as "src/UI/Feature/Foo/Bar/tpl/test.twig.html" will | ||
* be added with namespace "Foo->Bar", and therefore can be referenced | ||
* using "@Foo->Bar/test.twig.html". | ||
*/ | ||
final class TemplateDirectoryCompilerPass implements CompilerPassInterface | ||
{ | ||
#[\Override] | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$baseDir = $this->getBundleParam($container, 'headsnet_symfony_tools.twig.import_feature_dirs.base_dir'); | ||
|
||
if (!strlen($baseDir)) { | ||
return; | ||
} | ||
|
||
if ($container->hasDefinition('twig.loader.native_filesystem')) { | ||
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.native_filesystem'); | ||
/** @var string $projectDir */ | ||
$projectDir = $container->getParameter('kernel.project_dir'); | ||
$separator = $this->getBundleParam($container, 'headsnet_symfony_tools.twig.import_feature_dirs.separator'); | ||
$tplDirName = $this->getBundleParam($container, 'headsnet_symfony_tools.twig.import_feature_dirs.tpl_dir_name'); | ||
$featureDir = sprintf('%s/%s', $projectDir, $baseDir); | ||
|
||
foreach ($this->tplDirPaths($featureDir, $tplDirName) as $file) { | ||
$tplDirToAdd = str_replace($projectDir . '/', '', $file->getPathname()); | ||
|
||
$namespaceToAdd = str_replace( | ||
[rtrim($baseDir, '/') . '/', '/' . trim($tplDirName, '/'), '/'], | ||
['', '', $separator], | ||
$tplDirToAdd | ||
); | ||
|
||
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$tplDirToAdd, $namespaceToAdd]); | ||
} | ||
} | ||
} | ||
|
||
private function tplDirPaths(string $featureDir, string $tplDirName): Finder | ||
{ | ||
$finder = new Finder(); | ||
$finder->directories() | ||
->in($featureDir) | ||
->name($tplDirName) | ||
->sortByName() | ||
; | ||
|
||
return $finder; | ||
} | ||
|
||
/** | ||
* Slightly convoluted method to obtain the bundle configuration, as the parameters | ||
* do not seem to be ready in the container if we access them directly. | ||
*/ | ||
private function getBundleParam(ContainerBuilder $container, string $paramName): string | ||
{ | ||
/** @var string $parameter */ | ||
$parameter = $container->getParameterBag()->resolveValue( | ||
$container->getParameter($paramName) | ||
); | ||
|
||
return $parameter; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
headsnet_symfony_tools: | ||
root_namespace: App | ||
forms: | ||
default_empty_string: true | ||
disable_validation: true | ||
disable_autocomplete: true | ||
default_empty_string: true | ||
disable_validation: true | ||
disable_autocomplete: true | ||
rate_limiting: | ||
use_headers: true | ||
twig: | ||
import_feature_dirs: | ||
base_dir: src/UI/Feature | ||
separator: '->' | ||
tpl_dir_name: tpl | ||
|
Empty file.
Empty file.
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,59 @@ | ||
<?php | ||
|
||
namespace Headsnet\SymfonyToolsBundle\Tests\Twig; | ||
|
||
use Headsnet\SymfonyToolsBundle\Twig\TemplateDirectoryCompilerPass; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; | ||
|
||
#[CoversClass(TemplateDirectoryCompilerPass::class)] | ||
class TemplateDirectoryCompilerPassTest extends TestCase | ||
{ | ||
private TemplateDirectoryCompilerPass $compilerPass; | ||
|
||
private ContainerBuilder $container; | ||
|
||
#[\Override] | ||
protected function setUp(): void | ||
{ | ||
$this->compilerPass = new TemplateDirectoryCompilerPass(); | ||
$this->container = new ContainerBuilder(new ParameterBag([ | ||
'headsnet_symfony_tools.twig.import_feature_dirs.base_dir' => 'tests/Twig/Fixtures/', | ||
'headsnet_symfony_tools.twig.import_feature_dirs.separator' => '->', | ||
'headsnet_symfony_tools.twig.import_feature_dirs.tpl_dir_name' => 'tpl', | ||
])); | ||
$this->container->setParameter('kernel.project_dir', '.'); | ||
|
||
$twigFilesystemLoaderDefinition = new Definition(); | ||
$this->container->setDefinition('twig.loader.native_filesystem', $twigFilesystemLoaderDefinition); | ||
} | ||
|
||
#[Test] | ||
public function process_with_duration_file_paths(): void | ||
{ | ||
$this->compilerPass->process($this->container); | ||
|
||
$definition = $this->container->getDefinition('twig.loader.native_filesystem'); | ||
$calls = $definition->getMethodCalls(); | ||
$this->assertCount(2, $calls); | ||
|
||
$expected = [ | ||
['addPath', ['tests/Twig/Fixtures/FeatureA/tpl', 'FeatureA']], | ||
['addPath', ['tests/Twig/Fixtures/FeatureB/tpl', 'FeatureB']], | ||
]; | ||
|
||
$this->assertSame($expected, $calls); | ||
} | ||
|
||
#[Test] | ||
public function process_with_nonexistent_definition(): void | ||
{ | ||
$this->container->removeDefinition('twig.loader.native_filesystem'); | ||
$this->compilerPass->process($this->container); | ||
$this->assertFalse($this->container->hasDefinition('twig.loader.native_filesystem')); | ||
} | ||
} |