ControllerGenerator.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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. namespace Sonata\AdminBundle\Generator;
  11. use Sensio\Bundle\GeneratorBundle\Generator\Generator;
  12. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  13. /**
  14. * Class ControllerGenerator.
  15. *
  16. * @author Marek Stipek <mario.dweller@seznam.cz>
  17. * @author Simon Cosandey <simon.cosandey@simseo.ch>
  18. */
  19. class ControllerGenerator extends Generator
  20. {
  21. /** @var string|null */
  22. private $class;
  23. /** @var string|null */
  24. private $file;
  25. /**
  26. * @param array|string $skeletonDirectory
  27. */
  28. public function __construct($skeletonDirectory)
  29. {
  30. $this->setSkeletonDirs($skeletonDirectory);
  31. }
  32. /**
  33. * @param BundleInterface $bundle
  34. * @param string $controllerClassBasename
  35. *
  36. * @throws \RuntimeException
  37. */
  38. public function generate(BundleInterface $bundle, $controllerClassBasename)
  39. {
  40. $this->class = sprintf('%s\Controller\%s', $bundle->getNamespace(), $controllerClassBasename);
  41. $this->file = sprintf(
  42. '%s/Controller/%s.php',
  43. $bundle->getPath(),
  44. str_replace('\\', '/', $controllerClassBasename)
  45. );
  46. $parts = explode('\\', $this->class);
  47. if (file_exists($this->file)) {
  48. throw new \RuntimeException(sprintf(
  49. 'Unable to generate the admin controller class "%s". The file "%s" already exists.',
  50. $this->class,
  51. realpath($this->file)
  52. ));
  53. }
  54. $this->renderFile('AdminController.php.twig', $this->file, array(
  55. 'classBasename' => array_pop($parts),
  56. 'namespace' => implode('\\', $parts),
  57. ));
  58. }
  59. /**
  60. * @return string|null
  61. */
  62. public function getClass()
  63. {
  64. return $this->class;
  65. }
  66. /**
  67. * @return string|null
  68. */
  69. public function getFile()
  70. {
  71. return $this->file;
  72. }
  73. }