Development

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

You must first sign up to be able to contribute.

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

Revision 17762, 5.8 kB (checked in by fabien, 6 years ago)

[1.2, 1.3] fixed the --csrf-secret option of generate:app (at last)

  • 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     if (false === $options['csrf-secret'])
118     {
119       $options['csrf-secret'] = 'false';
120     }
121
122     // Set no_script_name value in settings.yml for production environment
123     $finder = sfFinder::type('file')->name('settings.yml');
124     $this->getFilesystem()->replaceTokens($finder->in($appDir.'/config'), '##', '##', array(
125       'NO_SCRIPT_NAME'    => $firstApp ? 'on' : 'off',
126       'CSRF_SECRET'       => sfYamlInline::dump(sfYamlInline::parseScalar($options['csrf-secret'])),
127       'ESCAPING_STRATEGY' => sfYamlInline::dump((boolean) sfYamlInline::parseScalar($options['escaping-strategy'])),
128     ));
129
130     $this->getFilesystem()->copy(dirname(__FILE__).'/skeleton/app/web/index.php', sfConfig::get('sf_web_dir').'/'.$indexName.'.php');
131     $this->getFilesystem()->copy(dirname(__FILE__).'/skeleton/app/web/index.php', sfConfig::get('sf_web_dir').'/'.$app.'_dev.php');
132
133     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$indexName.'.php', '##', '##', array(
134       'APP_NAME'    => $app,
135       'ENVIRONMENT' => 'prod',
136       'IS_DEBUG'    => 'false',
137       'IP_CHECK'    => '',
138     ));
139
140     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$app.'_dev.php', '##', '##', array(
141       'APP_NAME'    => $app,
142       'ENVIRONMENT' => 'dev',
143       'IS_DEBUG'    => 'true',
144       'IP_CHECK'    => '// this check prevents access to debug front controllers that are deployed by accident to production servers.'.PHP_EOL.
145                        '// feel free to remove this, extend it or make something more sophisticated.'.PHP_EOL.
146                        'if (!in_array(@$_SERVER[\'REMOTE_ADDR\'], array(\'127.0.0.1\', \'::1\')))'.PHP_EOL.
147                        '{'.PHP_EOL.
148                        '  die(\'You are not allowed to access this file. Check \'.basename(__FILE__).\' for more information.\');'.PHP_EOL.
149                        '}'.PHP_EOL,
150     ));
151
152     $this->getFilesystem()->rename($appDir.'/config/ApplicationConfiguration.class.php', $appDir.'/config/'.$app.'Configuration.class.php');
153
154     $this->getFilesystem()->replaceTokens($appDir.'/config/'.$app.'Configuration.class.php', '##', '##', array('APP_NAME' => $app));
155
156     $fixPerms = new sfProjectPermissionsTask($this->dispatcher, $this->formatter);
157     $fixPerms->setCommandApplication($this->commandApplication);
158     $fixPerms->run();
159
160     // Create test dir
161     $this->getFilesystem()->mkdirs(sfConfig::get('sf_test_dir').'/functional/'.$app);
162   }
163 }
164
Note: See TracBrowser for help on using the browser.