AddTemplatesCompilerPass.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. public function fixSettings($container)
  42. {
  43. $pool = $container->getDefinition('sonata.admin.manager.orm');
  44. // not very clean but don't know how to do that for now
  45. $settings = false;
  46. $methods = $pool->getMethodCalls();
  47. foreach ($methods as $pos => $calls) {
  48. if ($calls[0] == '__hack_doctrine_orm__') {
  49. $settings = $calls[1];
  50. break;
  51. }
  52. }
  53. if ($settings) {
  54. unset($methods[$pos]);
  55. }
  56. $pool->setMethodCalls($methods);
  57. return $settings;
  58. }
  59. }