AddTemplatesCompilerPass.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. /*
  17. *
  18. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  19. */
  20. class AddTemplatesCompilerPass implements CompilerPassInterface
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function process(ContainerBuilder $container)
  26. {
  27. $settings = $this->fixSettings($container);
  28. foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
  29. if (!isset($attributes[0]['manager_type']) || $attributes[0]['manager_type'] != 'orm') {
  30. continue;
  31. }
  32. $definition = $container->getDefinition($id);
  33. if (!$definition->hasMethodCall('setFormTheme')) {
  34. $definition->addMethodCall('setFormTheme', array($settings['templates']['form']));
  35. }
  36. if (!$definition->hasMethodCall('setFilterTheme')) {
  37. $definition->addMethodCall('setFilterTheme', array($settings['templates']['filter']));
  38. }
  39. }
  40. }
  41. /**
  42. * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
  43. *
  44. * @return bool
  45. */
  46. public function fixSettings(ContainerBuilder $container)
  47. {
  48. $pool = $container->getDefinition('sonata.admin.manager.orm');
  49. // not very clean but don't know how to do that for now
  50. $settings = false;
  51. $methods = $pool->getMethodCalls();
  52. foreach ($methods as $pos => $calls) {
  53. if ($calls[0] == '__hack_doctrine_orm__') {
  54. $settings = $calls[1];
  55. break;
  56. }
  57. }
  58. if ($settings) {
  59. unset($methods[$pos]);
  60. }
  61. $pool->setMethodCalls($methods);
  62. return $settings;
  63. }
  64. }