AbstractBcGenerator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * {@inheritdoc}
  31. */
  32. public function setSkeletonDirs($skeletonDirs)
  33. {
  34. $this->skeletonDirs = is_array($skeletonDirs) ? $skeletonDirs : array($skeletonDirs);
  35. $this->bcEnabled = false;
  36. if (method_exists(get_parent_class(get_parent_class($this)), 'setSkeletonDirs')) {
  37. //Sensio Generator >=2.3
  38. parent::setSkeletonDirs($skeletonDirs);
  39. } else {
  40. //Sensio Generator 2.2
  41. $this->bcEnabled = true;
  42. }
  43. }
  44. /**
  45. * Set backward compatibility with Sensio Generator 2.2.*
  46. *
  47. * @param boolean $bcEnabled
  48. */
  49. public function setBc($bcEnabled)
  50. {
  51. $this->bcEnabled = $bcEnabled;
  52. }
  53. protected function renderBc($template, $parameters)
  54. {
  55. if ($this->bcEnabled) {
  56. //Sensio Generator 2.2
  57. return $this->render($this->skeletonDirs, $template, $parameters);
  58. } else {
  59. //Sensio Generator >=2.3
  60. return $this->render($template, $parameters);
  61. }
  62. }
  63. protected function renderFileBc($template, $target, $parameters)
  64. {
  65. if ($this->bcEnabled) {
  66. //Sensio Generator 2.2
  67. return $this->renderFile($this->skeletonDirs, $template, $target, $parameters);
  68. } else {
  69. //Sensio Generator >=2.3
  70. return $this->renderFile($template, $target, $parameters);
  71. }
  72. }
  73. }