From 003fd4c536fecc37825b50c1450a428695980cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9C=C3=A9?= Date: Thu, 28 Dec 2017 21:51:20 +0100 Subject: [PATCH] Sf4 compat & refactor twig path override * Allow Symfony 4 * Overrides Twig path for EasyAdmin namespace with a CompilerPass --- composer.json | 56 +++++++++---------- .../Compiler/TwigPathPass.php | 29 ++++++++++ src/EasyAdminExtensionBundle.php | 8 +-- src/Resources/views/default/list.html.twig | 2 +- .../views/form/bootstrap_3_layout.html.twig | 2 +- 5 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 src/DependencyInjection/Compiler/TwigPathPass.php diff --git a/composer.json b/composer.json index 1bd76ef..281a571 100644 --- a/composer.json +++ b/composer.json @@ -18,40 +18,40 @@ "doctrine/doctrine-bundle" : "~1.2", "doctrine/orm" : "~2.3", "pagerfanta/pagerfanta" : "~1.0,>=1.0.1", - "sensio/framework-extra-bundle" : "~3.0,>=3.0.2", - "symfony/asset" : "~3.0", - "symfony/config" : "~3.0", - "symfony/dependency-injection" : "~3.0", - "symfony/doctrine-bridge" : "~3.0", - "symfony/event-dispatcher" : "~3.0", - "symfony/form" : "~3.0", - "symfony/framework-bundle" : "~3.0", - "symfony/http-foundation" : "~3.0", - "symfony/http-kernel" : "~3.0", - "symfony/polyfill-mbstring" : "^1.0", - "symfony/property-access" : "~3.0", - "symfony/security-bundle" : "~3.0", - "symfony/translation" : "~3.0", - "symfony/twig-bridge" : "~3.0", - "symfony/twig-bundle" : "~3.0", - "symfony/validator" : "~3.0", + "sensio/framework-extra-bundle" : "~2.3|~3.0,>=3.0.2|4.0.x|~5.0", + "symfony/asset" : "~3.0|^4.0", + "symfony/config" : "~3.0|^4.0", + "symfony/dependency-injection" : "~3.0|^4.0", + "symfony/doctrine-bridge" : "~3.0|^4.0", + "symfony/event-dispatcher" : "~3.0|^4.0", + "symfony/form" : "~3.0|^4.0", + "symfony/framework-bundle" : "~3.0|^4.0", + "symfony/http-foundation" : "~3.0|^4.0", + "symfony/http-kernel" : "~3.0|^4.0", + "symfony/polyfill-mbstring" : "^1.0|^4.0", + "symfony/property-access" : "~3.0|^4.0", + "symfony/security-bundle" : "~3.0|^4.0", + "symfony/translation" : "~3.0|^4.0", + "symfony/twig-bridge" : "~3.0|^4.0", + "symfony/twig-bundle" : "~3.0|^4.0", + "symfony/validator" : "~3.0|^4.0", "twig/extensions" : "~1.0", - "twig/twig" : "~1.14,>=1.14.2|~2.0", + "twig/twig" : "~1.26|~2.0", "javiereguiluz/easyadmin-bundle": "^1.16" }, "require-dev": { "doctrine/doctrine-fixtures-bundle" : "~2.2", "phpunit/phpunit" : "~5", "psr/log" : "~1.0", - "symfony/browser-kit" : "~3.0", - "symfony/console" : "~3.0", - "symfony/css-selector" : "~3.0", - "symfony/dom-crawler" : "~3.0", - "symfony/finder" : "~3.0", - "symfony/phpunit-bridge" : "~3.0", - "symfony/templating" : "~3.0", - "symfony/var-dumper" : "~3.0", - "symfony/yaml" : "~3.0" + "symfony/browser-kit" : "~3.0|^4.0", + "symfony/console" : "~3.0|^4.0", + "symfony/css-selector" : "~3.0|^4.0", + "symfony/dom-crawler" : "~3.0|^4.0", + "symfony/finder" : "~3.0|^4.0", + "symfony/phpunit-bridge" : "~3.0|^4.0", + "symfony/templating" : "~3.0|^4.0", + "symfony/var-dumper" : "~3.0|^4.0", + "symfony/yaml" : "~3.0|^4.0" }, "config": { "sort-packages": true @@ -68,7 +68,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.1.x-dev" + "dev-master": "1.2.x-dev" } } } diff --git a/src/DependencyInjection/Compiler/TwigPathPass.php b/src/DependencyInjection/Compiler/TwigPathPass.php new file mode 100644 index 0000000..defd15e --- /dev/null +++ b/src/DependencyInjection/Compiler/TwigPathPass.php @@ -0,0 +1,29 @@ +getAlias('twig.loader')->__toString(); + $twigLoaderFilesystemDefinition = $container->getDefinition($twigLoaderFilesystemId); + + // Replaces native EasyAdmin templates + $easyAdminExtensionBundleRefl = new \ReflectionClass(EasyAdminExtensionBundle::class); + $easyAdminExtensionBundlePath = dirname($easyAdminExtensionBundleRefl->getFilename()); + $easyAdminExtensionTwigPath = $easyAdminExtensionBundlePath.'/Resources/views'; + $twigLoaderFilesystemDefinition->addMethodCall('prependPath', array($easyAdminExtensionTwigPath, 'EasyAdmin')); + + $nativeEasyAdminBundleRefl = new \ReflectionClass(EasyAdminBundle::class); + $nativeEasyAdminBundlePath = dirname($nativeEasyAdminBundleRefl->getFilename()); + $nativeEasyAdminTwigPath = $nativeEasyAdminBundlePath.'/Resources/views'; + // Defines a namespace from native EasyAdmin templates + $twigLoaderFilesystemDefinition->addMethodCall('addPath', array($nativeEasyAdminTwigPath, 'BaseEasyAdmin')); + } +} diff --git a/src/EasyAdminExtensionBundle.php b/src/EasyAdminExtensionBundle.php index 3c2d6c1..ab62901 100644 --- a/src/EasyAdminExtensionBundle.php +++ b/src/EasyAdminExtensionBundle.php @@ -2,6 +2,8 @@ namespace AlterPHP\EasyAdminExtensionBundle; +use AlterPHP\EasyAdminExtensionBundle\DependencyInjection\Compiler\TwigPathPass; +use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -11,10 +13,6 @@ public function build(ContainerBuilder $container) { parent::build($container); - // Prepend EasyAdminExtension bundle tmeplates to EasyAdmin namespace - $container->prependExtensionConfig( - 'twig', - array('paths' => array(__DIR__.'/Resources/views' => 'EasyAdmin')) - ); + $container->addCompilerPass(new TwigPathPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION); } } diff --git a/src/Resources/views/default/list.html.twig b/src/Resources/views/default/list.html.twig index c551fb8..cf89d4e 100644 --- a/src/Resources/views/default/list.html.twig +++ b/src/Resources/views/default/list.html.twig @@ -1,4 +1,4 @@ -{% extends 'EasyAdminBundle:default:list.html.twig' %} +{% extends '@BaseEasyAdmin/default/list.html.twig' %} {% set requestFilters = app.request.get('filters', {}) %} {% set _request_parameters = _request_parameters|default({})|merge({ diff --git a/src/Resources/views/form/bootstrap_3_layout.html.twig b/src/Resources/views/form/bootstrap_3_layout.html.twig index 5d2eceb..32b5624 100644 --- a/src/Resources/views/form/bootstrap_3_layout.html.twig +++ b/src/Resources/views/form/bootstrap_3_layout.html.twig @@ -1,4 +1,4 @@ -{% use 'EasyAdminBundle:form:bootstrap_3_layout.html.twig' %} +{% use '@BaseEasyAdmin/form/bootstrap_3_layout.html.twig' %} {% block easyadmin_embedded_list_row %} {{ render(path('easyadmin', { entity: entity, action: 'embeddedList', filters: filters })) }}