The symfony Cookbook

Como criar uma tarefa

Be trained by symfony experts
Dec 10: Paris (1.1 - Francais)
Dec 10: Atlanta (1.1 - English)
Dec 17: Montreal (1.1 - Francais)
Jan 21: Paris (1.1 - Francais)
Feb 18: Paris (1.1 - Francais)
and more...

About

You are currently reading "The symfony Cookbook" which is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License license.

Search


powered by google

Chapter Content

Vamos escrever a nossa primeira tarefa

Alguma interação com a linha de comando

Outras características úteis

You are currently browsing "The symfony Cookbook" in Portuguese for the 1.1 version. Switch to another language:
Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Translation of this work into another language is explicitly allowed.

Assim como qualquer aplicação, seu projeto possui tarefas de manutenção repetitivas, operações do banco de dados ou outros console scripts rodando em uma base regular.

O symfony 1.1 estende as tarefas pake do symfony 1.0 para criar um utilitário de linha de comando poderoso e uniforme para seus projetos, totalmente integrado com a Interfade de Linha de Comando (CLI) do symfony.

Vamos escrever a nossa primeira tarefa

Abra o diretório do seu projeto symfony 1.1 e digite:

$ php symfony generate:task doNothing

Ele irá iniciar uma tarefa vazia em lib/task/doNothingTask.class.php. Vamos sintonizá-lo um pouco.

class doNothingTask extends sfBaseTask
{
  protected function configure()
  {
    $this->namespace        = 'project';
    $this->name             = 'do-nothing';
    $this->briefDescription = 'Does strictly nothing';
 
    $this->detailedDescription = <<<EOF
Esta tarefa é completamente inútil, e deve ser executada com a maior freqüência possível.
EOF;
  }
 
  protected function execute($arguments = array(), $options = array())
  {
    $this->logSection('do-nothing', 'I did nothing successfully!');
  }
}
 

Esta tarefa, com toda a certeza não é muito, mas demonstra os primeiros conceitos básicos:

Você pode brincar um pouco com ele:

$ php symfony help project:do-nothing
$ php symfony project:do-nothing

Alguma interação com a linha de comando

Argumentos e opções são a forma de fornecer parâmetros para a tarefa.

$ php symfony project:hello-world --name="Romain"

Aqui nós estamos executando a tarefa project:hello-world com a opção name definida para Romain

$ php symfony project:hello-world Hi

Agora, nós executaremos a mesma tarefa com o primeiro argumento definido como Hi.

Opções e argumentos podem possuir valores default, serem opcionais ou obrigatórios e incorporar seu propósito para ser exibido na sintaxe da tarefa.

Vamos escrever nossa tarefa project:hello-world:

class doHelloWorldTask extends sfBaseTask
{
  protected function configure()
  {
    $this->addArgument('verb', sfCommandArgument::OPTIONAL, 'Customize the verb used to say hello', 'hello');
    $this->addOption('name', null, sfCommandOption::PARAMETER_OPTIONAL, 'Customize the person to say hello to', 'world');
 
    $this->namespace        = 'project';
    $this->name             = 'hello-world';
    $this->briefDescription = 'Spread the (hello) world';
 
    $this->detailedDescription = <<<EOF
Runs an evolved hello world display, with customisable name and word.
EOF;
  }
 
  protected function execute($arguments = array(), $options = array())
  {
    $this->logSection('do', ucfirst($arguments['verb']).' '.ucfirst($options['name']));
  }
}
 

Agora, verifique como o symfony ajudará o usuário perdido sobre como utilizar a nossa nova tarefa:

$ php symfony project:hello-world invalid arguments given
$ php symfony help project:hello-world

E brincando um pouco com a tarefa:

$ php symfony project:hello-world
$ php symfony project:hello-world --name="romain"
$ php symfony project:hello-world --name=romain hi
$ php symfony project:hello-world hi --name=romain

Outras características úteis

O que você achou? Não é esta uma cereja sobre o bolo, ou por exemplo, algum coro de Jazz no symfony?

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the user mailing-list or to the forum.