Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
factory.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage HTTP
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * HTTP factory class.
14  *
15  * @package Joomla.Platform
16  * @subpackage HTTP
17  * @since 12.1
18  */
20 {
21  /**
22  * method to receive Http instance.
23  *
24  * @param JRegistry $options Client options object.
25  * @param mixed $adapters Adapter (string) or queue of adapters (array) to use for communication.
26  *
27  * @return JHttp Joomla Http class
28  *
29  * @since 12.1
30  */
31  public static function getHttp(JRegistry $options = null, $adapters = null)
32  {
33  if (empty($options))
34  {
35  $options = new JRegistry;
36  }
37 
38  return new JHttp($options, self::getAvailableDriver($options, $adapters));
39  }
40 
41  /**
42  * Finds an available http transport object for communication
43  *
44  * @param JRegistry $options Option for creating http transport object
45  * @param mixed $default Adapter (string) or queue of adapters (array) to use
46  *
47  * @return JHttpTransport Interface sub-class
48  *
49  * @since 12.1
50  */
51  public static function getAvailableDriver(JRegistry $options, $default = null)
52  {
53  if (is_null($default))
54  {
55  $availableAdapters = self::getHttpTransports();
56  }
57  else
58  {
59  settype($default, 'array');
60  $availableAdapters = $default;
61  }
62 
63  // Check if there is at least one available http transport adapter
64  if (!count($availableAdapters))
65  {
66  return false;
67  }
68 
69  foreach ($availableAdapters as $adapter)
70  {
71  $class = 'JHttpTransport' . ucfirst($adapter);
72 
73  if ($class::isSupported())
74  {
75  return new $class($options);
76  }
77  }
78 
79  return false;
80  }
81 
82  /**
83  * Get the http transport handlers
84  *
85  * @return array An array of available transport handlers
86  *
87  * @since 12.1
88  */
89  public static function getHttpTransports()
90  {
91  $names = array();
92  $iterator = new DirectoryIterator(__DIR__ . '/transport');
93 
94  foreach ($iterator as $file)
95  {
96  $fileName = $file->getFilename();
97 
98  // Only load for php files.
99  // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
100  if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php')
101  {
102  $names[] = substr($fileName, 0, strrpos($fileName, '.'));
103  }
104  }
105 
106  return $names;
107  }
108 }