ControllerGenerator.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /**
  22. * @var string|null
  23. */
  24. private $class;
  25. /**
  26. * @var string|null
  27. */
  28. private $file;
  29. /**
  30. * @param array|string $skeletonDirectory
  31. */
  32. public function __construct($skeletonDirectory)
  33. {
  34. $this->setSkeletonDirs($skeletonDirectory);
  35. }
  36. /**
  37. * @param BundleInterface $bundle
  38. * @param string $controllerClassBasename
  39. *
  40. * @throws \RuntimeException
  41. */
  42. public function generate(BundleInterface $bundle, $controllerClassBasename)
  43. {
  44. $this->class = sprintf('%s\Controller\%s', $bundle->getNamespace(), $controllerClassBasename);
  45. $this->file = sprintf(
  46. '%s/Controller/%s.php',
  47. $bundle->getPath(),
  48. str_replace('\\', '/', $controllerClassBasename)
  49. );
  50. $parts = explode('\\', $this->class);
  51. if (file_exists($this->file)) {
  52. throw new \RuntimeException(sprintf(
  53. 'Unable to generate the admin controller class "%s". The file "%s" already exists.',
  54. $this->class,
  55. realpath($this->file)
  56. ));
  57. }
  58. $this->renderFile('AdminController.php.twig', $this->file, array(
  59. 'classBasename' => array_pop($parts),
  60. 'namespace' => implode('\\', $parts),
  61. ));
  62. }
  63. /**
  64. * @return string|null
  65. */
  66. public function getClass()
  67. {
  68. return $this->class;
  69. }
  70. /**
  71. * @return string|null
  72. */
  73. public function getFile()
  74. {
  75. return $this->file;
  76. }
  77. }