Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 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
 * sfPluginConfiguration represents a configuration for a symfony plugin.
13
 *
14
 * @package    symfony
15
 * @subpackage config
16
 * @author     Kris Wallsmith <kris.wallsmith@symfony-project.com>
17
 * @version    SVN: $Id: sfPluginConfiguration.class.php 23822 2009-11-12 15:13:48Z Kris.Wallsmith $
18
 */
19
abstract class sfPluginConfiguration
20
{
21
  protected
22
    $configuration = null,
23
    $dispatcher    = null,
24
    $name          = null,
25
    $rootDir       = null;
26
 
27
  /**
28
   * Constructor.
29
   *
30
   * @param sfProjectConfiguration $configuration The project configuration
31
   * @param string                 $rootDir       The plugin root directory
32
   * @param string                 $name          The plugin name
33
   */
34
  public function __construct(sfProjectConfiguration $configuration, $rootDir = null, $name = null)
35
  {
36
    $this->configuration = $configuration;
37
    $this->dispatcher = $configuration->getEventDispatcher();
38
    $this->rootDir = null === $rootDir ? $this->guessRootDir() : realpath($rootDir);
39
    $this->name = null === $name ? $this->guessName() : $name;
40
 
41
    $this->setup();
42
    $this->configure();
43
 
44
    if (!$this->configuration instanceof sfApplicationConfiguration)
45
    {
46
      $this->initializeAutoload();
47
      $this->initialize();
48
    }
49
  }
50
 
51
  /**
52
   * Sets up the plugin.
53
   *
54
   * This method can be used when creating a base plugin configuration class for other plugins to extend.
55
   */
56
  public function setup()
57
  {
58
  }
59
 
60
  /**
61
   * Configures the plugin.
62
   *
63
   * This method is called before the plugin's classes have been added to sfAutoload.
64
   */
65
  public function configure()
66
  {
67
  }
68
 
69
  /**
70
   * Initializes the plugin.
71
   *
72
   * This method is called after the plugin's classes have been added to sfAutoload.
73
   *
74
   * @return boolean|null If false sfApplicationConfiguration will look for a config.php (maintains BC with symfony < 1.2)
75
   */
76
  public function initialize()
77
  {
78
  }
79
 
80
  /**
81
   * Returns the plugin root directory.
82
   *
83
   * @return string
84
   */
85
  public function getRootDir()
86
  {
87
    return $this->rootDir;
88
  }
89
 
90
  /**
91
   * Returns the plugin name.
92
   *
93
   * @return string
94
   */
95
  public function getName()
96
  {
97
    return $this->name;
98
  }
99
 
100
  /**
101
   * Initializes autoloading for the plugin.
102
   *
103
   * This method is called when a plugin is initialized in a project
104
   * configuration. Otherwise, autoload is handled in
105
   * {@link sfApplicationConfiguration} using {@link sfAutoload}.
106
   *
107
   * @see sfSimpleAutoload
108
   */
109
  public function initializeAutoload()
110
  {
111
    $autoload = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
112
 
113
    if (is_readable($file = $this->rootDir.'/config/autoload.yml'))
114
    {
115
      $this->configuration->getEventDispatcher()->connect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
116
      $autoload->loadConfiguration(array($file));
117
      $this->configuration->getEventDispatcher()->disconnect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
118
    }
119
    else
120
    {
121
      $autoload->addDirectory($this->rootDir.'/lib');
122
    }
123
 
124
    $autoload->register();
125
  }
126
 
127
  /**
128
   * Filters sfAutoload configuration values.
129
   *
130
   * @param sfEvent $event
131
   * @param array   $config
132
   *
133
   * @return array
134
   */
135
  public function filterAutoloadConfig(sfEvent $event, array $config)
136
  {
137
    // use array_merge so config is added to the front of the autoload array
138
    if (!isset($config['autoload'][$this->name.'_lib']))
139
    {
140
      $config['autoload'] = array_merge(array(
141
        $this->name.'_lib' => array(
142
          'path'      => $this->rootDir.'/lib',
143
          'recursive' => true,
144
        ),
145
      ), $config['autoload']);
146
    }
147
 
148
    if (!isset($config['autoload'][$this->name.'_module_libs']))
149
    {
150
      $config['autoload'] = array_merge(array(
151
        $this->name.'_module_libs' => array(
152
          'path'      => $this->rootDir.'/modules/*/lib',
153
          'recursive' => true,
154
          'prefix'    => 1,
155
        ),
156
      ), $config['autoload']);
157
    }
158
 
159
    return $config;
160
  }
161
 
162
  /**
163
   * Connects the current plugin's tests to the "test:*" tasks.
164
   */
165
  public function connectTests()
166
  {
167
    $this->dispatcher->connect('task.test.filter_test_files', array($this, 'filterTestFiles'));
168
  }
169
 
170
  /**
171
   * Listens for the "task.test.filter_test_files" event and adds tests from the current plugin.
172
   *
173
   * @param  sfEvent $event
174
   * @param  array   $files
175
   *
176
   * @return array An array of files with the appropriate tests from the current plugin merged in
177
   */
178
  public function filterTestFiles(sfEvent $event, $files)
179
  {
180
    $task = $event->getSubject();
181
 
182
    if ($task instanceof sfTestAllTask)
183
    {
184
      $directory = $this->rootDir.'/test';
185
      $names = array();
186
    }
187
    else if ($task instanceof sfTestFunctionalTask)
188
    {
189
      $directory = $this->rootDir.'/test/functional';
190
      $names = $event['arguments']['controller'];
191
    }
192
    else if ($task instanceof sfTestUnitTask)
193
    {
194
      $directory = $this->rootDir.'/test/unit';
195
      $names = $event['arguments']['name'];
196
    }
197
 
198
    if (!count($names))
199
    {
200
      $names = array('*');
201
    }
202
 
203
    foreach ($names as $name)
204
    {
205
      $finder = sfFinder::type('file')->follow_link()->name(basename($name).'Test.php');
206
      $files = array_merge($files, $finder->in($directory.'/'.dirname($name)));
207
    }
208
 
209
    return array_unique($files);
210
  }
211
 
212
  /**
213
   * Guesses the plugin root directory.
214
   *
215
   * @return string
216
   */
217
  protected function guessRootDir()
218
  {
219
    $r = new ReflectionClass(get_class($this));
220
    return realpath(dirname($r->getFilename()).'/..');
221
  }
222
 
223
  /**
224
   * Guesses the plugin name.
225
   *
226
   * @return string
227
   */
228
  protected function guessName()
229
  {
230
    return substr(get_class($this), 0, -13);
231
  }
232
}