ControllerGenerator.php 2.1 KB

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