sfPearRestPlugin.class.php 6.5 KB

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