Permalink
Browse files

Added config-class argument to specify a custom theme configuration c…

…lass
  • Loading branch information...
roldan committed May 28, 2011
1 parent 8a75ca0 commit 8eba42ddc12d494b6fcbfd225ff0629963d83992
Showing with 153 additions and 136 deletions.
  1. +74 −60 lib/task/sfThemeBaseTask.class.php
  2. +79 −76 lib/task/sfThemeGenerateTask.class.php
@@ -1,79 +1,93 @@
<?php
/**
-*
-*/
+ *
+ */
abstract class sfThemeBaseTask extends sfDoctrineGenerateModuleTask
{
- protected function getThemeConfiguration($theme = null)
- {
- // Get Theme Configuration
- if (!class_exists($configClass = sprintf('sf%sThemeConfiguration', sfInflector::camelize($theme))))
+
+ protected function getThemeConfiguration($theme = null, $configClass = null)
{
- throw new InvalidArgumentException(sprintf('No theme configuration class exists for "%s". Please create a %s class', $theme, $configClass));
+ // Get Theme Configuration
+ if (is_null($configClass))
+ {
+ // Use default
+ if (!class_exists($configClass = sprintf('sf%sThemeConfiguration', sfInflector::camelize($theme))))
+ {
+ throw new InvalidArgumentException(sprintf('No theme configuration class exists for "%s". Please create a %s class', $theme, $configClass));
+ }
+ }
+ else
+ {
+ // Use custom configClass
+ if (!class_exists($configClass))
+ {
+ throw new InvalidArgumentException(sprintf('No theme configuration class exists for "%s". Please create a %s class', $theme, $configClass));
+ }
+ }
+
+
+ return new $configClass($this, array_merge($this->commandManager->getArgumentValues(), $this->commandManager->getOptionValues()));
}
- return new $configClass($this, array_merge($this->commandManager->getArgumentValues(), $this->commandManager->getOptionValues()));
- }
-
- public function ask($question, $style = 'QUESTION', $default = null)
- {
- if ($default !== null)
+ public function ask($question, $style = 'QUESTION', $default = null)
{
- switch (true)
- {
- case $default === true:
- $text = 'true';
- break;
+ if ($default !== null)
+ {
+ switch (true)
+ {
+ case $default === true:
+ $text = 'true';
+ break;
+
+ case $default === false:
+ $text = 'false';
+ break;
+
+ default:
+ $text = $default;
+ }
+ $question = sprintf('%s [%s]:', $question, $text);
+ }
- case $default === false:
- $text = 'false';
- break;
-
- default:
- $text = $default;
- }
- $question = sprintf('%s [%s]:', $question, $text);
+ // Add colon to make it clear this is a PROMPT
+ if ($question[strlen($question) - 1] !== ':')
+ {
+ $question .= ':';
+ }
+
+ return parent::ask($question, $style, $default);
}
-
- // Add colon to make it clear this is a PROMPT
- if ($question[strlen($question)-1] !== ':')
+
+ public function getThemeDir($theme, $class)
{
- $question .= ':';
+ $dirs = array_merge(
+ array(sfConfig::get('sf_data_dir') . '/generator/' . $class . '/' . $theme), // project
+ $this->configuration->getPluginSubPaths('/data/generator/' . $class . '/' . $theme) // plugins
+ );
+
+ foreach ($dirs as $dir)
+ {
+ if (is_dir($dir))
+ {
+ return $dir;
+ }
+ }
}
- return parent::ask($question, $style, $default);
- }
-
- public function getThemeDir($theme, $class)
- {
- $dirs = array_merge(
- array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme), // project
- $this->configuration->getPluginSubPaths('/data/generator/'.$class.'/'.$theme) // plugins
- );
-
- foreach ($dirs as $dir)
+ public function bootstrapSymfony($app, $env, $debug = true)
{
- if (is_dir($dir))
- {
- return $dir;
- }
+ $this->configuration = ProjectConfiguration::getApplicationConfiguration($app, $env, $debug);
+
+ // Prevents from accidental re-bootstrapping!
+ if (!sfContext::hasInstance())
+ {
+ $this->context = sfContext::createInstance($this->configuration);
+ }
}
- }
-
- public function bootstrapSymfony($app, $env, $debug = true)
- {
- $this->configuration = ProjectConfiguration::getApplicationConfiguration($app, $env, $debug);
-
- // Prevents from accidental re-bootstrapping!
- if (!sfContext::hasInstance())
+
+ public function getEventDispatcher()
{
- $this->context = sfContext::createInstance($this->configuration);
+ return $this->dispatcher;
}
- }
-
- public function getEventDispatcher()
- {
- return $this->dispatcher;
- }
}
@@ -7,6 +7,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
/**
* Generates a Doctrine admin module.
*
@@ -17,96 +18,98 @@
*/
class sfThemeGenerateTask extends sfThemeBaseTask
{
- /**
- * @see sfTask
- */
- protected function configure()
- {
- $this->addArguments(array(
- new sfCommandArgument('theme', sfCommandArgument::REQUIRED, 'The theme name'),
- ));
-
- $this->addOptions(array(
- new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
- new sfCommandOption('accept-defaults', null, sfCommandOption::PARAMETER_NONE, 'Accept all default theme options'),
- ));
-
- $this->namespace = 'theme';
- $this->name = 'generate';
- $this->aliases = array('generate-theme');
- $this->briefDescription = 'Generates functionality in your project based on a theme';
-
- $this->detailedDescription = <<<EOF
+
+ /**
+ * @see sfTask
+ */
+ protected function configure()
+ {
+ $this->addArguments(array(
+ new sfCommandArgument('theme', sfCommandArgument::REQUIRED, 'The theme name'),
+ ));
+
+ $this->addOptions(array(
+ new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
+ new sfCommandOption('accept-defaults', null, sfCommandOption::PARAMETER_NONE, 'Accept all default theme options'),
+ new sfCommandOption("config-class", null, sfCommandOption::PARAMETER_OPTIONAL, 'Configuration class to use', null)
+ ));
+
+ $this->namespace = 'theme';
+ $this->name = 'generate';
+ $this->aliases = array('generate-theme');
+ $this->briefDescription = 'Generates functionality in your project based on a theme';
+
+ $this->detailedDescription = <<<EOF
The [generate:theme|INFO] task generates functionality according to a theme:
[./symfony generate:theme default|INFO]
The theme will then prompt the user for the arguments it requires to create itself.
EOF;
- }
-
- /**
- * @see sfTask
- */
- protected function execute($arguments = array(), $options = array())
- {
- $databaseManager = new sfDatabaseManager($this->configuration);
-
- $this->themeConfiguration = $this->getThemeConfiguration($arguments['theme']);
-
- $this->themeConfiguration->setup();
- $this->themeConfiguration->execute();
- $this->themeConfiguration->cleanup();
-
- $this->logSection('generate', 'Task complete.');
- }
-
- protected function process(sfCommandManager $commandManager, $options)
- {
- $this->commandManager = new sfThemeCommandManager($commandManager);
- $this->commandManager->process($options);
- $commandManager->process($options);
-
- if (!$this->commandManager->isValid())
- {
- throw new sfCommandArgumentsException(sprintf("The execution of task \"%s\" failed.\n- %s", $this->getFullName(), implode("\n- ", $this->commandManager->getErrors())));
}
- }
- // ==============================================
- // = From sfDoctrineGenerateAdminTask.class.php =
- // ==============================================
+ /**
+ * @see sfTask
+ */
+ protected function execute($arguments = array(), $options = array())
+ {
+ $databaseManager = new sfDatabaseManager($this->configuration);
+
+ $this->themeConfiguration = $this->getThemeConfiguration($arguments['theme'], $options['config-class']);
+
+ $this->themeConfiguration->setup();
+ $this->themeConfiguration->execute();
+ $this->themeConfiguration->cleanup();
- protected function getRouteFromName($name)
- {
- $config = new sfRoutingConfigHandler();
- $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
+ $this->logSection('generate', 'Task complete.');
+ }
- if (isset($routes[$name]))
+ protected function process(sfCommandManager $commandManager, $options)
{
- return $routes[$name];
+ $this->commandManager = new sfThemeCommandManager($commandManager);
+ $this->commandManager->process($options);
+ $commandManager->process($options);
+
+ if (!$this->commandManager->isValid())
+ {
+ throw new sfCommandArgumentsException(sprintf("The execution of task \"%s\" failed.\n- %s", $this->getFullName(), implode("\n- ", $this->commandManager->getErrors())));
+ }
}
- return false;
- }
-
- /**
- * Checks whether a route references a model and module.
- *
- * @param mixed $route A route collection
- * @param string $model A model name
- * @param string $module A module name
- *
- * @return boolean
- */
- protected function checkRoute($route, $model, $module)
- {
- if ($route instanceof sfDoctrineRouteCollection)
+ // ==============================================
+ // = From sfDoctrineGenerateAdminTask.class.php =
+ // ==============================================
+
+ protected function getRouteFromName($name)
{
- $options = $route->getOptions();
- return $model == $options['model'] && $module == $options['module'];
+ $config = new sfRoutingConfigHandler();
+ $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml'));
+
+ if (isset($routes[$name]))
+ {
+ return $routes[$name];
+ }
+
+ return false;
}
- return false;
- }
+ /**
+ * Checks whether a route references a model and module.
+ *
+ * @param mixed $route A route collection
+ * @param string $model A model name
+ * @param string $module A module name
+ *
+ * @return boolean
+ */
+ protected function checkRoute($route, $model, $module)
+ {
+ if ($route instanceof sfDoctrineRouteCollection)
+ {
+ $options = $route->getOptions();
+ return $model == $options['model'] && $module == $options['module'];
+ }
+
+ return false;
+ }
}

0 comments on commit 8eba42d

Please sign in to comment.