ControllerGenerator.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  14. /**
  15. * @author Marek Stipek <mario.dweller@seznam.cz>
  16. * @author Simon Cosandey <simon.cosandey@simseo.ch>
  17. */
  18. class ControllerGenerator extends Generator
  19. {
  20. /** @var string|null */
  21. private $class;
  22. /** @var string|null */
  23. private $file;
  24. /**
  25. * @param array|string $skeletonDirectory
  26. */
  27. public function __construct($skeletonDirectory)
  28. {
  29. $this->setSkeletonDirs($skeletonDirectory);
  30. }
  31. /**
  32. * @param BundleInterface $bundle
  33. * @param string $controllerClassBasename
  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. }