InitApplicationCommand.php 3.3 KB

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