1. sfPearRestPlugin.class.php
  2. /** * sfPearRestPlugin interacts with a symfony plugin channel. * * @package symfony * @subpackage plugin * @author Fabien Potencier * @version SVN: $Id: sfPearRestPlugin.class.php 21908 2009-09-11 12:06:21Z fabien $ */
  3. class sfPearRestPlugin extends sfPearRest11
  4. {
  5. protected
  6. $config,
  7. $rest10,
  8. $restBase,
  9. $channel;
  10. /**
  11. * Constructs a new sfRestPlugin instance.
  12. *
  13. * @param PEAR_Config $config The PEAR Config object
  14. * @param array $options An array of options
  15. */
  16. public function __construct(PEAR_Config $config, $options = array())
  17. {
  18. parent::__construct($config, $options);
  19. $this->config = $config;
  20. $this->rest10 = new sfPearRest10($config, $options);
  21. }
  22. /**
  23. * Sets the channel for the REST object.
  24. *
  25. * @param string $channel The channel name
  26. */
  27. public function setChannel($channel)
  28. {
  29. $this->channel = $channel;
  30. $this->restBase = $this->getRESTBase($channel);
  31. }
  32. /**
  33. * Gets the REST base path.
  34. *
  35. * @param string $channelName The channel name
  36. */
  37. protected function getRESTBase($channelName)
  38. {
  39. $channel = $this->config->getRegistry()->getChannel($channelName);
  40. if (PEAR::isError($channel))
  41. {
  42. throw new sfPluginException(sprintf('Unable to initialize channel "%s"', $channel->getMessage()));
  43. }
  44. $mirror = $this->config->get('preferred_mirror', null, $channelName);
  45. if (!$channel->supportsREST($mirror))
  46. {
  47. throw new sfPluginRestException(sprintf('The channel "%s" does not support the REST protocol', $channelName));
  48. }
  49. return $channel->getBaseURL('REST1.1', $mirror);
  50. }
  51. /**
  52. * Returns the license for a given plugin and version.
  53. *
  54. * @param string $plugin The plugin name
  55. * @param string $version The version
  56. *
  57. * @return string The license
  58. */
  59. public function getPluginLicense($plugin, $version)
  60. {
  61. $info = $this->packageInfo($this->restBase, $plugin);
  62. if (PEAR::isError($info))
  63. {
  64. throw new sfPluginRestException(sprintf('Unable to get plugin licence information for plugin "%s": %s', $plugin, $info->getMessage()));
  65. }
  66. if (null === $info)
  67. {
  68. // plugin does not exist
  69. return null;
  70. }
  71. if (!isset($info['license']) || null === $info['license'])
  72. {
  73. throw new Exception('No license found for this plugin!');
  74. }
  75. return $info['releases'][$version]['license'];
  76. }
  77. /**
  78. * Gets the all available versions for a given plugin.
  79. *
  80. * @param string $plugin The plugin name
  81. * @param string $stability The stability name
  82. *
  83. * @return array An array of versions
  84. */
  85. public function getPluginVersions($plugin, $stability = null)
  86. {
  87. $allreleases = $this->_rest->retrieveData($this->restBase.'r/'.strtolower($plugin).'/allreleases.xml');
  88. if (PEAR::isError($allreleases))
  89. {
  90. throw new sfPluginRestException(sprintf('Unable to get information for plugin "%s": %s', $plugin, $allreleases->getMessage()));
  91. }
  92. if (!isset($allreleases['r']) || (isset($allreleases['r']) && !is_array($allreleases['r']) || !count($allreleases['r'])))
  93. {
  94. throw new sfPluginRestException(sprintf('No release available for plugin "%s"', $plugin));
  95. }
  96. if (!isset($allreleases['r'][0]))
  97. {
  98. $allreleases['r'] = array($allreleases['r']);
  99. }
  100. $versions = array();
  101. $allowedStates = $this->getAllowedStates($stability);
  102. foreach ($allreleases['r'] as $release)
  103. {
  104. if (!isset($allowedStates[$release['s']]))
  105. {
  106. continue;
  107. }
  108. $versions[] = $release['v'];
  109. }
  110. if (!count($versions))
  111. {
  112. throw new sfPluginException(sprintf('No release available for plugin "%s" in state "%s"', $plugin, $stability));
  113. }
  114. return $versions;
  115. }
  116. /**
  117. * Returns plugin dependencies.
  118. *
  119. * @param string $plugin The plugin name
  120. * @param string $version The plugin version
  121. *
  122. * @return array An array of depedencies
  123. */
  124. public function getPluginDependencies($plugin, $version)
  125. {
  126. $dependencies = $this->_rest->retrieveData($this->restBase.'r/'.strtolower($plugin).'/deps.'.$version.'.txt');
  127. if (PEAR::isError($dependencies))
  128. {
  129. throw new sfPluginRestException(sprintf('Unable to get dependencies information for plugin "%s": %s', $plugin, $dependencies->getMessage()));
  130. }
  131. return unserialize($dependencies);
  132. }
  133. /**
  134. * Gets the plugin download URL.
  135. *
  136. * @param string $plugin The plugin name
  137. * @param string $version The plugin version
  138. * @param string $stability The stability
  139. *
  140. * @return string The URL for the plugin
  141. */
  142. public function getPluginDownloadURL($plugin, $version, $stability)
  143. {
  144. $installed = $this->config->getRegistry()->packageInfo($plugin, 'version', $this->channel);
  145. if ($installed >= $version)
  146. {
  147. throw new sfPluginException(sprintf('Plugin "%s" version "%s" is already installed (you tried to install version "%s")', $plugin, $installed, $version));
  148. }
  149. $info = $this->getDownloadURL($this->restBase, array('channel' => $this->channel, 'package' => $plugin, 'version' => $version), $stability, $installed);
  150. if (PEAR::isError($info))
  151. {
  152. throw new sfPluginRestException(sprintf('Unable to get download information for plugin "%s | %s | %s": %s', $plugin, $version, $stability, $info->getMessage()));
  153. }
  154. if (!isset($info['url']))
  155. {
  156. throw new sfPluginRestException(sprintf('Plugin "%s" cannot be installed (No URL found)', $plugin));
  157. }
  158. return $info['url'].(extension_loaded('zlib') ? '.tgz' : '.tar');
  159. }
  160. /**
  161. * Returns an array of set of possible states sorted from most to least stable.
  162. *
  163. * @param string $stability Stability name
  164. *
  165. * @return array An array of stability names
  166. */
  167. protected function getAllowedStates($stability = null)
  168. {
  169. $stability = null === $stability ? $this->config->get('preferred_state', null, $this->channel) : $stability;
  170. return array_flip($this->betterStates($stability, true));
  171. }
  172. /**
  173. * Proxies method to the PEAR REST10 object.
  174. *
  175. * @param string $method The method name
  176. * @param array $arguments An array of arguments
  177. */
  178. public function __call($method, $arguments)
  179. {
  180. if (method_exists($this->rest10, $method))
  181. {
  182. return call_user_func_array(array($this->rest10, $method), $arguments);
  183. }
  184. }
  185. }

Debug toolbar