ControllerGenerator.php 2.1 KB

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