Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just in case you haven't already seen this: <a href="http://www.symfony-project.org/cookbook/1_1/en/tasks" rel="nofollow noreferrer">http://www.symfony-project.org/cookbook/1_1/en/tasks</a></p> <p>That provides a good overview of custom task creation including options and arguments. I actually just sat down to write a couple tasks a few days ago so I'll try and go over what I've picked up so far. Here's an example from a task I created that updates a project from subversion for a quick rebuild:</p> <pre><code>$this-&gt;addOptions(array( new sfCommandOption('username', null, sfCommandOption::PARAMETER_REQUIRED, 'Subversion username'), ... )); </code></pre> <p>The first parameter is the name of the option and will play into the command execution by becoming:</p> <p>./symfony my:task --<strong>username</strong>="foo"</p> <p>The second parameter is defined as a shortcut by the class definition (%symfony_lib_dir%/command/sfCommandOption.class.php) but I haven't played with it yet, your guess is as good as mine.</p> <p>The third parameter specifies if there's more to the argument then just it's calling. Your options are as follows:</p> <pre><code>PARAMETER_NONE ex: --username PARAMETER_OPTIONAL ex: --username[=...] PARAMETER_REQUIRED ex: --username=... </code></pre> <p>The fourth parameter is the description of the argument for help output.</p> <p>There's an optional fifth parameter that can be specified for a default value as long as you're not PARAMETER_NONE (it'll throw an exception on execution if you are).</p> <p>Once your options are set up you can access them from the second parameter passed to your task's execute function, they'll come through as an associative array. Continuing my example I would have the following:</p> <pre><code>protected function execute($arguments = array(), $options = array()) { $task = 'svn up'; if($options['username']) { $task .= ' --username="' . $options['username'] . '"'; } .... } </code></pre> <p>What has always been most helpful for me is code examples and since Symfony is bundled with a great collection of existing tasks I highly recommend reviewing them (%symfony_lib_dir%/task/*) for inspiration and guidance. I was really helped by project/sfProjectDeploy.class.php specifically, it uses a wide variety of the sfTask functionality and was crucial to finding my way through my custom tasks.</p> <p>Hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload