Skip to content

Commit

Permalink
Fix/104 (#115)
Browse files Browse the repository at this point in the history
* Workaround by prepending TwigBundle config

* remove old comment
  • Loading branch information
alterphp authored Apr 4, 2019
1 parent 43ec4cb commit 13cd1f7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
39 changes: 0 additions & 39 deletions src/DependencyInjection/Compiler/TwigPathPass.php

This file was deleted.

45 changes: 44 additions & 1 deletion src/DependencyInjection/EasyAdminExtensionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace AlterPHP\EasyAdminExtensionBundle\DependencyInjection;

use EasyCorp\Bundle\EasyAdminBundle\EasyAdminBundle;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

Expand All @@ -12,7 +14,7 @@
*
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class EasyAdminExtensionExtension extends Extension
class EasyAdminExtensionExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
Expand All @@ -32,4 +34,45 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}

public function prepend(ContainerBuilder $container)
{
$twigConfigs = $container->getExtensionConfig('twig');

$paths = [];
// keeping user-configured paths
foreach ($twigConfigs as $twigConfig) {
if (isset($twigConfig['default_path'])) {
$userDefinedTwigDefaultPath = $twigConfig['default_path'];
}
if (isset($twigConfig['paths'])) {
$paths += $twigConfig['paths'];
}
}

$twigDefaultPathDefaultValue = $container->getParameterBag()->resolveValue('%kernel.project_dir%/templates');
$twigDefaultPath = $userDefinedTwigDefaultPath ?? $twigDefaultPathDefaultValue;

// Waiting this PR or any alternative is implemented by Symfony itself
// @see https://github.com/symfony/symfony/pull/30527
// Put back user default path
$userDefaultPath = $twigDefaultPath.'/bundles/EasyAdminBundle/';
if (file_exists($userDefaultPath)) {
$paths[$userDefaultPath] = 'EasyAdmin';
}

// EasyAdminExtension overrides EasyAdmin templates
$paths[\dirname(__DIR__).'/Resources/views/'] = 'EasyAdmin';

// Creates BaseEasyAdmin namespace to specifically target to original EasyAdmin templates
$nativeEasyAdminBundleRefl = new \ReflectionClass(EasyAdminBundle::class);
if ($nativeEasyAdminBundleRefl->isUserDefined()) {
$nativeEasyAdminBundlePath = \dirname((string) $nativeEasyAdminBundleRefl->getFileName());
$nativeEasyAdminTwigPath = $nativeEasyAdminBundlePath.'/Resources/views';
// Defines a namespace from native EasyAdmin templates
$paths[$nativeEasyAdminTwigPath] = 'BaseEasyAdmin';
}

$container->prependExtensionConfig('twig', ['paths' => $paths]);
}
}
4 changes: 0 additions & 4 deletions src/EasyAdminExtensionBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace AlterPHP\EasyAdminExtensionBundle;

use AlterPHP\EasyAdminExtensionBundle\DependencyInjection\Compiler\TwigPathPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -14,8 +12,6 @@ public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new TwigPathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);

$this->addRegisterMappingsPass($container);
}

Expand Down

0 comments on commit 13cd1f7

Please sign in to comment.