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