1. sfGenerateProjectTask.class.php
  2. /** * Generates a new project. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfGenerateProjectTask.class.php 27211 2010-01-26 20:23:26Z FabianLange $ */
  3. class sfGenerateProjectTask extends sfGeneratorBaseTask
  4. {
  5. /**
  6. * @see sfTask
  7. */
  8. protected function doRun(sfCommandManager $commandManager, $options)
  9. {
  10. $this->process($commandManager, $options);
  11. return $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues());
  12. }
  13. /**
  14. * @see sfTask
  15. */
  16. protected function configure()
  17. {
  18. $this->addArguments(array(
  19. new sfCommandArgument('name', sfCommandArgument::REQUIRED, 'The project name'),
  20. new sfCommandArgument('author', sfCommandArgument::OPTIONAL, 'The project author', 'Your name here'),
  21. ));
  22. $this->addOptions(array(
  23. new sfCommandOption('orm', null, sfCommandOption::PARAMETER_REQUIRED, 'The ORM to use by default', 'Doctrine'),
  24. new sfCommandOption('installer', null, sfCommandOption::PARAMETER_REQUIRED, 'An installer script to execute', null),
  25. ));
  26. $this->namespace = 'generate';
  27. $this->name = 'project';
  28. $this->briefDescription = 'Generates a new project';
  29. $this->detailedDescription = <<<EOF
  30. The [generate:project|INFO] task creates the basic directory structure
  31. for a new project in the current directory:
  32. [./symfony generate:project blog|INFO]
  33. If the current directory already contains a symfony project,
  34. it throws a [sfCommandException|COMMENT].
  35. By default, the task configures Doctrine as the ORM. If you want to use
  36. Propel, use the [--orm|COMMENT] option:
  37. [./symfony generate:project blog --orm=Propel|INFO]
  38. If you don't want to use an ORM, pass [none|COMMENT] to [--orm|COMMENT] option:
  39. [./symfony generate:project blog --orm=none|INFO]
  40. You can also pass the [--installer|COMMENT] option to further customize the
  41. project:
  42. [./symfony generate:project blog --installer=./installer.php|INFO]
  43. You can optionally include a second [author|COMMENT] argument to specify what name to
  44. use as author when symfony generates new classes:
  45. [./symfony generate:project blog "Jack Doe"|INFO]
  46. EOF;
  47. }
  48. /**
  49. * @see sfTask
  50. */
  51. protected function execute($arguments = array(), $options = array())
  52. {
  53. if (file_exists('symfony'))
  54. {
  55. throw new sfCommandException(sprintf('A symfony project already exists in this directory (%s).', getcwd()));
  56. }
  57. if (!in_array(strtolower($options['orm']), array('propel', 'doctrine', 'none')))
  58. {
  59. throw new InvalidArgumentException(sprintf('Invalid ORM name "%s".', $options['orm']));
  60. }
  61. if ($options['installer'] && $this->commandApplication && !file_exists($options['installer']))
  62. {
  63. throw new InvalidArgumentException(sprintf('The installer "%s" does not exist.', $options['installer']));
  64. }
  65. // clean orm option
  66. $options['orm'] = ucfirst(strtolower($options['orm']));
  67. $this->arguments = $arguments;
  68. $this->options = $options;
  69. // create basic project structure
  70. $this->installDir(dirname(__FILE__).'/skeleton/project');
  71. // update ProjectConfiguration class (use a relative path when the symfony core is nested within the project)
  72. $symfonyCoreAutoload = 0 === strpos(sfConfig::get('sf_symfony_lib_dir'), sfConfig::get('sf_root_dir')) ?
  73. sprintf('dirname(__FILE__).\'/..%s/autoload/sfCoreAutoload.class.php\'', str_replace(sfConfig::get('sf_root_dir'), '', sfConfig::get('sf_symfony_lib_dir'))) :
  74. var_export(sfConfig::get('sf_symfony_lib_dir').'/autoload/sfCoreAutoload.class.php', true);
  75. $this->replaceTokens(array(sfConfig::get('sf_config_dir')), array('SYMFONY_CORE_AUTOLOAD' => $symfonyCoreAutoload));
  76. $this->tokens = array(
  77. 'ORM' => $this->options['orm'],
  78. 'PROJECT_NAME' => $this->arguments['name'],
  79. 'AUTHOR_NAME' => $this->arguments['author'],
  80. 'PROJECT_DIR' => sfConfig::get('sf_root_dir'),
  81. );
  82. $this->replaceTokens();
  83. // execute the choosen ORM installer script
  84. if (in_array($options['orm'], array('Doctrine', 'Propel')))
  85. {
  86. include dirname(__FILE__).'/../../plugins/sf'.$options['orm'].'Plugin/config/installer.php';
  87. }
  88. // execute a custom installer
  89. if ($options['installer'] && $this->commandApplication)
  90. {
  91. if ($this->canRunInstaller($options['installer']))
  92. {
  93. $this->reloadTasks();
  94. include $options['installer'];
  95. }
  96. }
  97. // fix permission for common directories
  98. $fixPerms = new sfProjectPermissionsTask($this->dispatcher, $this->formatter);
  99. $fixPerms->setCommandApplication($this->commandApplication);
  100. $fixPerms->setConfiguration($this->configuration);
  101. $fixPerms->run();
  102. $this->replaceTokens();
  103. }
  104. protected function canRunInstaller($installer)
  105. {
  106. if (preg_match('#^(https?|ftps?)://#', $installer))
  107. {
  108. if (ini_get('allow_url_fopen') === false)
  109. {
  110. $this->logSection('generate', sprintf('Cannot run remote installer "%s" because "allow_url_fopen" is off', $installer));
  111. }
  112. if (ini_get('allow_url_include') === false)
  113. {
  114. $this->logSection('generate', sprintf('Cannot run remote installer "%s" because "allow_url_include" is off', $installer));
  115. }
  116. return ini_get('allow_url_fopen') && ini_get('allow_url_include');
  117. }
  118. return true;
  119. }
  120. }

Debug toolbar