sfSymfonyPluginManager.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfSymfonyPluginManager allows you to manage symfony plugins installation and uninstallation.
  11. *
  12. * @package symfony
  13. * @subpackage plugin
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfSymfonyPluginManager.class.php 12847 2008-11-09 19:01:49Z FabianLange $
  16. */
  17. class sfSymfonyPluginManager extends sfPluginManager
  18. {
  19. /**
  20. * Initializes this sfPluginManager instance.
  21. *
  22. * Available options:
  23. *
  24. * * web_dir: The directory where to plugins assets (images, stylesheets, javascripts, ...)
  25. *
  26. * See sfPluginManager for other options.
  27. *
  28. * @param sfEventDispatcher $dispatcher An event dispatcher instance
  29. * @param sfPearEnvironment $environment A sfPearEnvironment instance
  30. */
  31. public function initialize(sfEventDispatcher $dispatcher, sfPearEnvironment $environment)
  32. {
  33. parent::initialize($dispatcher, $environment);
  34. if (!$environment->getOption('web_dir'))
  35. {
  36. throw new sfPluginException('You must provide a "web_dir" option.');
  37. }
  38. }
  39. /**
  40. * Configures this plugin manager.
  41. */
  42. public function configure()
  43. {
  44. // register symfony channel
  45. $this->environment->registerChannel('pear.symfony-project.com', true);
  46. // register symfony plugins channel
  47. $this->environment->registerChannel('plugins.symfony-project.org', true);
  48. // register symfony for dependencies
  49. $this->registerSymfonyPackage();
  50. // register callbacks to manage web content
  51. $this->dispatcher->connect('plugin.post_install', array($this, 'ListenToPluginPostInstall'));
  52. $this->dispatcher->connect('plugin.pre_uninstall', array($this, 'ListenToPluginPostUninstall'));
  53. }
  54. /**
  55. * Installs web content for a plugin.
  56. *
  57. * @param string $plugin The plugin name
  58. */
  59. public function installWebContent($plugin, $sourceDirectory)
  60. {
  61. $webDir = $sourceDirectory.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'web';
  62. if (is_dir($webDir))
  63. {
  64. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Installing web data for plugin')));
  65. $filesystem = new sfFilesystem();
  66. $filesystem->relativeSymlink($webDir, $this->environment->getOption('web_dir').DIRECTORY_SEPARATOR.$plugin, true);
  67. }
  68. }
  69. /**
  70. * Unnstalls web content for a plugin.
  71. *
  72. * @param string $plugin The plugin name
  73. */
  74. public function uninstallWebContent($plugin)
  75. {
  76. $targetDir = $this->environment->getOption('web_dir').DIRECTORY_SEPARATOR.$plugin;
  77. if (is_dir($targetDir))
  78. {
  79. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Uninstalling web data for plugin')));
  80. $filesystem = new sfFilesystem();
  81. if (is_link($targetDir))
  82. {
  83. $filesystem->remove($targetDir);
  84. }
  85. else
  86. {
  87. $filesystem->remove(sfFinder::type('any')->in($targetDir));
  88. $filesystem->remove($targetDir);
  89. }
  90. }
  91. }
  92. /**
  93. * Listens to the plugin.post_install event.
  94. *
  95. * @param sfEvent $event An sfEvent instance
  96. */
  97. public function ListenToPluginPostInstall($event)
  98. {
  99. $this->installWebContent($event['plugin'],
  100. isset($event['plugin_dir']) ? $event['plugin_dir'] : $this->environment->getOption('plugin_dir'));
  101. }
  102. /**
  103. * Listens to the plugin.post_uninstall event.
  104. *
  105. * @param sfEvent $event An sfEvent instance
  106. */
  107. public function ListenToPluginPostUninstall($event)
  108. {
  109. $this->uninstallWebContent($event['plugin']);
  110. }
  111. /**
  112. * Registers the symfony package for the current version.
  113. */
  114. protected function registerSymfonyPackage()
  115. {
  116. $symfony = new PEAR_PackageFile_v2_rw();
  117. $symfony->setPackage('symfony');
  118. $symfony->setChannel('pear.symfony-project.com');
  119. $symfony->setConfig($this->environment->getConfig());
  120. $symfony->setPackageType('php');
  121. $symfony->setAPIVersion('1.1.0');
  122. $symfony->setAPIStability('stable');
  123. $symfony->setReleaseVersion(preg_replace('/\-\w+$/', '', SYMFONY_VERSION));
  124. $symfony->setReleaseStability(false === strpos(SYMFONY_VERSION, 'DEV') ? 'stable' : 'beta');
  125. $symfony->setDate(date('Y-m-d'));
  126. $symfony->setDescription('symfony');
  127. $symfony->setSummary('symfony');
  128. $symfony->setLicense('MIT License');
  129. $symfony->clearContents();
  130. $symfony->resetFilelist();
  131. $symfony->addMaintainer('lead', 'fabpot', 'Fabien Potencier', 'fabien.potencier@symfony-project.com');
  132. $symfony->setNotes('-');
  133. $symfony->setPearinstallerDep('1.4.3');
  134. $symfony->setPhpDep('5.1.0');
  135. $this->environment->getRegistry()->deletePackage('symfony', 'pear.symfony-project.com');
  136. if (!$this->environment->getRegistry()->addPackage2($symfony))
  137. {
  138. throw new sfPluginException('Unable to register the symfony package');
  139. }
  140. }
  141. /**
  142. * Returns true if the plugin is comptatible with the dependency.
  143. *
  144. * @param array $dependency A dependency array
  145. *
  146. * @return Boolean true if the plugin is compatible, false otherwise
  147. */
  148. protected function isPluginCompatibleWithDependency($dependency)
  149. {
  150. if (isset($dependency['channel']) && 'symfony' == $dependency['name'] && 'pear.symfony-project.com' == $dependency['channel'])
  151. {
  152. return $this->checkDependency($dependency);
  153. }
  154. return true;
  155. }
  156. }