GenerateAdminCommand.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. namespace Sonata\AdminBundle\Command;
  11. use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
  12. use Sonata\AdminBundle\Generator\AdminGenerator;
  13. use Sonata\AdminBundle\Generator\ControllerGenerator;
  14. use Sonata\AdminBundle\Manipulator\ServicesManipulator;
  15. use Sonata\AdminBundle\Model\ModelManagerInterface;
  16. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  17. use Symfony\Bundle\FrameworkBundle\Console\Application;
  18. use Symfony\Component\Console\Input\InputArgument;
  19. use Symfony\Component\Console\Input\InputInterface;
  20. use Symfony\Component\Console\Input\InputOption;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Symfony\Component\DependencyInjection\Container;
  23. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  24. use Symfony\Component\HttpKernel\KernelInterface;
  25. /**
  26. * @author Marek Stipek <mario.dweller@seznam.cz>
  27. * @author Simon Cosandey <simon.cosandey@simseo.ch>
  28. */
  29. class GenerateAdminCommand extends ContainerAwareCommand
  30. {
  31. /** @var string[] */
  32. private $managerTypes;
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function configure()
  37. {
  38. $this
  39. ->setName('sonata:admin:generate')
  40. ->setDescription('Generates an admin class based on the given model class')
  41. ->addArgument('model', InputArgument::REQUIRED, 'The fully qualified model class')
  42. ->addOption('bundle', 'b', InputOption::VALUE_OPTIONAL, 'The bundle name')
  43. ->addOption('admin', 'a', InputOption::VALUE_OPTIONAL, 'The admin class basename')
  44. ->addOption('controller', 'c', InputOption::VALUE_OPTIONAL, 'The controller class basename')
  45. ->addOption('manager', 'm', InputOption::VALUE_OPTIONAL, 'The model manager type')
  46. ->addOption('services', 'y', InputOption::VALUE_OPTIONAL, 'The services YAML file', 'services.yml')
  47. ->addOption('id', 'i', InputOption::VALUE_OPTIONAL, 'The admin service ID')
  48. ;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. protected function execute(InputInterface $input, OutputInterface $output)
  54. {
  55. $modelClass = Validators::validateClass($input->getArgument('model'));
  56. $modelClassBasename = current(array_slice(explode('\\', $modelClass), -1));
  57. $bundle = $this->getBundle($input->getOption('bundle') ?: $this->getBundleNameFromClass($modelClass));
  58. $adminClassBasename = $input->getOption('admin') ?: $modelClassBasename . 'Admin';
  59. $adminClassBasename = Validators::validateAdminClassBasename($adminClassBasename);
  60. $managerType = $input->getOption('manager') ?: $this->getDefaultManagerType();
  61. $modelManager = $this->getModelManager($managerType);
  62. $skeletonDirectory = __DIR__ . '/../Resources/skeleton';
  63. $adminGenerator = new AdminGenerator($modelManager, $skeletonDirectory);
  64. try {
  65. $adminGenerator->generate($bundle, $adminClassBasename, $modelClass);
  66. $output->writeln(sprintf(
  67. '%sThe admin class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
  68. PHP_EOL,
  69. $adminGenerator->getClass(),
  70. realpath($adminGenerator->getFile())
  71. ));
  72. } catch (\Exception $e) {
  73. $this->writeError($output, $e->getMessage());
  74. }
  75. if ($controllerClassBasename = $input->getOption('controller')) {
  76. $controllerClassBasename = Validators::validateControllerClassBasename($controllerClassBasename);
  77. $controllerGenerator = new ControllerGenerator($skeletonDirectory);
  78. try {
  79. $controllerGenerator->generate($bundle, $controllerClassBasename);
  80. $output->writeln(sprintf(
  81. '%sThe controller class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
  82. PHP_EOL,
  83. $controllerGenerator->getClass(),
  84. realpath($controllerGenerator->getFile())
  85. ));
  86. } catch (\Exception $e) {
  87. $this->writeError($output, $e->getMessage());
  88. }
  89. }
  90. if ($servicesFile = $input->getOption('services')) {
  91. $adminClass = $adminGenerator->getClass();
  92. $file = sprintf('%s/Resources/config/%s', $bundle->getPath(), $servicesFile);
  93. $servicesManipulator = new ServicesManipulator($file);
  94. $controllerName = $controllerClassBasename
  95. ? sprintf('%s:%s', $bundle->getName(), substr($controllerClassBasename, 0, -10))
  96. : 'SonataAdminBundle:CRUD'
  97. ;
  98. try {
  99. $id = $input->getOption('id') ?: $this->getAdminServiceId($bundle->getName(), $adminClassBasename);
  100. $servicesManipulator->addResource($id, $modelClass, $adminClass, $controllerName, $managerType);
  101. $output->writeln(sprintf(
  102. '%sThe service "<info>%s</info>" has been appended to the file <info>"%s</info>".',
  103. PHP_EOL,
  104. $id,
  105. realpath($file)
  106. ));
  107. } catch (\Exception $e) {
  108. $this->writeError($output, $e->getMessage());
  109. }
  110. }
  111. return 0;
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. protected function interact(InputInterface $input, OutputInterface $output)
  117. {
  118. $dialog = $this->getDialogHelper();
  119. $dialog->writeSection($output, 'Welcome to the Sonata admin generator');
  120. $modelClass = $this->askAndValidate(
  121. $output,
  122. 'The fully qualified model class',
  123. $input->getArgument('model'),
  124. 'Sonata\AdminBundle\Command\Validators::validateClass'
  125. );
  126. $modelClassBasename = current(array_slice(explode('\\', $modelClass), -1));
  127. $bundleName = $this->askAndValidate(
  128. $output,
  129. 'The bundle name',
  130. $input->getOption('bundle') ?: $this->getBundleNameFromClass($modelClass),
  131. 'Sensio\Bundle\GeneratorBundle\Command\Validators::validateBundleName'
  132. );
  133. $adminClassBasename = $this->askAndValidate(
  134. $output,
  135. 'The admin class basename',
  136. $input->getOption('admin') ?: $modelClassBasename . 'Admin',
  137. 'Sonata\AdminBundle\Command\Validators::validateAdminClassBasename'
  138. );
  139. if (count($this->getAvailableManagerTypes()) > 1) {
  140. $managerType = $this->askAndValidate(
  141. $output,
  142. 'The manager type',
  143. $input->getOption('manager') ?: $this->getDefaultManagerType(),
  144. array($this, 'validateManagerType')
  145. );
  146. $input->setOption('manager', $managerType);
  147. }
  148. $question = $dialog->getQuestion('Do you want to generate a controller', 'no', '?');
  149. if ($dialog->askConfirmation($output, $question, false)) {
  150. $controllerClassBasename = $this->askAndValidate(
  151. $output,
  152. 'The controller class basename',
  153. $input->getOption('controller') ?: $modelClassBasename . 'AdminController',
  154. 'Sonata\AdminBundle\Command\Validators::validateControllerClassBasename'
  155. );
  156. $input->setOption('controller', $controllerClassBasename);
  157. }
  158. $question = $dialog->getQuestion('Do you want to update the services YAML configuration file', 'yes', '?');
  159. if ($dialog->askConfirmation($output, $question)) {
  160. $path = $this->getBundle($bundleName)->getPath() . '/Resources/config/';
  161. $servicesFile = $this->askAndValidate(
  162. $output,
  163. 'The services YAML configuration file',
  164. is_file($path . 'admin.yml') ? 'admin.yml' : 'services.yml',
  165. 'Sonata\AdminBundle\Command\Validators::validateServicesFile'
  166. );
  167. $id = $this->askAndValidate(
  168. $output,
  169. 'The admin service ID',
  170. $this->getAdminServiceId($bundleName, $adminClassBasename),
  171. 'Sonata\AdminBundle\Command\Validators::validateServiceId'
  172. );
  173. $input->setOption('services', $servicesFile);
  174. $input->setOption('id', $id);
  175. }
  176. $input->setArgument('model', $modelClass);
  177. $input->setOption('admin', $adminClassBasename);
  178. $input->setOption('bundle', $bundleName);
  179. }
  180. /**
  181. * @param string $managerType
  182. * @return string
  183. * @throws \InvalidArgumentException
  184. */
  185. public function validateManagerType($managerType)
  186. {
  187. $managerTypes = $this->getAvailableManagerTypes();
  188. if (!isset($managerTypes[$managerType])) {
  189. throw new \InvalidArgumentException(sprintf(
  190. 'Invalid manager type "%s". Available manager types are "%s".',
  191. $managerType,
  192. implode('", "', $managerTypes)
  193. ));
  194. }
  195. return $managerType;
  196. }
  197. /**
  198. * @param string $class
  199. * @return string|null
  200. * @throws \InvalidArgumentException
  201. */
  202. private function getBundleNameFromClass($class)
  203. {
  204. $application = $this->getApplication();
  205. /* @var $application Application */
  206. foreach ($application->getKernel()->getBundles() as $bundle) {
  207. if (strpos($class, $bundle->getNamespace() . '\\') === 0) {
  208. return $bundle->getName();
  209. };
  210. }
  211. return null;
  212. }
  213. /**
  214. * @param string $name
  215. * @return BundleInterface
  216. */
  217. private function getBundle($name)
  218. {
  219. return $this->getKernel()->getBundle($name);
  220. }
  221. /**
  222. * @param OutputInterface $output
  223. * @param string $message
  224. */
  225. private function writeError(OutputInterface $output, $message)
  226. {
  227. $output->writeln(sprintf("\n<error>%s</error>", $message));
  228. }
  229. /**
  230. * @param OutputInterface $output
  231. * @param string $question
  232. * @param mixed $default
  233. * @param callable $validator
  234. * @return mixed
  235. */
  236. private function askAndValidate(OutputInterface $output, $question, $default, $validator)
  237. {
  238. $dialog = $this->getDialogHelper();
  239. return $dialog->askAndValidate($output, $dialog->getQuestion($question, $default), $validator, false, $default);
  240. }
  241. /**
  242. * @return string
  243. * @throws \RuntimeException
  244. */
  245. private function getDefaultManagerType()
  246. {
  247. if (!$managerTypes = $this->getAvailableManagerTypes()) {
  248. throw new \RuntimeException('There are no model managers registered.');
  249. }
  250. return current($managerTypes);
  251. }
  252. /**
  253. * @param string $managerType
  254. * @return ModelManagerInterface
  255. */
  256. private function getModelManager($managerType)
  257. {
  258. return $this->getContainer()->get('sonata.admin.manager.' . $managerType);
  259. }
  260. /**
  261. * @param string $bundleName
  262. * @param string $adminClassBasename
  263. * @return string
  264. */
  265. private function getAdminServiceId($bundleName, $adminClassBasename)
  266. {
  267. $prefix = substr($bundleName, -6) == 'Bundle' ? substr($bundleName, 0, -6) : $bundleName;
  268. $suffix = substr($adminClassBasename, -5) == 'Admin' ? substr($adminClassBasename, 0, -5) : $adminClassBasename;
  269. $suffix = str_replace('\\', '.', $suffix);
  270. return Container::underscore(sprintf(
  271. '%s.admin.%s',
  272. $prefix,
  273. $suffix
  274. ));
  275. }
  276. /**
  277. * @return string[]
  278. */
  279. private function getAvailableManagerTypes()
  280. {
  281. $container = $this->getContainer();
  282. if (!$container instanceof Container) {
  283. return array();
  284. }
  285. if ($this->managerTypes === null) {
  286. $this->managerTypes = array();
  287. foreach ($container->getServiceIds() as $id) {
  288. if (strpos($id, 'sonata.admin.manager.') === 0) {
  289. $managerType = substr($id, 21);
  290. $this->managerTypes[$managerType] = $managerType;
  291. }
  292. }
  293. }
  294. return $this->managerTypes;
  295. }
  296. /**
  297. * @return KernelInterface
  298. */
  299. private function getKernel()
  300. {
  301. $application = $this->getApplication();
  302. /* @var $application Application */
  303. return $application->getKernel();
  304. }
  305. /**
  306. * @return DialogHelper
  307. */
  308. private function getDialogHelper()
  309. {
  310. $dialogHelper = $this->getHelper('dialog');
  311. if (!$dialogHelper instanceof DialogHelper) {
  312. $dialogHelper = new DialogHelper();
  313. $this->getHelperSet()->set($dialogHelper);
  314. }
  315. return $dialogHelper;
  316. }
  317. }