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

Debug toolbar