Development

/branches/1.1/lib/task/generator/sfGenerateAppTask.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/task/generator/sfGenerateAppTask.class.php

Revision 17041, 5.7 kB (checked in by fabien, 5 years ago)

[1.1, 1.2, 1.3] fixed --csrf-secret and --escaping-strategy options of generate:app

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 require_once(dirname(__FILE__).'/sfGeneratorBaseTask.class.php');
12
13 /**
14  * Generates a new application.
15  *
16  * @package    symfony
17  * @subpackage task
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfGenerateAppTask extends sfGeneratorBaseTask
22 {
23   /**
24    * @see sfTask
25    */
26   protected function doRun(sfCommandManager $commandManager, $options)
27   {
28     $this->process($commandManager, $options);
29
30     $this->checkProjectExists();
31
32     return $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues());
33   }
34
35   /**
36    * @see sfTask
37    */
38   protected function configure()
39   {
40     $this->addArguments(array(
41       new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
42     ));
43
44     $this->addOptions(array(
45       new sfCommandOption('escaping-strategy', null, sfCommandOption::PARAMETER_REQUIRED, 'Output escaping strategy', false),
46       new sfCommandOption('csrf-secret', null, sfCommandOption::PARAMETER_REQUIRED, 'Secret to use for CSRF protection', false),
47     ));
48
49     $this->aliases = array('init-app');
50     $this->namespace = 'generate';
51     $this->name = 'app';
52
53     $this->briefDescription = 'Generates a new application';
54
55     $this->detailedDescription = <<<EOF
56 The [generate:app|INFO] task creates the basic directory structure
57 for a new application in the current project:
58
59   [./symfony generate:app frontend|INFO]
60
61 This task also creates two front controller scripts in the
62 [web/|COMMENT] directory:
63
64   [web/%application%.php|INFO]     for the production environment
65   [web/%application%_dev.php|INFO] for the development environment
66
67 For the first application, the production environment script is named
68 [index.php|COMMENT].
69
70 If an application with the same name already exists,
71 it throws a [sfCommandException|COMMENT].
72
73 You can enable output escaping (to prevent XSS) by using the [escaping-strategy|COMMENT] option:
74
75   [./symfony generate:app frontend --escaping-strategy=on|INFO]
76
77 You can enable session token in forms (to prevent CSRF) by defining
78 a secret with the [csrf-secret|COMMENT] option:
79
80   [./symfony generate:app frontend --csrf-secret=UniqueSecret|INFO]
81
82 EOF;
83   }
84
85   /**
86    * @see sfTask
87    */
88   protected function execute($arguments = array(), $options = array())
89   {
90     $app = $arguments['application'];
91
92     // Validate the application name
93     if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $app))
94     {
95       throw new sfCommandException(sprintf('The application name "%s" is invalid.', $app));
96     }
97
98     $appDir = sfConfig::get('sf_apps_dir').'/'.$app;
99
100     if (is_dir($appDir))
101     {
102       throw new sfCommandException(sprintf('The application "%s" already exists.', $appDir));
103     }
104
105     // Create basic application structure
106     $finder = sfFinder::type('any')->discard('.sf');
107     $this->getFilesystem()->mirror(dirname(__FILE__).'/skeleton/app/app', $appDir, $finder);
108
109     // Create $app.php or index.php if it is our first app
110     $indexName = 'index';
111     $firstApp = !file_exists(sfConfig::get('sf_web_dir').'/index.php');
112     if (!$firstApp)
113     {
114       $indexName = $app;
115     }
116
117     // Set no_script_name value in settings.yml for production environment
118     $finder = sfFinder::type('file')->name('settings.yml');
119     $this->getFilesystem()->replaceTokens($finder->in($appDir.'/config'), '##', '##', array(
120       'NO_SCRIPT_NAME'    => $firstApp ? 'on' : 'off',
121       'CSRF_SECRET'       => sfYamlInline::dump(sfYamlInline::parseScalar($options['csrf-secret'])),
122       'ESCAPING_STRATEGY' => sfYamlInline::dump((boolean) sfYamlInline::parseScalar($options['escaping-strategy'])),
123     ));
124
125     $this->getFilesystem()->copy(dirname(__FILE__).'/skeleton/app/web/index.php', sfConfig::get('sf_web_dir').'/'.$indexName.'.php');
126     $this->getFilesystem()->copy(dirname(__FILE__).'/skeleton/app/web/index.php', sfConfig::get('sf_web_dir').'/'.$app.'_dev.php');
127
128     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$indexName.'.php', '##', '##', array(
129       'APP_NAME'    => $app,
130       'ENVIRONMENT' => 'prod',
131       'IS_DEBUG'    => 'false',
132       'IP_CHECK'    => '',
133     ));
134
135     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$app.'_dev.php', '##', '##', array(
136       'APP_NAME'    => $app,
137       'ENVIRONMENT' => 'dev',
138       'IS_DEBUG'    => 'true',
139       'IP_CHECK'    => '// this check prevents access to debug front controllers that are deployed by accident to production servers.'.PHP_EOL.
140                        '// feel free to remove this, extend it or make something more sophisticated.'.PHP_EOL.
141                        'if (!in_array(@$_SERVER[\'REMOTE_ADDR\'], array(\'127.0.0.1\', \'::1\')))'.PHP_EOL.
142                        '{'.PHP_EOL.
143                        '  die(\'You are not allowed to access this file. Check \'.basename(__FILE__).\' for more information.\');'.PHP_EOL.
144                        '}'.PHP_EOL,
145     ));
146
147     $this->getFilesystem()->rename($appDir.'/config/ApplicationConfiguration.class.php', $appDir.'/config/'.$app.'Configuration.class.php');
148
149     $this->getFilesystem()->replaceTokens($appDir.'/config/'.$app.'Configuration.class.php', '##', '##', array('APP_NAME' => $app));
150
151     $fixPerms = new sfProjectPermissionsTask($this->dispatcher, $this->formatter);
152     $fixPerms->setCommandApplication($this->commandApplication);
153     $fixPerms->run();
154
155     // Create test dir
156     $this->getFilesystem()->mkdirs(sfConfig::get('sf_test_dir').'/functional/'.$app);
157   }
158 }
159
Note: See TracBrowser for help on using the browser.