InitApplicationCommand.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Command;
  3. use Symfony\Components\Console\Command\Command as BaseCommand;
  4. use Symfony\Components\Console\Input\InputArgument;
  5. use Symfony\Components\Console\Input\InputOption;
  6. use Symfony\Components\Console\Input\InputInterface;
  7. use Symfony\Components\Console\Output\OutputInterface;
  8. use Symfony\Components\Console\Output\Output;
  9. use Symfony\Bundle\FrameworkBundle\Util\Filesystem;
  10. use Symfony\Bundle\FrameworkBundle\Util\Mustache;
  11. /*
  12. * This file is part of the symfony framework.
  13. *
  14. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  15. *
  16. * This source file is subject to the MIT license that is bundled
  17. * with this source code in the file LICENSE.
  18. */
  19. /**
  20. * Initializes a new application.
  21. *
  22. * @package symfony
  23. * @subpackage console
  24. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  25. */
  26. class InitApplicationCommand extends BaseCommand
  27. {
  28. /**
  29. * @see Command
  30. */
  31. protected function configure()
  32. {
  33. $this
  34. ->setDefinition(array(
  35. new InputArgument('name', InputArgument::REQUIRED, 'The application name (Hello)'),
  36. new InputArgument('path', InputArgument::REQUIRED, 'The path to the application (hello/)'),
  37. new InputArgument('web_path', InputArgument::REQUIRED, 'The path to the public web root (web/)'),
  38. new InputOption('format', '', InputOption::PARAMETER_REQUIRED, 'Use the format for configuration files (php, xml, or yaml)', 'xml'),
  39. ))
  40. ->setName('init:application')
  41. ;
  42. }
  43. /**
  44. * @see Command
  45. */
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. if (file_exists($targetDir = $input->getArgument('path'))) {
  49. throw new \RuntimeException(sprintf('The directory "%s" already exists.', $targetDir));
  50. }
  51. if (!file_exists($webDir = $input->getArgument('web_path'))) {
  52. mkdir($webDir, 0777, true);
  53. }
  54. $parameters = array(
  55. 'class' => $input->getArgument('name'),
  56. 'application' => strtolower($input->getArgument('name')),
  57. 'format' => $input->getOption('format'),
  58. );
  59. $filesystem = new Filesystem();
  60. $filesystem->mirror(__DIR__.'/../Resources/skeleton/application/generic', $targetDir);
  61. $filesystem->mirror(__DIR__.'/../Resources/skeleton/application/'.$input->getOption('format'), $targetDir);
  62. Mustache::renderDir($targetDir, $parameters);
  63. $filesystem->chmod($targetDir.'/console', 0777);
  64. $filesystem->chmod($targetDir.'/logs', 0777);
  65. $filesystem->chmod($targetDir.'/cache', 0777);
  66. $filesystem->rename($targetDir.'/Kernel.php', $targetDir.'/'.$input->getArgument('name').'Kernel.php');
  67. $filesystem->rename($targetDir.'/Cache.php', $targetDir.'/'.$input->getArgument('name').'Cache.php');
  68. $filesystem->copy(__DIR__.'/../Resources/skeleton/web/front_controller.php', $file = $webDir.'/'.(file_exists($webDir.'/index.php') ? strtolower($input->getArgument('name')) : 'index').'.php');
  69. Mustache::renderFile($file, $parameters);
  70. $filesystem->copy(__DIR__.'/../Resources/skeleton/web/front_controller_debug.php', $file = $webDir.'/'.strtolower($input->getArgument('name')).'_dev.php');
  71. Mustache::renderFile($file, $parameters);
  72. }
  73. }