Development

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

You must first sign up to be able to contribute.

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

Revision 24018, 6.0 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] fixed command.* events not firing from generate:app task

  • 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 configure()
27   {
28     $this->addArguments(array(
29       new sfCommandArgument('app', sfCommandArgument::REQUIRED, 'The application name'),
30     ));
31
32     $this->addOptions(array(
33       new sfCommandOption('escaping-strategy', null, sfCommandOption::PARAMETER_REQUIRED, 'Output escaping strategy', true),
34       new sfCommandOption('csrf-secret', null, sfCommandOption::PARAMETER_REQUIRED, 'Secret to use for CSRF protection', true),
35     ));
36
37     $this->aliases = array('init-app');
38     $this->namespace = 'generate';
39     $this->name = 'app';
40
41     $this->briefDescription = 'Generates a new application';
42
43     $this->detailedDescription = <<<EOF
44 The [generate:app|INFO] task creates the basic directory structure
45 for a new application in the current project:
46
47   [./symfony generate:app frontend|INFO]
48
49 This task also creates two front controller scripts in the
50 [web/|COMMENT] directory:
51
52   [web/%application%.php|INFO]     for the production environment
53   [web/%application%_dev.php|INFO] for the development environment
54
55 For the first application, the production environment script is named
56 [index.php|COMMENT].
57
58 If an application with the same name already exists,
59 it throws a [sfCommandException|COMMENT].
60
61 By default, the output escaping is enabled (to prevent XSS), and a random
62 secret is also generated to prevent CSRF.
63
64 You can disable output escaping by using the [escaping-strategy|COMMENT]
65 option:
66
67   [./symfony generate:app frontend --escaping-strategy=false|INFO]
68
69 You can enable session token in forms (to prevent CSRF) by defining
70 a secret with the [csrf-secret|COMMENT] option:
71
72   [./symfony generate:app frontend --csrf-secret=UniqueSecret|INFO]
73
74 You can customize the default skeleton used by the task by creating a
75 [%sf_data_dir%/skeleton/app|COMMENT] directory.
76 EOF;
77   }
78
79   /**
80    * @see sfTask
81    */
82   protected function execute($arguments = array(), $options = array())
83   {
84     $app = $arguments['app'];
85
86     // Validate the application name
87     if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $app))
88     {
89       throw new sfCommandException(sprintf('The application name "%s" is invalid.', $app));
90     }
91
92     $appDir = sfConfig::get('sf_apps_dir').'/'.$app;
93
94     if (is_dir($appDir))
95     {
96       throw new sfCommandException(sprintf('The application "%s" already exists.', $appDir));
97     }
98
99     if (is_readable(sfConfig::get('sf_data_dir').'/skeleton/app'))
100     {
101       $skeletonDir = sfConfig::get('sf_data_dir').'/skeleton/app';
102     }
103     else
104     {
105       $skeletonDir = dirname(__FILE__).'/skeleton/app';
106     }
107
108     // Create basic application structure
109     $finder = sfFinder::type('any')->discard('.sf');
110     $this->getFilesystem()->mirror($skeletonDir.'/app', $appDir, $finder);
111
112     // Create $app.php or index.php if it is our first app
113     $indexName = 'index';
114     $firstApp = !file_exists(sfConfig::get('sf_web_dir').'/index.php');
115     if (!$firstApp)
116     {
117       $indexName = $app;
118     }
119
120     if (true === $options['csrf-secret'])
121     {
122       $options['csrf-secret'] = sha1(rand(111111111, 99999999).getmypid());
123     }
124
125     // Set no_script_name value in settings.yml for production environment
126     $finder = sfFinder::type('file')->name('settings.yml');
127     $this->getFilesystem()->replaceTokens($finder->in($appDir.'/config'), '##', '##', array(
128       'NO_SCRIPT_NAME'    => $firstApp ? 'true' : 'false',
129       'CSRF_SECRET'       => sfYamlInline::dump(sfYamlInline::parseScalar($options['csrf-secret'])),
130       'ESCAPING_STRATEGY' => sfYamlInline::dump((boolean) sfYamlInline::parseScalar($options['escaping-strategy'])),
131       'USE_DATABASE'      => sfConfig::has('sf_orm') ? 'true' : 'false',
132     ));
133
134     $this->getFilesystem()->copy($skeletonDir.'/web/index.php', sfConfig::get('sf_web_dir').'/'.$indexName.'.php');
135     $this->getFilesystem()->copy($skeletonDir.'/web/index.php', sfConfig::get('sf_web_dir').'/'.$app.'_dev.php');
136
137     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$indexName.'.php', '##', '##', array(
138       'APP_NAME'    => $app,
139       'ENVIRONMENT' => 'prod',
140       'IS_DEBUG'    => 'false',
141       'IP_CHECK'    => '',
142     ));
143
144     $this->getFilesystem()->replaceTokens(sfConfig::get('sf_web_dir').'/'.$app.'_dev.php', '##', '##', array(
145       'APP_NAME'    => $app,
146       'ENVIRONMENT' => 'dev',
147       'IS_DEBUG'    => 'true',
148       'IP_CHECK'    => '// this check prevents access to debug front controllers that are deployed by accident to production servers.'.PHP_EOL.
149                        '// feel free to remove this, extend it or make something more sophisticated.'.PHP_EOL.
150                        'if (!in_array(@$_SERVER[\'REMOTE_ADDR\'], array(\'127.0.0.1\', \'::1\')))'.PHP_EOL.
151                        '{'.PHP_EOL.
152                        '  die(\'You are not allowed to access this file. Check \'.basename(__FILE__).\' for more information.\');'.PHP_EOL.
153                        '}'.PHP_EOL,
154     ));
155
156     $this->getFilesystem()->rename($appDir.'/config/ApplicationConfiguration.class.php', $appDir.'/config/'.$app.'Configuration.class.php');
157
158     $this->getFilesystem()->replaceTokens($appDir.'/config/'.$app.'Configuration.class.php', '##', '##', array('APP_NAME' => $app));
159
160     $fixPerms = new sfProjectPermissionsTask($this->dispatcher, $this->formatter);
161     $fixPerms->setCommandApplication($this->commandApplication);
162     $fixPerms->setConfiguration($this->configuration);
163     $fixPerms->run();
164
165     // Create test dir
166     $this->getFilesystem()->mkdirs(sfConfig::get('sf_test_dir').'/functional/'.$app);
167   }
168 }
169
Note: See TracBrowser for help on using the browser.