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