ControllerGenerator.php 2.1 KB

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