Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace Guzzle\Http;
4
 
5
use Guzzle\Http\Client;
6
use Guzzle\Http\ClientInterface;
7
use Guzzle\Stream\StreamRequestFactoryInterface;
8
use Guzzle\Stream\PhpStreamRequestFactory;
9
 
10
/**
11
 * Simplified interface to Guzzle that does not require a class to be instantiated
12
 */
13
final class StaticClient
14
{
15
    /** @var Client Guzzle client */
16
    private static $client;
17
 
18
    /**
19
     * Mount the client to a simpler class name for a specific client
20
     *
21
     * @param string          $className Class name to use to mount
22
     * @param ClientInterface $client    Client used to send requests
23
     */
24
    public static function mount($className = 'Guzzle', ClientInterface $client = null)
25
    {
26
        class_alias(__CLASS__, $className);
27
        if ($client) {
28
            self::$client = $client;
29
        }
30
    }
31
 
32
    /**
33
     * @param  string $method  HTTP request method (GET, POST, HEAD, DELETE, PUT, etc)
34
     * @param  string $url     URL of the request
35
     * @param  array  $options Options to use with the request. See: Guzzle\Http\Message\RequestFactory::applyOptions()
36
     * @return \Guzzle\Http\Message\Response|\Guzzle\Stream\Stream
37
     */
38
    public static function request($method, $url, $options = array())
39
    {
40
        // @codeCoverageIgnoreStart
41
        if (!self::$client) {
42
            self::$client = new Client();
43
        }
44
        // @codeCoverageIgnoreEnd
45
 
46
        $request = self::$client->createRequest($method, $url, null, null, $options);
47
 
48
        if (isset($options['stream'])) {
49
            if ($options['stream'] instanceof StreamRequestFactoryInterface) {
50
                return $options['stream']->fromRequest($request);
51
            } elseif ($options['stream'] == true) {
52
                $streamFactory = new PhpStreamRequestFactory();
53
                return $streamFactory->fromRequest($request);
54
            }
55
        }
56
 
57
        return $request->send();
58
    }
59
 
60
    /**
61
     * Send a GET request
62
     *
63
     * @param string $url     URL of the request
64
     * @param array  $options Array of request options
65
     *
66
     * @return \Guzzle\Http\Message\Response
67
     * @see Guzzle::request for a list of available options
68
     */
69
    public static function get($url, $options = array())
70
    {
71
        return self::request('GET', $url, $options);
72
    }
73
 
74
    /**
75
     * Send a HEAD request
76
     *
77
     * @param string $url     URL of the request
78
     * @param array  $options Array of request options
79
     *
80
     * @return \Guzzle\Http\Message\Response
81
     * @see Guzzle::request for a list of available options
82
     */
83
    public static function head($url, $options = array())
84
    {
85
        return self::request('HEAD', $url, $options);
86
    }
87
 
88
    /**
89
     * Send a DELETE request
90
     *
91
     * @param string $url     URL of the request
92
     * @param array  $options Array of request options
93
     *
94
     * @return \Guzzle\Http\Message\Response
95
     * @see Guzzle::request for a list of available options
96
     */
97
    public static function delete($url, $options = array())
98
    {
99
        return self::request('DELETE', $url, $options);
100
    }
101
 
102
    /**
103
     * Send a POST request
104
     *
105
     * @param string $url     URL of the request
106
     * @param array  $options Array of request options
107
     *
108
     * @return \Guzzle\Http\Message\Response
109
     * @see Guzzle::request for a list of available options
110
     */
111
    public static function post($url, $options = array())
112
    {
113
        return self::request('POST', $url, $options);
114
    }
115
 
116
    /**
117
     * Send a PUT request
118
     *
119
     * @param string $url     URL of the request
120
     * @param array  $options Array of request options
121
     *
122
     * @return \Guzzle\Http\Message\Response
123
     * @see Guzzle::request for a list of available options
124
     */
125
    public static function put($url, $options = array())
126
    {
127
        return self::request('PUT', $url, $options);
128
    }
129
 
130
    /**
131
     * Send a PATCH request
132
     *
133
     * @param string $url     URL of the request
134
     * @param array  $options Array of request options
135
     *
136
     * @return \Guzzle\Http\Message\Response
137
     * @see Guzzle::request for a list of available options
138
     */
139
    public static function patch($url, $options = array())
140
    {
141
        return self::request('PATCH', $url, $options);
142
    }
143
 
144
    /**
145
     * Send an OPTIONS request
146
     *
147
     * @param string $url     URL of the request
148
     * @param array  $options Array of request options
149
     *
150
     * @return \Guzzle\Http\Message\Response
151
     * @see Guzzle::request for a list of available options
152
     */
153
    public static function options($url, $options = array())
154
    {
155
        return self::request('OPTIONS', $url, $options);
156
    }
157
}