GenerateAdminCommand.php 15 KB

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