Development

/branches/1.3/lib/command/sfSymfonyCommandApplication.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/command/sfSymfonyCommandApplication.class.php

Revision 20053, 3.8 kB (checked in by nicolas, 5 years ago)

[1.1, 1.2, 1.3] fixed autoload order, enhanced performances of tasks autoloading

  • 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  * sfSymfonyCommandApplication manages the symfony CLI.
13  *
14  * @package    symfony
15  * @subpackage command
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfSymfonyCommandApplication extends sfCommandApplication
20 {
21   protected $taskFiles = array();
22  
23   /**
24    * Configures the current symfony command application.
25    */
26   public function configure()
27   {
28     if (!isset($this->options['symfony_lib_dir']))
29     {
30       throw new sfInitializationException('You must pass a "symfony_lib_dir" option.');
31     }
32     
33     $configurationFile = getcwd().'/config/ProjectConfiguration.class.php';
34     if (is_readable($configurationFile))
35     {
36       require_once $configurationFile;
37       $configuration = new ProjectConfiguration(getcwd(), $this->dispatcher);
38     }
39     else
40     {
41       $configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
42     }
43
44     // application
45     $this->setName('symfony');
46     $this->setVersion(SYMFONY_VERSION);
47
48     $this->loadTasks($configuration);
49   }
50
51   /**
52    * Runs the current application.
53    *
54    * @param mixed $options The command line options
55    *
56    * @return integer 0 if everything went fine, or an error code
57    */
58   public function run($options = null)
59   {
60     $this->handleOptions($options);
61     $arguments = $this->commandManager->getArgumentValues();
62
63     if (!isset($arguments['task']))
64     {
65       $arguments['task'] = 'list';
66       $this->commandOptions .= $arguments['task'];
67     }
68
69     $this->currentTask = $this->getTaskToExecute($arguments['task']);
70
71     if ($this->currentTask instanceof sfCommandApplicationTask)
72     {
73       $this->currentTask->setCommandApplication($this);
74     }
75
76     $ret = $this->currentTask->runFromCLI($this->commandManager, $this->commandOptions);
77
78     $this->currentTask = null;
79
80     return $ret;
81   }
82
83   /**
84    * Loads all available tasks.
85    *
86    * Looks for tasks in the symfony core, the current project and all project plugins.
87    *
88    * @param sfProjectConfiguration $configuration The project configuration
89    */
90   public function loadTasks(sfProjectConfiguration $configuration)
91   {
92     // Symfony core tasks
93     $dirs = array(sfConfig::get('sf_symfony_lib_dir').'/task');
94
95     // Plugin tasks
96     foreach ($configuration->getPluginPaths() as $path)
97     {
98       if (is_dir($taskPath = $path.'/lib/task'))
99       {
100         $dirs[] = $taskPath;
101       }
102     }
103
104     // project tasks
105     $dirs[] = sfConfig::get('sf_lib_dir').'/task';
106
107     $finder = sfFinder::type('file')->name('*Task.class.php');
108     foreach ($finder->in($dirs) as $file)
109     {
110       $this->taskFiles[basename($file, '.class.php')] = $file;
111     }
112
113     // register local autoloader for tasks
114     spl_autoload_register(array($this, 'autoloadTask'));
115
116     // require tasks
117     foreach ($this->taskFiles as $task => $file)
118     {
119       // forces autoloading of each task class
120       class_exists($task, true);
121     }
122
123     // unregister local autoloader
124     spl_autoload_unregister(array($this, 'autoloadTask'));
125   }
126
127   /**
128    * Autoloads a task class
129    *
130    * @param  string  $class  The task class name
131    *
132    * @return Boolean
133    */
134   public function autoloadTask($class)
135   {
136     if (isset($this->taskFiles[$class]))
137     {
138       require_once $this->taskFiles[$class];
139
140       return true;
141     }
142
143     return false;
144   }
145
146   /**
147    * @see sfCommandApplication
148    */
149   public function getLongVersion()
150   {
151     return sprintf('%s version %s (%s)', $this->getName(), $this->formatter->format($this->getVersion(), 'INFO'), sfConfig::get('sf_symfony_lib_dir'))."\n";
152   }
153 }
154
Note: See TracBrowser for help on using the browser.