Development

/branches/1.1/lib/task/sfBaseTask.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/task/sfBaseTask.class.php

Revision 16558, 4.3 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.1] backported r13413 (closes #4911)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Base class for all symfony tasks.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 abstract class sfBaseTask extends sfCommandApplicationTask
20 {
21   protected
22     $configuration = null;
23
24   /**
25    * @see sfTask
26    */
27   protected function doRun(sfCommandManager $commandManager, $options)
28   {
29     $event = $this->dispatcher->filter(new sfEvent($this, 'command.filter_options', array('command_manager' => $commandManager)), $options);
30     $options = $event->getReturnValue();
31
32     $this->process($commandManager, $options);
33
34     $event = new sfEvent($this, 'command.pre_command', array('arguments' => $commandManager->getArgumentValues(), 'options' => $commandManager->getOptionValues()));
35     $this->dispatcher->notifyUntil($event);
36     if ($event->isProcessed())
37     {
38       return $event->getReturnValue();
39     }
40
41     $this->checkProjectExists();
42
43     $application = $commandManager->getArgumentSet()->hasArgument('application') ? $commandManager->getArgumentValue('application') : null;
44     $env         = $commandManager->getOptionSet()->hasOption('env') ? $commandManager->getOptionValue('env') : 'test';
45     if (!is_null($application))
46     {
47       $this->checkAppExists($application);
48
49       require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
50
51       $this->configuration = ProjectConfiguration::getApplicationConfiguration($application, $env, true, null, $this->dispatcher);
52     }
53     else
54     {
55       if (file_exists(sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php'))
56       {
57         require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
58         $this->configuration = new ProjectConfiguration(null, $this->dispatcher);
59       }
60       else
61       {
62         $this->configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
63       }
64     }
65
66     $autoloader = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
67     foreach ($this->configuration->getModelDirs() as $dir)
68     {
69       $autoloader->addDirectory($dir);
70     }
71
72     if (!is_null($this->commandApplication) && !$this->commandApplication->withTrace())
73     {
74       sfConfig::set('sf_logging_enabled', false);
75     }
76
77     $ret = $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues());
78
79     $this->dispatcher->notify(new sfEvent($this, 'command.post_command'));
80
81     return $ret;
82   }
83
84   /**
85    * Returns the filesystem instance.
86    *
87    * @return sfFilesystem A sfFilesystem instance
88    */
89   public function getFilesystem()
90   {
91     if (!isset($this->filesystem))
92     {
93       if (is_null($this->commandApplication) || $this->commandApplication->isVerbose())
94       {
95         $this->filesystem = new sfFilesystem($this->dispatcher, $this->formatter);
96       }
97       else
98       {
99         $this->filesystem = new sfFilesystem();
100       }
101     }
102
103     return $this->filesystem;
104   }
105
106   /**
107    * Checks if the current directory is a symfony project directory.
108    *
109    * @return true if the current directory is a symfony project directory, false otherwise
110    */
111   public function checkProjectExists()
112   {
113     if (!file_exists('symfony'))
114     {
115       throw new sfException('You must be in a symfony project directory.');
116     }
117   }
118
119   /**
120    * Checks if an application exists.
121    *
122    * @param  string $app  The application name
123    *
124    * @return bool true if the application exists, false otherwise
125    */
126   public function checkAppExists($app)
127   {
128     if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app))
129     {
130       throw new sfException(sprintf('Application "%s" does not exist', $app));
131     }
132   }
133
134   /**
135    * Checks if a module exists.
136    *
137    * @param  string $app     The application name
138    * @param  string $module  The module name
139    *
140    * @return bool true if the module exists, false otherwise
141    */
142   public function checkModuleExists($app, $module)
143   {
144     if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app.'/modules/'.$module))
145     {
146       throw new sfException(sprintf('Module "%s/%s" does not exist.', $app, $module));
147     }
148   }
149 }
150
Note: See TracBrowser for help on using the browser.