Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/104 #115

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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