sfPluginConfiguration.class.php 5.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php

/*
 * This file is part of the symfony package.
 * (c) 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.
 */

/**
 * sfPluginConfiguration represents a configuration for a symfony plugin.
 * 
 * @package    symfony
 * @subpackage config
 * @author     Kris Wallsmith <kris.wallsmith@symfony-project.com>
 * @version    SVN: $Id: sfPluginConfiguration.class.php 23822 2009-11-12 15:13:48Z Kris.Wallsmith $
 */
abstract class sfPluginConfiguration
{
  protected
    $configuration = null,
    $dispatcher    = null,
    $name          = null,
    $rootDir       = null;

  /**
   * Constructor.
   * 
   * @param sfProjectConfiguration $configuration The project configuration
   * @param string                 $rootDir       The plugin root directory
   * @param string                 $name          The plugin name
   */
  public function __construct(sfProjectConfiguration $configuration, $rootDir = null, $name = null)
  {
    $this->configuration = $configuration;
    $this->dispatcher = $configuration->getEventDispatcher();
    $this->rootDir = null === $rootDir ? $this->guessRootDir() : realpath($rootDir);
    $this->name = null === $name ? $this->guessName() : $name;

    $this->setup();
    $this->configure();

    if (!$this->configuration instanceof sfApplicationConfiguration)
    {
      $this->initializeAutoload();
      $this->initialize();
    }
  }

  /**
   * Sets up the plugin.
   * 
   * This method can be used when creating a base plugin configuration class for other plugins to extend.
   */
  public function setup()
  {
  }

  /**
   * Configures the plugin.
   * 
   * This method is called before the plugin's classes have been added to sfAutoload.
   */
  public function configure()
  {
  }

  /**
   * Initializes the plugin.
   * 
   * This method is called after the plugin's classes have been added to sfAutoload.
   * 
   * @return boolean|null If false sfApplicationConfiguration will look for a config.php (maintains BC with symfony < 1.2)
   */
  public function initialize()
  {
  }

  /**
   * Returns the plugin root directory.
   * 
   * @return string
   */
  public function getRootDir()
  {
    return $this->rootDir;
  }

  /**
   * Returns the plugin name.
   * 
   * @return string
   */
  public function getName()
  {
    return $this->name;
  }

  /**
   * Initializes autoloading for the plugin.
   * 
   * This method is called when a plugin is initialized in a project
   * configuration. Otherwise, autoload is handled in
   * {@link sfApplicationConfiguration} using {@link sfAutoload}.
   * 
   * @see sfSimpleAutoload
   */
  public function initializeAutoload()
  {
    $autoload = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');

    if (is_readable($file = $this->rootDir.'/config/autoload.yml'))
    {
      $this->configuration->getEventDispatcher()->connect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
      $autoload->loadConfiguration(array($file));
      $this->configuration->getEventDispatcher()->disconnect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
    }
    else
    {
      $autoload->addDirectory($this->rootDir.'/lib');
    }

    $autoload->register();
  }

  /**
   * Filters sfAutoload configuration values.
   * 
   * @param sfEvent $event  
   * @param array   $config 
   * 
   * @return array
   */
  public function filterAutoloadConfig(sfEvent $event, array $config)
  {
    // use array_merge so config is added to the front of the autoload array
    if (!isset($config['autoload'][$this->name.'_lib']))
    {
      $config['autoload'] = array_merge(array(
        $this->name.'_lib' => array(
          'path'      => $this->rootDir.'/lib',
          'recursive' => true,
        ),
      ), $config['autoload']);
    }

    if (!isset($config['autoload'][$this->name.'_module_libs']))
    {
      $config['autoload'] = array_merge(array(
        $this->name.'_module_libs' => array(
          'path'      => $this->rootDir.'/modules/*/lib',
          'recursive' => true,
          'prefix'    => 1,
        ),
      ), $config['autoload']);
    }

    return $config;
  }

  /**
   * Connects the current plugin's tests to the "test:*" tasks.
   */
  public function connectTests()
  {
    $this->dispatcher->connect('task.test.filter_test_files', array($this, 'filterTestFiles'));
  }

  /**
   * Listens for the "task.test.filter_test_files" event and adds tests from the current plugin.
   * 
   * @param  sfEvent $event
   * @param  array   $files
   * 
   * @return array An array of files with the appropriate tests from the current plugin merged in
   */
  public function filterTestFiles(sfEvent $event, $files)
  {
    $task = $event->getSubject();

    if ($task instanceof sfTestAllTask)
    {
      $directory = $this->rootDir.'/test';
      $names = array();
    }
    else if ($task instanceof sfTestFunctionalTask)
    {
      $directory = $this->rootDir.'/test/functional';
      $names = $event['arguments']['controller'];
    }
    else if ($task instanceof sfTestUnitTask)
    {
      $directory = $this->rootDir.'/test/unit';
      $names = $event['arguments']['name'];
    }

    if (!count($names))
    {
      $names = array('*');
    }

    foreach ($names as $name)
    {
      $finder = sfFinder::type('file')->follow_link()->name(basename($name).'Test.php');
      $files = array_merge($files, $finder->in($directory.'/'.dirname($name)));
    }

    return array_unique($files);
  }

  /**
   * Guesses the plugin root directory.
   * 
   * @return string
   */
  protected function guessRootDir()
  {
    $r = new ReflectionClass(get_class($this));
    return realpath(dirname($r->getFilename()).'/..');
  }

  /**
   * Guesses the plugin name.
   * 
   * @return string
   */
  protected function guessName()
  {
    return substr(get_class($this), 0, -13);
  }
}