ControllerGenerator.php 2.0 KB

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