Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 170 lines (151 sloc) 4.487 kb
e7f3c31 GWoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf GWoo Happy 2012!
gwoo authored
5 * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
e7f3c31 GWoo going lithium
gwoo authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
f65a402 GWoo updating headers
gwoo authored
8
9e2105b GWoo Moving \http to \net\http and \util\socket to \net\socket.
gwoo authored
9 namespace lithium\net;
e7f3c31 GWoo going lithium
gwoo authored
10
9e2105b GWoo Moving \http to \net\http and \util\socket to \net\socket.
gwoo authored
11 /**
827e14f Michael Nitschinger QA: formatting and docblock changes.
daschl authored
12 * Abstract class for connecting to sockets with various adapters.
9e2105b GWoo Moving \http to \net\http and \util\socket to \net\socket.
gwoo authored
13 *
827e14f Michael Nitschinger QA: formatting and docblock changes.
daschl authored
14 * Currently, Curl, Stream and Context adapters are available.
9e2105b GWoo Moving \http to \net\http and \util\socket to \net\socket.
gwoo authored
15 */
e7f3c31 GWoo going lithium
gwoo authored
16 abstract class Socket extends \lithium\core\Object {
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
17
0cdb915 GWoo workign some more on standard socket interface
gwoo authored
18 /**
827e14f Michael Nitschinger QA: formatting and docblock changes.
daschl authored
19 * The resource for the current connection.
0cdb915 GWoo workign some more on standard socket interface
gwoo authored
20 *
21 * @var resource
22 */
e7f3c31 GWoo going lithium
gwoo authored
23 protected $_resource = null;
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
24
25 /**
827e14f Michael Nitschinger QA: formatting and docblock changes.
daschl authored
26 * The classes for the socket.
077ba19 GWoo adding '\net\Message` for the basic request/response from `\net\Socket`.
gwoo authored
27 *
28 * @var array
29 */
30 protected $_classes = array(
ed584fa GWoo making `\net\socket` classes work the same
gwoo authored
31 'request' => 'lithium\net\Message',
ed575a0 Nate Abele Fixing socket lookup implementation for `\net\http\Service`. Fixing API ...
nateabele authored
32 'response' => 'lithium\net\Message'
077ba19 GWoo adding '\net\Message` for the basic request/response from `\net\Socket`.
gwoo authored
33 );
34
35 /**
827e14f Michael Nitschinger QA: formatting and docblock changes.
daschl authored
36 * Auto config.
077ba19 GWoo adding '\net\Message` for the basic request/response from `\net\Socket`.
gwoo authored
37 *
827e14f Michael Nitschinger QA: formatting and docblock changes.
daschl authored
38 * @var array
077ba19 GWoo adding '\net\Message` for the basic request/response from `\net\Socket`.
gwoo authored
39 */
40 protected $_autoConfig = array('classes' => 'merge');
41
42 /**
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
43 * Constructor.
44 *
45 * @param array $config Available configuration options are:
46 * - `'persistent'`: Use a persistent connection (defaults to `false`).
47 * - `'protocol'`: Transfer protocol to use (defaults to `'tcp'`).
48 * - `'host'`: Host name or address (defaults to `'localhost'`).
49 * - `'login'`: Username for a login (defaults to `'root'`).
50 * - `'password'`: Password for a login (defaults to `''`).
51 * - `'port'`: Host port (defaults to `80`).
52 * - `'timeout'`: Seconds after opening the socket times out (defaults to `30`).
53 */
f80098d Nate Abele Adding type hinting to constructor `$config` arrays and all applicable `...
nateabele authored
54 public function __construct(array $config = array()) {
e7f3c31 GWoo going lithium
gwoo authored
55 $defaults = array(
56 'persistent' => false,
907efd2 GWoo beginning refactor of \net
gwoo authored
57 'scheme' => 'tcp',
e7f3c31 GWoo going lithium
gwoo authored
58 'host' => 'localhost',
59 'port' => 80,
60 'timeout' => 30
61 );
aeed15c Nate Abele Moving database results handling to self-contained iterable result objec...
nateabele authored
62 parent::__construct($config + $defaults);
e7f3c31 GWoo going lithium
gwoo authored
63 }
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
64
65 /**
66 * Opens the socket and sets `Socket::$_resource`.
67 *
827e14f Michael Nitschinger QA: formatting and docblock changes.
daschl authored
68 * @param array $options Update the config settings.
c25e039 Howard Lince III Cleaning up PHPDoc @return tags with multiple return types defined.
Howard3 authored
69 * @return mixed The open resource on success, `false` otherwise.
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
70 */
4849a5b GWoo BREAK: refactoring `Socket` and `Service` classes so connections are mor...
gwoo authored
71 public function open(array $options = array()) {
538ed63 GWoo fixes #38
gwoo authored
72 parent::__construct($options + $this->_config);
73 return false;
4849a5b GWoo BREAK: refactoring `Socket` and `Service` classes so connections are mor...
gwoo authored
74 }
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
75
76 /**
77 * Closes the socket and unsets `Socket::$_resource`.
78 *
79 * @return boolean `true` on success, `false` otherwise.
80 */
e7f3c31 GWoo going lithium
gwoo authored
81 abstract public function close();
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
82
83 /**
84 * Test for the end-of-file on the socket.
85 *
86 * @return boolean `true` if end has been reached, `false` otherwise.
87 */
e7f3c31 GWoo going lithium
gwoo authored
88 abstract public function eof();
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
89
90 /**
91 * Reads from the socket.
92 *
077ba19 GWoo adding '\net\Message` for the basic request/response from `\net\Socket`.
gwoo authored
93 * @return object `lithium\net\Message`
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
94 */
e7f3c31 GWoo going lithium
gwoo authored
95 abstract public function read();
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
96
97 /**
98 * Writes data to the socket.
99 *
100 * @param mixed $data
a0930f4 Jeff Loiselle fixed spelling errors
phishy authored
101 * @return boolean `true` if data has been successfully written, `false` otherwise.
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
102 */
e7f3c31 GWoo going lithium
gwoo authored
103 abstract public function write($data);
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
104
105 /**
106 * Sets the timeout on the socket *connection*.
107 *
108 * @param integer $time Seconds after the connection times out.
a0930f4 Jeff Loiselle fixed spelling errors
phishy authored
109 * @return Boolean `true` if timeout has been set, `false` otherwise.
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
110 */
e7f3c31 GWoo going lithium
gwoo authored
111 abstract public function timeout($time);
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
112
113 /**
114 * Sets the encoding of the socket connection.
115 *
116 * @param string $charset The character set to use.
117 * @return boolean `true` if encoding has been set, `false` otherwise.
118 */
e7f3c31 GWoo going lithium
gwoo authored
119 abstract public function encoding($charset);
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
120
121 /**
ed584fa GWoo making `\net\socket` classes work the same
gwoo authored
122 * Sets the options to be used in subsequent requests.
123 *
124 * @param array $flags If $values is an array, $flags will be used as the
125 * keys to an associative array of curl options. If $values is not set,
126 * then $flags will be used as the associative array.
127 * @param array $value If set, this array becomes the values for the
128 * associative array of curl options.
129 * @return void
130 */
131 public function set($flags, $value = null) {}
132
133 /**
134 * Aggregates read and write methods into a coherent request response
135 *
136 * @param mixed $message a request object based on `\lithium\net\Message`
137 * @param array $options
138 * - '`response`': a fully-namespaced string for the response object
139 * @return object a response object based on `\lithium\net\Message`
140 */
141 public function send($message = null, array $options = array()) {
142 $defaults = array('response' => $this->_classes['response']);
143 $options += $defaults;
144
145 if ($this->write($message)) {
cafcbcc GWoo adding `tests\integration\SocketTest`
gwoo authored
146 $config = array('message' => $this->read()) + $this->_config;
147 return $this->_instance($options['response'], $config);
ed584fa GWoo making `\net\socket` classes work the same
gwoo authored
148 }
149 }
150
151 /**
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
152 * Destructor.
153 *
154 * @return void
155 */
e7f3c31 GWoo going lithium
gwoo authored
156 public function __destruct() {
157 $this->close();
158 }
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
159
160 /**
161 * Returns the resource.
162 *
c25e039 Howard Lince III Cleaning up PHPDoc @return tags with multiple return types defined.
Howard3 authored
163 * @return resource
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
164 */
e7f3c31 GWoo going lithium
gwoo authored
165 public function resource() {
166 return $this->_resource;
b37dac0 David Persson Adding dockblocks to Socket.
davidpersson authored
167 }
8eed07a Nate Abele Adding PHP close tags to classes, removing old docblock info.
nateabele authored
168 }
169
170 ?>
Something went wrong with that request. Please try again.