Changeset 9641 for doc/branches/1.1/cookbook/en
- Timestamp:
- 06/18/08 19:39:43 (5 years ago)
- Files:
-
- doc/branches/1.1/cookbook/en/tasks.txt (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
doc/branches/1.1/cookbook/en/tasks.txt
r9640 r9641 21 21 -------------------------- 22 22 23 First create a %SF_ROOT_DIR%/lib/task/doNothingTask.class.phpfile, and open it in your favorite editor.23 First create a `%SF_ROOT_DIR%/lib/task/doNothingTask.class.php` file, and open it in your favorite editor. 24 24 25 [php]26 class doNothingTask extends sfBaseTask27 {28 protected function configure()29 {30 $this->namespace = 'project';31 $this->name = 'do-nothing';32 $this->briefDescription = 'Does strictly nothing';33 34 $this->detailedDescription = <<<EOF35 This task is completely useless, and should be run as often as possible.36 EOF;37 }38 39 protected function execute($arguments = array(), $options = array())40 {41 $this->logSection('do-nothing', 'I did nothing successfully!');42 }43 }25 [php] 26 class doNothingTask extends sfBaseTask 27 { 28 protected function configure() 29 { 30 $this->namespace = 'project'; 31 $this->name = 'do-nothing'; 32 $this->briefDescription = 'Does strictly nothing'; 33 34 $this->detailedDescription = <<<EOF 35 This task is completely useless, and should be run as often as possible. 36 EOF; 37 } 38 39 protected function execute($arguments = array(), $options = array()) 40 { 41 $this->logSection('do-nothing', 'I did nothing successfully!'); 42 } 43 } 44 44 45 45 This task for sure does not much, but demonstrates the first basic concepts.: 46 46 47 * The `configure()` method describes the task. Invocation name, scope, syntax, help, options and arguments.48 * The `execute()` method is the one who does all the job actually, and will be called when the task is run.49 * The `logSection()` method can be used to print messages on the console output.47 * The `configure()` method describes the task. Invocation name, scope, syntax, help, options and arguments. 48 * The `execute()` method is the one who does all the job actually, and will be called when the task is run. 49 * The `logSection()` method can be used to print messages on the console output. 50 50 51 51 You can play around a bit with it: 52 52 53 $ php symfony help project:do-nothing54 $ php symfony project:do-nothing53 $ php symfony help project:do-nothing 54 $ php symfony project:do-nothing 55 55 56 56 Some command line interaction … … 59 59 Arguments and options are the way to give parameters to a task. 60 60 61 $ php symfony project:hello-world --name="Romain"61 $ php symfony project:hello-world --name="Romain" 62 62 63 63 Here we're running the `project:hello-world` task with the `name` option set to `Romain` 64 64 65 $ php symfony project:hello-world Hi65 $ php symfony project:hello-world Hi 66 66 67 67 Now, we run the same task with the first argument set to `Hi`. … … 71 71 Let's write our `project:hello-world` task: 72 72 73 [php]74 class doHelloWorldTask extends sfBaseTask75 {76 protected function configure()77 {78 $this->addArgument('verb', sfCommandArgument::OPTIONAL, 'Customize the verb used to say hello', 'hello');79 $this->addOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Customize the person to say hello to', 'world');80 81 $this->namespace = 'project';82 $this->name = 'hello-world';83 $this->briefDescription = 'Spread the (hello) world';84 85 $this->detailedDescription = <<<EOF86 Runs an evolved hello world display, with customisable name and word.87 EOF;88 }89 90 protected function execute($arguments = array(), $options = array())91 {92 $this->logSection('do', ucfirst($arguments['verb']).' '.ucfirst($options['name']));93 }94 }73 [php] 74 class doHelloWorldTask extends sfBaseTask 75 { 76 protected function configure() 77 { 78 $this->addArgument('verb', sfCommandArgument::OPTIONAL, 'Customize the verb used to say hello', 'hello'); 79 $this->addOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Customize the person to say hello to', 'world'); 80 81 $this->namespace = 'project'; 82 $this->name = 'hello-world'; 83 $this->briefDescription = 'Spread the (hello) world'; 84 85 $this->detailedDescription = <<<EOF 86 Runs an evolved hello world display, with customisable name and word. 87 EOF; 88 } 89 90 protected function execute($arguments = array(), $options = array()) 91 { 92 $this->logSection('do', ucfirst($arguments['verb']).' '.ucfirst($options['name'])); 93 } 94 } 95 95 96 96 Now check out how symfony helps the lost user about how to use our new task: 97 97 98 $ php symfony project:hello-world invalid arguments given99 $ php symfony help project:hello-world98 $ php symfony project:hello-world invalid arguments given 99 $ php symfony help project:hello-world 100 100 101 101 And play a bit with the task: 102 102 103 $ php symfony project:hello-world104 $ php symfony project:hello-world --name="romain"105 $ php symfony project:hello-world --name=romain hi106 $ php symfony project:hello-world hi --name=romain103 $ php symfony project:hello-world 104 $ php symfony project:hello-world --name="romain" 105 $ php symfony project:hello-world --name=romain hi 106 $ php symfony project:hello-world hi --name=romain 107 107 108 108 Some other handy features 109 109 ------------------------- 110 110 111 * **Do you need the database layer?**111 * **Do you need the database layer?** 112 112 113 113 [php] … … 119 119 } 120 120 121 * **Run another task within a task?**121 * **Run another task within a task?** 122 122 123 123 [php] … … 125 125 $myOtherTask->run($arguments = array('foo' => 'bar'), $options = array('far' => 'boo')); 126 126 127 * **Need to let the user choose the environment, while providing a default one?**127 * **Need to let the user choose the environment, while providing a default one?** 128 128 129 129 Just add the `env` option in the `::configure()` method and symfony will use its value as the environemnt. … … 132 132 $this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'prod'); 133 133 134 135 134 What do you think? Won't this new powerful batch subsystem greatly improve your project maintenance times?