Spaces:
No application file
No application file
File size: 5,166 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
<?php
declare(strict_types=1);
namespace Mautic\CoreBundle\DependencyInjection\Compiler;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class SystemThemeTemplatePathPass implements CompilerPassInterface
{
/**
* Processes the container to update Twig template paths.
*
* This method updates the Twig template paths to pick up templates from the "<application_dir>/themes/system" directory.
* It retrieves the definition of the Twig filesystem loader service and modifies it to include the system theme directory.
* Additionally, it iterates through bundle template paths and adds them to the Twig filesystem loader.
*/
public function process(ContainerBuilder $container): void
{
if ($container->hasDefinition('twig.loader.native_filesystem')) {
// Retrieve the definition of the Twig filesystem loader service.
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.native_filesystem');
// Get the application directory from parameters
$applicationDir = $container->getParameter('mautic.application_dir');
// Define the system theme directory
$systemThemeDir = $applicationDir.DIRECTORY_SEPARATOR.'themes/system';
// If the system theme directory exists, we are registering paths.
if (file_exists($systemThemeDir)) {
// Remove paths registered in TwigExtension for re-registration.
$twigFilesystemLoaderDefinition->removeMethodCall('addPath');
// Re-register user-configured paths.
$paths = $container->getDefinition('twig.template_iterator')->getArgument(1);
foreach ($paths as $path => $namespace) {
if (!$namespace) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path]);
} else {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, $namespace]);
}
}
// Re-register twig default_path.
$defaultTwigPath = $container->getParameterBag()->get('twig.default_path');
if (file_exists($defaultTwigPath)) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
}
$container->addResource(new FileExistenceResource($defaultTwigPath));
// Re-register bundle paths adding `themes/system` path.
foreach ($this->getBundleTemplatePaths($container) as $name => $paths) {
$namespace = $this->normalizeBundleName($name);
foreach ($paths as $path) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, $namespace]);
}
if ($paths) {
// the last path must be the bundle views directory
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, '!'.$namespace]);
}
}
}
}
}
/**
* Get the bundle template paths for registration.
*
* @return array<string, array<int, string>>
*/
private function getBundleTemplatePaths(ContainerBuilder $container): array
{
$bundleHierarchy = [];
$applicationDir = $container->getParameterBag()->get('mautic.application_dir');
$systemThemeDir = $applicationDir.DIRECTORY_SEPARATOR.'themes/system';
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
// Default override bundle path.
$defaultOverrideBundlePath = $container->getParameterBag()->get('twig.default_path').'/bundles/'.$name;
if (file_exists($defaultOverrideBundlePath)) {
$bundleHierarchy[$name][] = $defaultOverrideBundlePath;
}
$container->addResource(new FileExistenceResource($defaultOverrideBundlePath));
// The `themes/system` override a path for bundle.
$bundleName = pathinfo($bundle['path'])['filename'];
$themeOverrideBundlePath = $systemThemeDir.DIRECTORY_SEPARATOR.$bundleName;
if (file_exists($dir = $themeOverrideBundlePath.'/Resources/views')) {
$bundleHierarchy[$name][] = $dir;
$container->addResource(new FileExistenceResource($dir));
}
if (file_exists($dir = $bundle['path'].'/Resources/views') || file_exists($dir = $bundle['path'].'/templates')) {
$bundleHierarchy[$name][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));
}
return $bundleHierarchy;
}
private function normalizeBundleName(string $name): string
{
if (str_ends_with($name, 'Bundle')) {
$name = substr($name, 0, -6);
}
return $name;
}
}
|