1. sfGenerateAppTask.class.php
  2. /** * Generates a new application. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfGenerateAppTask.class.php 24039 2009-11-16 17:52:14Z Kris.Wallsmith $ */
  3. class sfGenerateAppTask extends sfGeneratorBaseTask
  4. {
  5. /**
  6. * @see sfTask
  7. */
  8. protected function configure()
  9. {
  10. $this->addArguments(array(
  11. new sfCommandArgument('app', sfCommandArgument::REQUIRED, 'The application name'),
  12. ));
  13. $this->addOptions(array(
  14. new sfCommandOption('escaping-strategy', null, sfCommandOption::PARAMETER_REQUIRED, 'Output escaping strategy', true),
  15. new sfCommandOption('csrf-secret', null, sfCommandOption::PARAMETER_REQUIRED, 'Secret to use for CSRF protection', true),
  16. ));
  17. $this->namespace = 'generate';
  18. $this->name = 'app';
  19. $this->briefDescription = 'Generates a new application';
  20. $this->detailedDescription = <<<EOF
  21. The [generate:app|INFO] task creates the basic directory structure
  22. for a new application in the current project:
  23. [./symfony generate:app frontend|INFO]
  24. This task also creates two front controller scripts in the
  25. [web/|COMMENT] directory:
  26. [web/%application%.php|INFO] for the production environment
  27. [web/%application%_dev.php|INFO] for the development environment
  28. For the first application, the production environment script is named
  29. [index.php|COMMENT].
  30. If an application with the same name already exists,
  31. it throws a [sfCommandException|COMMENT].
  32. By default, the output escaping is enabled (to prevent XSS), and a random
  33. secret is also generated to prevent CSRF.
  34. You can disable output escaping by using the [escaping-strategy|COMMENT]
  35. option:
  36. [./symfony generate:app frontend --escaping-strategy=false|INFO]
  37. You can enable session token in forms (to prevent CSRF) by defining
  38. a secret with the [csrf-secret|COMMENT] option:
  39. [./symfony generate:app frontend --csrf-secret=UniqueSecret|INFO]
  40. You can customize the default skeleton used by the task by creating a
  41. [%sf_data_dir%/skeleton/app|COMMENT] directory.
  42. EOF;
  43. }
  44. /**
  45. * @see sfTask
  46. */
  47. protected function execute($arguments = array(), $options = array())
  48. {
  49. $app = $arguments['app'];
  50. // Validate the application name
  51. if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $app))
  52. {
  53. throw new sfCommandException(sprintf('The application name "%s" is invalid.', $app));
  54. }
  55. $appDir = sfConfig::get('sf_apps_dir').'/'.$app;
  56. if (is_dir($appDir))
  57. {
  58. throw new sfCommandException(sprintf('The application "%s" already exists.', $appDir));
  59. }
  60. if (is_readable(sfConfig::get('sf_data_dir').'/skeleton/app'))
  61. {
  62. $skeletonDir = sfConfig::get('sf_data_dir').'/skeleton/app';
  63. }
  64. else
  65. {
  66. $skeletonDir = dirname(__FILE__).'/skeleton/app';
  67. }
  68. // Create basic application structure
  69. $finder = sfFinder::type('any')->discard('.sf');
  70. $this->getFilesystem()->mirror($skeletonDir.'/app', $appDir, $finder);
  71. // Create $app.php or index.php if it is our first app
  72. $indexName = 'index';
  73. $firstApp = !file_exists(sfConfig::get('sf_web_dir').'/index.php');
  74. if (!$firstApp)
  75. {
  76. $indexName = $app;
  77. }
  78. if (true === $options['csrf-secret'])
  79. {
  80. $options['csrf-secret'] = sha1(rand(111111111, 99999999).getmypid());
  81. }
  82. // Set no_script_name value in settings.yml for production environment
  83. $finder = sfFinder::type('file')->name('settings.yml');
  84. $this->getFilesystem()->replaceTokens($finder->in($appDir.'/config'), '##', '##', array(
  85. 'NO_SCRIPT_NAME' => $firstApp ? 'true' : 'false',
  86. 'CSRF_SECRET' => sfYamlInline::dump(sfYamlInline::parseScalar($options['csrf-secret'])),
  87. 'ESCAPING_STRATEGY' => sfYamlInline::dump((boolean) sfYamlInline::parseScalar($options['escaping-strategy'])),
  88. 'USE_DATABASE' => sfConfig::has('sf_orm') ? 'true' : 'false',
  89. ));
  90. $this->getFilesystem()->copy($skeletonDir.'/web/index.php', sfConfig::get('sf_web_dir').'/'.$indexName.'.php');
  91. $this->getFilesystem()->copy($skeletonDir.'/web/index.php', sfConfig::get('sf_web_dir').'/'.$app.'_dev.php');
  92. $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$indexName.'.php', '##', '##', array(
  93. 'APP_NAME' => $app,
  94. 'ENVIRONMENT' => 'prod',
  95. 'IS_DEBUG' => 'false',
  96. 'IP_CHECK' => '',
  97. ));
  98. $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$app.'_dev.php', '##', '##', array(
  99. 'APP_NAME' => $app,
  100. 'ENVIRONMENT' => 'dev',
  101. 'IS_DEBUG' => 'true',
  102. 'IP_CHECK' => '// this check prevents access to debug front controllers that are deployed by accident to production servers.'.PHP_EOL.
  103. '// feel free to remove this, extend it or make something more sophisticated.'.PHP_EOL.
  104. 'if (!in_array(@$_SERVER[\'REMOTE_ADDR\'], array(\'127.0.0.1\', \'::1\')))'.PHP_EOL.
  105. '{'.PHP_EOL.
  106. ' die(\'You are not allowed to access this file. Check \'.basename(__FILE__).\' for more information.\');'.PHP_EOL.
  107. '}'.PHP_EOL,
  108. ));
  109. $this->getFilesystem()->rename($appDir.'/config/ApplicationConfiguration.class.php', $appDir.'/config/'.$app.'Configuration.class.php');
  110. $this->getFilesystem()->replaceTokens($appDir.'/config/'.$app.'Configuration.class.php', '##', '##', array('APP_NAME' => $app));
  111. $fixPerms = new sfProjectPermissionsTask($this->dispatcher, $this->formatter);
  112. $fixPerms->setCommandApplication($this->commandApplication);
  113. $fixPerms->setConfiguration($this->configuration);
  114. $fixPerms->run();
  115. // Create test dir
  116. $this->getFilesystem()->mkdirs(sfConfig::get('sf_test_dir').'/functional/'.$app);
  117. }
  118. }

Debug toolbar