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
 * sfSymfonyPluginManager allows you to manage symfony plugins installation and uninstallation.
13
 *
14
 * @package    symfony
15
 * @subpackage plugin
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfSymfonyPluginManager.class.php 25218 2009-12-10 20:06:45Z Jonathan.Wage $
18
 */
19
class sfSymfonyPluginManager extends sfPluginManager
20
{
21
  /**
22
   * Initializes this sfPluginManager instance.
23
   *
24
   * Available options:
25
   *
26
   * * web_dir: The directory where to plugins assets (images, stylesheets, javascripts, ...)
27
   *
28
   * See sfPluginManager for other options.
29
   *
30
   * @param sfEventDispatcher $dispatcher   An event dispatcher instance
31
   * @param sfPearEnvironment $environment  A sfPearEnvironment instance
32
   */
33
  public function initialize(sfEventDispatcher $dispatcher, sfPearEnvironment $environment)
34
  {
35
    parent::initialize($dispatcher, $environment);
36
 
37
    if (!$environment->getOption('web_dir'))
38
    {
39
      throw new sfPluginException('You must provide a "web_dir" option.');
40
    }
41
  }
42
 
43
  /**
44
   * Configures this plugin manager.
45
   */
46
  public function configure()
47
  {
48
    // register symfony channel
49
    $this->environment->registerChannel('pear.symfony-project.com', true);
50
 
51
    // register symfony plugins channel
52
    $this->environment->registerChannel('plugins.symfony-project.org', true);
53
 
54
    // register symfony for dependencies
55
    $this->registerSymfonyPackage();
56
 
57
    // register callbacks to manage web content
58
    $this->dispatcher->connect('plugin.post_install',  array($this, 'listenToPluginPostInstall'));
59
    $this->dispatcher->connect('plugin.post_uninstall', array($this, 'listenToPluginPostUninstall'));
60
  }
61
 
62
  /**
63
   * Installs web content for a plugin.
64
   *
65
   * @param string $plugin The plugin name
66
   */
67
  public function installWebContent($plugin, $sourceDirectory)
68
  {
69
    $webDir = $sourceDirectory.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'web';
70
    if (is_dir($webDir))
71
    {
72
      $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Installing web data for plugin')));
73
 
74
      $filesystem = new sfFilesystem();
75
      $filesystem->relativeSymlink($webDir, $this->environment->getOption('web_dir').DIRECTORY_SEPARATOR.$plugin, true);
76
    }
77
  }
78
 
79
  /**
80
   * Unnstalls web content for a plugin.
81
   *
82
   * @param string $plugin The plugin name
83
   */
84
  public function uninstallWebContent($plugin)
85
  {
86
    $targetDir = $this->environment->getOption('web_dir').DIRECTORY_SEPARATOR.$plugin;
87
    if (is_dir($targetDir))
88
    {
89
      $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Uninstalling web data for plugin')));
90
 
91
      $filesystem = new sfFilesystem();
92
 
93
      if (is_link($targetDir))
94
      {
95
        $filesystem->remove($targetDir);
96
      }
97
      else
98
      {
99
        $filesystem->remove(sfFinder::type('any')->in($targetDir));
100
        $filesystem->remove($targetDir);
101
      }
102
    }
103
  }
104
 
105
  /**
106
   * Enables a plugin in the ProjectConfiguration class.
107
   *
108
   * This is a static method that does not rely on the PEAR environment
109
   * as we don't want this method to have PEAR as a dependency.
110
   *
111
   * @param string $plugin    The name of the plugin
112
   * @param string $configDir The config directory
113
   */
114
  static public function enablePlugin($plugin, $configDir)
115
  {
116
    if (!$configDir)
117
    {
118
      throw new sfPluginException('You must provide a "config_dir" option.');
119
    }
120
 
121
    $manipulator = sfClassManipulator::fromFile($configDir.'/ProjectConfiguration.class.php');
122
    $manipulator->wrapMethod('setup', '', sprintf('$this->enablePlugins(\'%s\');', $plugin));
123
    $manipulator->save();
124
  }
125
 
126
  /**
127
   * Disables a plugin in the ProjectConfiguration class.
128
   *
129
   * This is a static method that does not rely on the PEAR environment
130
   * as we don't want this method to have PEAR as a dependency.
131
   *
132
   * @param string $plugin The name of the plugin
133
   * @param string $configDir The config directory
134
   */
135
  static public function disablePlugin($plugin, $configDir)
136
  {
137
    if (!$configDir)
138
    {
139
      throw new sfPluginException('You must provide a "config_dir" option.');
140
    }
141
 
142
    $file = $configDir.'/ProjectConfiguration.class.php';
143
    $source = file_get_contents($file);
144
 
145
    $source = preg_replace(sprintf('# *\$this\->enablePlugins\(array\(([^\)]+), *\'%s\'([^\)]*)\)\)#', $plugin), '$this->enablePlugins(array($1$2))', $source);
146
    $source = preg_replace(sprintf('# *\$this\->enablePlugins\(array\(\'%s\', *([^\)]*)\)\)#', $plugin), '$this->enablePlugins(array($1))', $source);
147
    $source = preg_replace(sprintf('# *\$this\->enablePlugins\(\'%s\'\); *\n?#', $plugin), '', $source);
148
    $source = preg_replace(sprintf('# *\$this\->enablePlugins\(array\(\'%s\'\)\); *\n?#', $plugin), '', $source);
149
    $source = preg_replace(sprintf('# *\$this\->enablePlugins\(array\(\)\); *\n?#', $plugin), '', $source);
150
 
151
    file_put_contents($file, $source);
152
  }
153
 
154
  /**
155
   * Listens to the plugin.post_install event.
156
   *
157
   * @param sfEvent $event An sfEvent instance
158
   */
159
  public function listenToPluginPostInstall($event)
160
  {
161
    $this->installWebContent($event['plugin'], isset($event['plugin_dir']) ? $event['plugin_dir'] : $this->environment->getOption('plugin_dir'));
162
 
163
    $this->enablePlugin($event['plugin'], $this->environment->getOption('config_dir'));
164
  }
165
 
166
  /**
167
   * Listens to the plugin.post_uninstall event.
168
   *
169
   * @param sfEvent $event An sfEvent instance
170
   */
171
  public function listenToPluginPostUninstall($event)
172
  {
173
    $this->uninstallWebContent($event['plugin']);
174
 
175
    $this->disablePlugin($event['plugin'], $this->environment->getOption('config_dir'));
176
  }
177
 
178
  /**
179
   * Registers the symfony package for the current version.
180
   */
181
  protected function registerSymfonyPackage()
182
  {
183
    $symfony = new PEAR_PackageFile_v2_rw();
184
    $symfony->setPackage('symfony');
185
    $symfony->setChannel('pear.symfony-project.com');
186
    $symfony->setConfig($this->environment->getConfig());
187
    $symfony->setPackageType('php');
188
    $symfony->setAPIVersion(preg_replace('/\d+(\-\w+)?$/', '0', SYMFONY_VERSION));
189
    $symfony->setAPIStability(false === strpos(SYMFONY_VERSION, 'DEV') ? 'stable' : 'beta');
190
    $symfony->setReleaseVersion(preg_replace('/\-\w+$/', '', SYMFONY_VERSION));
191
    $symfony->setReleaseStability(false === strpos(SYMFONY_VERSION, 'DEV') ? 'stable' : 'beta');
192
    $symfony->setDate(date('Y-m-d'));
193
    $symfony->setDescription('symfony');
194
    $symfony->setSummary('symfony');
195
    $symfony->setLicense('MIT License');
196
    $symfony->clearContents();
197
    $symfony->resetFilelist();
198
    $symfony->addMaintainer('lead', 'fabpot', 'Fabien Potencier', 'fabien.potencier@symfony-project.com');
199
    $symfony->setNotes('-');
200
    $symfony->setPearinstallerDep('1.4.3');
201
    $symfony->setPhpDep('5.2.4');
202
 
203
    $this->environment->getRegistry()->deletePackage('symfony', 'pear.symfony-project.com');
204
    if (!$this->environment->getRegistry()->addPackage2($symfony))
205
    {
206
      throw new sfPluginException('Unable to register the symfony package');
207
    }
208
  }
209
 
210
  /**
211
   * Returns true if the plugin is comptatible with the dependency.
212
   *
213
   * @param  array   $dependency A dependency array
214
   *
215
   * @return Boolean true if the plugin is compatible, false otherwise
216
   */
217
  protected function isPluginCompatibleWithDependency($dependency)
218
  {
219
    if (isset($dependency['channel']) && 'symfony' == $dependency['name'] && 'pear.symfony-project.com' == $dependency['channel'])
220
    {
221
      return $this->checkDependency($dependency);
222
    }
223
 
224
    return true;
225
  }
226
}