sfSymfonyCommandApplication.class.php 3.87 KB
Newer Older
Игорь's avatar
init    
Игорь committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfSymfonyCommandApplication manages the symfony CLI.
 *
 * @package    symfony
 * @subpackage command
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfSymfonyCommandApplication.class.php 20053 2009-07-09 12:49:20Z nicolas $
 */
class sfSymfonyCommandApplication extends sfCommandApplication
{
  protected $taskFiles = array();
  
  /**
   * Configures the current symfony command application.
   */
  public function configure()
  {
    if (!isset($this->options['symfony_lib_dir']))
    {
      throw new sfInitializationException('You must pass a "symfony_lib_dir" option.');
    }
    
    $configurationFile = getcwd().'/config/ProjectConfiguration.class.php';
    
    
    
    if (is_readable($configurationFile))
    {
      require_once $configurationFile;
      $configuration = new ProjectConfiguration(getcwd(), $this->dispatcher);
    }
    else
    {
      $configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
    }

    // application
    $this->setName('symfony');
    $this->setVersion(SYMFONY_VERSION);
    
    $this->loadTasks($configuration);
    
  }

  /**
   * Runs the current application.
   *
   * @param mixed $options The command line options
   *
   * @return integer 0 if everything went fine, or an error code
   */
  public function run($options = null)
  {
    $this->handleOptions($options);
    $arguments = $this->commandManager->getArgumentValues();

    if (!isset($arguments['task']))
    {
      $arguments['task'] = 'list';
      $this->commandOptions .= $arguments['task'];
    }

    $this->currentTask = $this->getTaskToExecute($arguments['task']);

    if ($this->currentTask instanceof sfCommandApplicationTask)
    {
      $this->currentTask->setCommandApplication($this);
    }

    $ret = $this->currentTask->runFromCLI($this->commandManager, $this->commandOptions);

    $this->currentTask = null;

    return $ret;
  }

  /**
   * Loads all available tasks.
   *
   * Looks for tasks in the symfony core, the current project and all project plugins.
   *
   * @param sfProjectConfiguration $configuration The project configuration
   */
  public function loadTasks(sfProjectConfiguration $configuration)
  {
    // Symfony core tasks
    $dirs = array(sfConfig::get('sf_symfony_lib_dir').'/task');

    // Plugin tasks
    foreach ($configuration->getPluginPaths() as $path)
    {
      if (is_dir($taskPath = $path.'/lib/task'))
      {
        $dirs[] = $taskPath;
      }
    }

    // project tasks
    $dirs[] = sfConfig::get('sf_lib_dir').'/task';

    $finder = sfFinder::type('file')->name('*Task.class.php');
    foreach ($finder->in($dirs) as $file)
    {
      $this->taskFiles[basename($file, '.class.php')] = $file;
    }

    // register local autoloader for tasks
    spl_autoload_register(array($this, 'autoloadTask'));

    // require tasks
    foreach ($this->taskFiles as $task => $file)
    {
      // forces autoloading of each task class
      class_exists($task, true);
      
    }

    // unregister local autoloader
    spl_autoload_unregister(array($this, 'autoloadTask'));
    
    
  }

  /**
   * Autoloads a task class
   *
   * @param  string  $class  The task class name
   *
   * @return Boolean
   */
  public function autoloadTask($class)
  {
    if (isset($this->taskFiles[$class]))
    {
      require_once $this->taskFiles[$class];

      return true;
    }

    return false;
  }

  /**
   * @see sfCommandApplication
   */
  public function getLongVersion()
  {
    return sprintf('%s version %s (%s)', $this->getName(), $this->formatter->format($this->getVersion(), 'INFO'), sfConfig::get('sf_symfony_lib_dir'))."\n";
  }
}