AbstractBcGenerator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  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. */
  11. namespace Sonata\AdminBundle\Generator;
  12. use Sensio\Bundle\GeneratorBundle\Generator\Generator;
  13. /**
  14. * Class that fixes backward incompatible changes between Sensio Generator 2.2 and 2.3.
  15. * This class should be removed if support for Symfony 2.2 (and Sensio Generator 2.2) will be dropped.
  16. *
  17. * @author Andrej Hudec <pulzarraider@gmail.com>
  18. */
  19. abstract class AbstractBcGenerator extends Generator
  20. {
  21. /**
  22. * @var array
  23. */
  24. private $skeletonDirs;
  25. /**
  26. * @var boolean
  27. */
  28. private $bcEnabled = false;
  29. /**
  30. * Returns the Generator Version
  31. *
  32. * @return string
  33. */
  34. public static function getGeneratorVersion()
  35. {
  36. $r = new \ReflectionClass('Sensio\Bundle\GeneratorBundle\Generator\Generator');
  37. return $r->hasMethod('setSkeletonDirs') === true ? '2.3' : '2.2';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function setSkeletonDirs($skeletonDirs)
  43. {
  44. $this->skeletonDirs = is_array($skeletonDirs) ? $skeletonDirs : array($skeletonDirs);
  45. $this->bcEnabled = false;
  46. if (self::getGeneratorVersion() === '2.2') {
  47. $this->bcEnabled = true;
  48. }
  49. if (self::getGeneratorVersion() === '2.3') {
  50. parent::setSkeletonDirs($this->skeletonDirs);
  51. }
  52. }
  53. /**
  54. * Set backward compatibility with Sensio Generator 2.2.*
  55. *
  56. * @param boolean $bcEnabled
  57. */
  58. public function setBc($bcEnabled)
  59. {
  60. $this->bcEnabled = $bcEnabled;
  61. }
  62. /**
  63. * @param string $template
  64. * @param array $parameters
  65. *
  66. * @return string
  67. */
  68. protected function renderBc($template, $parameters)
  69. {
  70. if ($this->bcEnabled) {
  71. // Sensio Generator 2.2
  72. return $this->render($this->skeletonDirs, $template, $parameters);
  73. }
  74. // Sensio Generator >=2.3
  75. return $this->render($template, $parameters);
  76. }
  77. /**
  78. * @param string $template
  79. * @param string $target
  80. * @param array $parameters
  81. *
  82. * @return int
  83. */
  84. protected function renderFileBc($template, $target, $parameters)
  85. {
  86. if ($this->bcEnabled) {
  87. // Sensio Generator 2.2
  88. return $this->renderFile($this->skeletonDirs, $template, $target, $parameters);
  89. }
  90. // Sensio Generator >=2.3
  91. return $this->renderFile($template, $target, $parameters);
  92. }
  93. }