Development

/branches/1.2/lib/plugin/sfPearRestPlugin.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugin/sfPearRestPlugin.class.php

Revision 14830, 6.5 kB (checked in by dwhittle, 5 years ago)

1.2: fixed notice in sfPearRestPlugin (closes #5641)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfPearRestPlugin interacts with a symfony plugin channel.
13  *
14  * @package    symfony
15  * @subpackage plugin
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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     if (!$channel->supportsREST($mirror))
67     {
68       throw new sfPluginRestException(sprintf('The channel "%s" does not support the REST protocol', $channelName));
69     }
70
71     return $channel->getBaseURL('REST1.1', $mirror);
72   }
73
74   /**
75    * Returns the license for a given plugin and version.
76    *
77    * @param string $plugin  The plugin name
78    * @param string $version The version
79    *
80    * @return string The license
81    */
82   public function getPluginLicense($plugin, $version)
83   {
84     $info = $this->packageInfo($this->restBase, $plugin);
85
86     if (PEAR::isError($info))
87     {
88       throw new sfPluginRestException(sprintf('Unable to get plugin licence information for plugin "%s": %s', $plugin, $info->getMessage()));
89     }
90
91     if (is_null($info))
92     {
93       // plugin does not exist
94       return null;
95     }
96
97     if (!isset($info['license']) || is_null($info['license']))
98     {
99       throw new Exception('No license found for this plugin!');
100     }
101
102     return $info['releases'][$version]['license'];
103   }
104
105   /**
106    * Gets the all available versions for a given plugin.
107    *
108    * @param  string $plugin     The plugin name
109    * @param  string $stability  The stability name
110    *
111    * @return array  An array of versions
112    */
113   public function getPluginVersions($plugin, $stability = null)
114   {
115     $allreleases = $this->_rest->retrieveData($this->restBase.'r/'.strtolower($plugin).'/allreleases.xml');
116     if (PEAR::isError($allreleases))
117     {
118       throw new sfPluginRestException(sprintf('Unable to get information for plugin "%s": %s', $plugin, $allreleases->getMessage()));
119     }
120
121     if (!isset($allreleases['r']) || (isset($allreleases['r']) && !is_array($allreleases['r']) || !count($allreleases['r'])))
122     {
123       throw new sfPluginRestException(sprintf('No release available for plugin "%s"', $plugin));
124     }
125
126     if (!isset($allreleases['r'][0]))
127     {
128       $allreleases['r'] = array($allreleases['r']);
129     }
130
131     $versions = array();
132     $allowedStates = $this->getAllowedStates($stability);
133     foreach ($allreleases['r'] as $release)
134     {
135       if (!isset($allowedStates[$release['s']]))
136       {
137         continue;
138       }
139
140       $versions[] = $release['v'];
141     }
142
143     if (!count($versions))
144     {
145       throw new sfPluginException(sprintf('No release available for plugin "%s" in state "%s"', $plugin, $stability));
146     }
147
148     return $versions;
149   }
150
151   /**
152    * Returns plugin dependencies.
153    *
154    * @param string $plugin  The plugin name
155    * @param string $version The plugin version
156    *
157    * @return array  An array of depedencies
158    */
159   public function getPluginDependencies($plugin, $version)
160   {
161     $dependencies = $this->_rest->retrieveData($this->restBase.'r/'.strtolower($plugin).'/deps.'.$version.'.txt');
162     if (PEAR::isError($dependencies))
163     {
164       throw new sfPluginRestException(sprintf('Unable to get dependencies information for plugin "%s": %s', $plugin, $dependencies->getMessage()));
165     }
166
167     return unserialize($dependencies);
168   }
169
170   /**
171    * Gets the plugin download URL.
172    *
173    * @param  string $plugin     The plugin name
174    * @param  string $version    The plugin version
175    * @param  string $stability  The stability
176    *
177    * @return string The URL for the plugin
178    */
179   public function getPluginDownloadURL($plugin, $version, $stability)
180   {
181     $installed = $this->config->getRegistry()->packageInfo($plugin, 'version', $this->channel);
182     if ($installed >= $version)
183     {
184       throw new sfPluginException(sprintf('Plugin "%s" version "%s" is already installed (you tried to install version "%s")', $plugin, $installed, $version));
185     }
186
187     $info = $this->getDownloadURL($this->restBase, array('channel' => $this->channel, 'package' => $plugin, 'version' => $version), $stability, $installed);
188     if (PEAR::isError($info))
189     {
190       throw new sfPluginRestException(sprintf('Unable to get download information for plugin "%s | %s | %s": %s', $plugin, $version, $stability, $info->getMessage()));
191     }
192
193     if (!isset($info['url']))
194     {
195       throw new sfPluginRestException(sprintf('Plugin "%s" cannot be installed (No URL found)', $plugin));
196     }
197
198     return $info['url'].(extension_loaded('zlib') ? '.tgz' : '.tar');
199   }
200
201   /**
202    * Returns an array of set of possible states sorted from most to least stable.
203    *
204    * @param  string $stability Stability name
205    *
206    * @return array  An array of stability names
207    */
208   protected function getAllowedStates($stability = null)
209   {
210     $stability = is_null($stability) ? $this->config->get('preferred_state', null, $this->channel) : $stability;
211
212     return array_flip($this->betterStates($stability, true));
213   }
214
215   /**
216    * Proxies method to the PEAR REST10 object.
217    *
218    * @param string $method    The method name
219    * @param array  $arguments An array of arguments
220    */
221   public function __call($method, $arguments)
222   {
223     if (method_exists($this->rest10, $method))
224     {
225       return call_user_func_array(array($this->rest10, $method), $arguments);
226     }
227   }
228 }
229
Note: See TracBrowser for help on using the browser.