![]() |
|
The symfony CookbookHoe maak je een task? |
|
You are currently reading "The symfony Cookbook" which is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License 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. |
Net als iedere web applicatie zal je project een aantal terugkerende onderhoudstaken, database operaties of andere console scripts regelmatig moeten draaien.
Symfony 1.1 breid de symfony 1.0 pake tasks uit om een krachtige en uniforme command line utility voor je projecten te bieden, volledig geintegreerd met de symfony Command Line Interface (CLI).
Open je symfony 1.1 project directory en typ:
$ php symfony generate:task doNothing
Dit zal een lege task aanmaken in lib/task/doNothingTask.class.php. Laten we die eens wat gaat aanpassen.
class doNothingTask extends sfBaseTask { protected function configure() { $this->namespace = 'project'; $this->name = 'do-nothing'; $this->briefDescription = 'Does strictly nothing'; $this->detailedDescription = <<<EOF This task is completely useless, and should be run as often as possible. EOF; } protected function execute($arguments = array(), $options = array()) { $this->logSection('do-nothing', 'I did nothing successfully!'); } }
Deze taak doet zeker niet veel, maar het demonstreert de eerste basis concepten:
configure() methode beschrijft de task. Naam om aan te roepen, scope, syntax, help, opties en argumenten.execute() methode is degene die alles det, en zal worden aangeroepen wanneer de task wordt uitgevoerd.logSection() methode kan worden gebruikt om berichten naar de console output te sturen.Je kan een beetje hiermee spelen: $ php symfony help project:do-nothing $ php symfony project:do-nothing
Argumenten en opties zijn een manier om parameters aan een task mee te geven.
$ php symfony project:hello-world --name="Romain"
Hier draaien we de project:hello-world task met de name optie gezet naar Romain
$ php symfony project:hello-world Hi
Nu draaien we dezelfde task met het eerste argument gezet als Hi.
Opties en argumenten kunnen standaard waardes hebben, optioneel of verplicht zijn en hun doel tonen in de syntax van de task.
Laten we onze project:hello-world task schrijven:
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'])); } }
Kijk nu hoe symfony helpt om een verdwaalde gebruiker hoe ze onze nieuwe task moeten gebruiken:
$ php symfony project:hello-world invalid arguments given
$ php symfony help project:hello-world
En speel wat met de task:
$ 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
Heb je een database laag nodig?
Om de database manager op te halen, moeten we in een applicatie context zijn. Dit is makkelijk gedaan door het speciale application argument te gebruiken.
protected function configure() { // ... $this->addArgument('application', sfCommandArgument::REQUIRED, 'Changes the application context of the task'); // ... }
Nu is de $this->configuration class eigenschap een instantie van de sfApplicationConfiguration in plaats van de standaard sfProjectConfiguration, en kan je die gebruiken om de database manager te initialiseren:
protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); // ... }
Een task in een andere task uitvoeren?
$myOtherTask = new myOtherTask($this->dispatcher, $this->formatter); $myOtherTask->run($arguments = array('foo' => 'bar'), $options = array('far' => 'boo'));
Wil je de gebruiker laten kiezen uit een omgeving, terwijl je ook een standaard omgeving bied?
Voeg gewoon de nieuwe env optie toe in de ::configure() methode en symfony zal de waarde gebruiken als de standaard omgeving.
$this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'prod');
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 official user mailing-list.