Development

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

You must first sign up to be able to contribute.

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

Revision 16171, 5.6 kB (checked in by fabien, 6 years ago)

[1.1, 1.2, 1.3] fixed command.filter_options doesn't filter anything (closes #6038)

  • 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') : ($commandManager->getOptionSet()->hasOption('application') ? $commandManager->getOptionValue('application') : null);
44     $env = $commandManager->getOptionSet()->hasOption('env') ? $commandManager->getOptionValue('env') : 'test';
45
46     if (true === $application)
47     {
48       $application = $this->getFirstApplication();
49
50       if ($commandManager->getOptionSet()->hasOption('application'))
51       {
52         $commandManager->setOption($commandManager->getOptionSet()->getOption('application'), $application);
53       }
54     }
55
56     $this->configuration = $this->createConfiguration($application, $env);
57
58     if (!is_null($this->commandApplication) && !$this->commandApplication->withTrace())
59     {
60       sfConfig::set('sf_logging_enabled', false);
61     }
62
63     $ret = $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues());
64
65     $this->dispatcher->notify(new sfEvent($this, 'command.post_command'));
66
67     return $ret;
68   }
69
70   /**
71    * Returns the filesystem instance.
72    *
73    * @return sfFilesystem A sfFilesystem instance
74    */
75   public function getFilesystem()
76   {
77     if (!isset($this->filesystem))
78     {
79       if (is_null($this->commandApplication) || $this->commandApplication->isVerbose())
80       {
81         $this->filesystem = new sfFilesystem($this->dispatcher, $this->formatter);
82       }
83       else
84       {
85         $this->filesystem = new sfFilesystem();
86       }
87     }
88
89     return $this->filesystem;
90   }
91
92   /**
93    * Checks if the current directory is a symfony project directory.
94    *
95    * @return true if the current directory is a symfony project directory, false otherwise
96    */
97   public function checkProjectExists()
98   {
99     if (!file_exists('symfony'))
100     {
101       throw new sfException('You must be in a symfony project directory.');
102     }
103   }
104
105   /**
106    * Checks if an application exists.
107    *
108    * @param  string $app  The application name
109    *
110    * @return bool true if the application exists, false otherwise
111    */
112   public function checkAppExists($app)
113   {
114     if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app))
115     {
116       throw new sfException(sprintf('Application "%s" does not exist', $app));
117     }
118   }
119
120   /**
121    * Checks if a module exists.
122    *
123    * @param  string $app     The application name
124    * @param  string $module  The module name
125    *
126    * @return bool true if the module exists, false otherwise
127    */
128   public function checkModuleExists($app, $module)
129   {
130     if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app.'/modules/'.$module))
131     {
132       throw new sfException(sprintf('Module "%s/%s" does not exist.', $app, $module));
133     }
134   }
135
136   /**
137    * Creates a configuration object.
138    *
139    * @param string  $application The application name
140    * @param string  $env         The environment name
141    *
142    * @return sfProjectConfiguration A sfProjectConfiguration instance
143    */
144   protected function createConfiguration($application, $env)
145   {
146     if (!is_null($application))
147     {
148       $this->checkAppExists($application);
149
150       require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
151
152       $configuration = ProjectConfiguration::getApplicationConfiguration($application, $env, true, null, $this->dispatcher);
153     }
154     else
155     {
156       if (file_exists(sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php'))
157       {
158         require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
159         $configuration = new ProjectConfiguration(null, $this->dispatcher);
160       }
161       else
162       {
163         $configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
164       }
165
166       if (!is_null($env))
167       {
168         sfConfig::set('sf_environment', $env);
169       }
170
171       $autoloader = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
172       $autoloader->addFiles(sfFinder::type('file')->prune('symfony')->follow_link()->name('*.php')->in(sfConfig::get('sf_lib_dir')));
173       $autoloader->register();
174     }
175
176     return $configuration;
177   }
178
179   /**
180    * Returns the first application in apps.
181    *
182    * @return string The Application name
183    */
184   protected function getFirstApplication()
185   {
186     if (count($dirs = sfFinder::type('dir')->ignore_version_control()->maxdepth(0)->follow_link()->relative()->in(sfConfig::get('sf_apps_dir'))))
187     {
188       return $dirs[0];
189     }
190
191     return null;
192   }
193 }
194
Note: See TracBrowser for help on using the browser.