ControllerGenerator.php 2.1 KB

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